简体   繁体   English

调用Web服务时异步/等待不起作用

[英]Async/Await not working when calling a web service

For some reason, I can't get the generated [Service Method Name]Async version of any method to compile in a web service I create in VB.NET. 由于某些原因,我无法生成要在VB.NET中创建的Web服务中编译的任何方法的生成的[服务方法名称]异步版本。

I get the same format of error message, no matter whether I'm using the actual service I've created or even the default service with its HelloWorld and HelloWorldAsync methods. 无论使用的是我创建的实际服务,还是默认的服务及其HelloWorld和HelloWorldAsync方法,我都会收到相同格式的错误消息。 So I'm obviously not doing something key that's required. 因此,我显然没有在做必要的关键操作。

The format of the error message in the VS 2012 code window is [Service MethodName]Response cannot be converted to [Type that will be returned]. VS 2012代码窗口中错误消息的格式为[服务方法名称]响应无法转换为[将返回的类型]。 So in the case of the default HelloWorld method, this code snippet : Imports StackOverflowClient.TestServiceReference 因此,对于默认的HelloWorld方法,此代码段:Imports StackOverflowClient.TestServiceReference

Public Class Form1 Dim SOClient As New TestWebServiceSoapClient 公共类Form1 Dim SOClient作为新的TestWebServiceSoapClient

Private Async Sub SayHelloAsync()
    Label1.Text = Await SOClient.HelloWorldAsync()
End Sub

End Class 末级

(Where StackOverflowClient is the name of the project,TestServiceReference is the Namespace assigned to the Service, and SOClient is the proxy class) will error out with the message: HelloWorldResponse cannot be converted to String. (其中StackOverflowClient是项目的名称,TestServiceReference是分配给Service的名称空间,而SOClient是代理类)将出错,并显示以下消息:HelloWorldResponse无法转换为String。 (In my real application, the message will be GetUserResponse cannot be converted to the User type I've created in the service. (在我的真实应用程序中,该消息将是GetUserResponse,无法将其转换为我在服务中创建的用户类型。

I've also sometimes had an error message of "Expression does not produce a value" when trying to do this with classes from the service, eg: lblUser.Text = StaffInfoService.GetNextStaffMemberAsync() 当尝试使用服务中的类来执行此操作时,有时还会出现错误消息“表达式不产生值”,例如:lblUser.Text = StaffInfoService.GetNextStaffMemberAsync()

I have the 'Allow generation of asynchronous operations' checkbox checked in the Service Reference Settings. 我在“服务参考设置”中选中了“允许异步操作生成”复选框。

I can't see what I've missed. 我看不到我错过的一切。 I don't want to have to use the older Begin/Completed/Callback approach of pre VS2010 unless I absolutely have to. 除非绝对必要,否则我不需要使用VS2010之前的较早的“开始/完成/回调”方法。 Any pointers to using Async/Await for this task will be really appreciated. 对于使用Async / Await来完成此任务的任何指示,我们将不胜感激。

That sounds like HelloWorldAsync is returning a type of Task<HelloWorldResponse> . 听起来好像HelloWorldAsync正在返回Task<HelloWorldResponse>类型。 When you Await it, the task is "unwrapped" and the type of the value is then HelloWorldResponse . 当您Await它时,任务将被“解开”,然后值的类型为HelloWorldResponse As the error message states, you can't convert this to a string. 如错误消息所述,您不能将其转换为字符串。 Perhaps you are looking for a particular property of that type? 也许您正在寻找该类型的特定属性?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM