我们有这个方法:
async Task<int> AccessTheWebAsync()
{
HttpClient client = new HttpClient();
Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");
// You can do work here that doesn't rely on the string from GetStringAsync.
DoIndependentWork();
string urlContents = await getStringTask;
//The thing is that this returns an int to a method that has a return type of Task<int>
return urlContents.Length;
}
Run Code Online (Sandbox Code Playgroud)
之间是否隐式转换发生Task<int>和int?如果没有,那么发生了什么?它是如何实现的?
我的 .NET Core 项目只为 WCF 服务生成异步方法。有没有办法像在常规 .NET 项目中一样使用同步方法?
在常规的.NET有两个同步和异步方法,如:
WcfClient.MethodName和WcfClient.MethodNameAsync
在 .NET Core 中只有 WcfClient.MethodNameAsync
这个问题对我有帮助: How to call asynchronous method from synchronous method in C#?
但是这个解决方案会导致其他问题。任何想法如何生成同步方法/函数?