![](/img/trans.png)
[英]Unable to authenticate soap web service when adding via Microsoft WCF Web service Reference Provider to .NET Core 3 console application
[英]How to properly use SendDataAsync method defined on the web service using Microsoft WCF Web Service Reference Provider client?
我可以訪問包含 SendDataAsync 方法的特定.asmx web 服務的 WSDL 文件 - 基本上我指定了要發送的時間戳和值。 I uploaded the WSDL file to my project in Visual Studio 2019 as a connected service (Add->Connected Service->Microsoft WCF Web Service Reference Provider->Browse->I added location of the WSDL file and specified the service that included SendDataAsync method )。 之后我創建了一個新客戶端並嘗試使用我的方法:
ServiceSoapClient client = new ServiceSoapClient(ServiceSoapClient.EndpointConfiguration.ServiceSoap);
client.SampleData sd = new client.SampleData();
sd.TStamp = DateTime.Now;
sd.Value = 10;
client.SendDataAsync(sd);
不幸的是,它不起作用。 我沒有收到任何錯誤或異常,因此我嘗試通過 Fiddler 檢查 web 服務的響應。 我發現實際上沒有任何東西被傳輸。 沒有建立任何聯系,什么都沒有。 現在我試着理解我做錯了什么。 我使用web服務上定義的方法的方式有問題嗎? 或者,也許該方法實際上並沒有像名字所暗示的那樣做? 或者問題可能與該方法是異步的事實有關? 歡迎任何建議::)
根據你的描述,我做了一個demo。demo中的異步方法是客戶端根據SendData自動生成的,即服務端沒有SendDataAsync方法,服務端只有SendData。
public void SendData(SampleData data)
{
Console.WriteLine(data.TStamp);
Console.WriteLine(data.value);
Console.WriteLine("success");
}
這是服務器端的 SendData 方法。
public System.Threading.Tasks.Task SendDataAsync(Client_SOAP.ServiceReference1.SampleData data) {
return base.Channel.SendDataAsync(data);
}
這是客戶端自動生成的異步方法。
ServiceReference1.Service1Client service1Client = new Service1Client();
SampleData sampleData = new SampleData();
sampleData.value = 10;
sampleData.TStamp = DateTime.Now;
service1Client.SendDataAsync(sampleData);
service1Client.Close();
Console.ReadLine();
這是客戶端調用異步方法。
這是客戶端調用后服務端的執行結果。
另一種情況,如果你的異步方法是服務端實現的,那么服務端實現異步操作有3種方式:基於任務的異步模式、基於事件的異步模式、IAsyncResult異步模式。服務器端使用的異步model,客戶端調用方式不同。
以下鏈接詳細介紹了客戶端的異步調用:
https://docs.microsoft.com/en-us/dotnet/framework/wcf/synchronous-and-asynchronous-operations
好的,感謝 Paulo Morgado 的評論和丁鵬的回答,我設法解決了我的問題。 使用異步方法的正確方法,例如我的 SendDataAsync,是使用await
運算符。 我將方法調用從:
client.SendDataAsync(sd);
至:
response = await client.SendDataAsync(sd);
我還必須將void Main
更改為async Main
並且該方法現在可以使用:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.