public Task<Guid> GetMyObjectIdAsync(string objectName)
When I see this, I expect it to have some advantage in using this method, and not just its wrapper in Task.Run() .
In particular, I expect it to release a stream when it gets into some I / O operations or it has the ability to do so.
Now consider if I have code:
_resource = GetResourceForID(GetMyObjectIdAsync(SomeLongRunningWayToGetName()));
If I have a reason for this to be done in the task, and I am in a situation where Task.Run() really makes sense (I have a reason to offload it to another thread) in the best way it would be to wrap it all up:
Task task = Task.Run(() => _resource = GetResourceForID(GetMyObjectIdAsync(SomeLongRunningWayToGetName())));
Here Task.Run() may be a bad idea for me as the caller , or it may be good, because I really get what it gives me. p>
However, if I see your signature, I will think that the best way to do this with your code is to turn it into code that uses this method.
Task task = SomeLongRunningWayToGetName() .ContinueWith(t => GetMyObjectIdAsync(t.Result)) .ContinueWith(t => _resource = GetResourceForIDAsync(t.Result));
(Or similarly using async and await ).
At best, this has a less good distribution of Task.Run() . In the worst case, I am await in order to benefit from better asynchrony, which he does not offer in a context that he could use if he really were there. (For example, I could use this in an MVC action, which I made asynchronous, because I thought that the extra overhead would be repaid in the best use of the thread pool).
So while Task.Run() sometimes useful, in this case it is always bad. If you cannot offer me more asynchrony than I can lead to using the class itself, do not make me think that you are doing this.
Offer only the public XXXAsync() method if it actually calls asynchronous I / O.
If you really need to align an asynchronous method, for example. matching the signature of a common base or interface, then it would be better:
public Task<Guid> GetMyObjectIdAsync(string objectName) { return Task.FromResult(GetMyObjectId(objectName); }
This is also bad (it would still be better for the caller to just call GetMyObjectId() directly), but at least if he is await when he is working in one thread, there is no overhead for using another thread to do the work. therefore, if mixed with another await , the negative impact will be reduced. Therefore, it is useful if you really have to return Task , but cannot add anything useful in what you call it.
But if you don't really need to, just don't do it.
(a private method that calls Run() because you have an advantage on every site and you just add convenience, rather than calling Run() in several places, but this should be well documented as such).