简体   繁体   中英

How to properly use SendDataAsync method defined on the web service using Microsoft WCF Web Service Reference Provider client?

I have access to WSDL file of a specific.asmx web service that contains a SendDataAsync method - basically I specify the TimeStamp and Value to be send. 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). After that I created a new client and tried to use my method like that:

ServiceSoapClient client = new ServiceSoapClient(ServiceSoapClient.EndpointConfiguration.ServiceSoap);
client.SampleData sd = new client.SampleData();
sd.TStamp = DateTime.Now;
sd.Value = 10;
client.SendDataAsync(sd);

Unfortunately, it doesn't work. I don't receive any errors or exceptions so I tried to check the response from the web service via Fiddler. I found out that actually nothing is being transmitted. No connection is being made, nothing. Now I try to understand what I'm doing wrong. Is my way of using the method defined on the web service wrong? Or maybe the method doesn't actually do what the name suggests? Or could the problem be related to the fact that the method is Async? Any suggestions are welcome: :)

According your description,I made a demo.The asynchronous method in demo is automatically generated by the client-side according to SendData, that is, the server-side has no SendDataAsync method, and the server-side only has SendData.

     public void SendData(SampleData data)
    {
        Console.WriteLine(data.TStamp);
        Console.WriteLine(data.value);
        Console.WriteLine("success");
    }

This is the SendData method of the server-side.

         public System.Threading.Tasks.Task SendDataAsync(Client_SOAP.ServiceReference1.SampleData data) {
        return base.Channel.SendDataAsync(data);
    }

This is an asynchronous method automatically generated by the client-side.

      ServiceReference1.Service1Client service1Client = new Service1Client();
        SampleData sampleData = new SampleData();
        sampleData.value = 10;
        sampleData.TStamp = DateTime.Now;
        service1Client.SendDataAsync(sampleData);
        service1Client.Close();
        Console.ReadLine();

This is the client-side calling asynchronous methods.

在此处输入图像描述

This is the execution result of the server-side after the client-side calls.

In another case,if your asynchronous method is implemented by the server-side, there are three ways for the server to implement asynchronous operation: the task-based asynchronous pattern, the Event-based Asynchronous Pattern, the IAsyncResult asynchronous pattern.For the different asynchronous model used by the server-side, the client calls in different ways.

The following link details the asynchronous invocation of the client-side:

https://docs.microsoft.com/en-us/dotnet/framework/wcf/synchronous-and-asynchronous-operations

Okay, thanks to both the comment from Paulo Morgado and the answer from Ding peng I managed to solve my problem. The proper way of using asynchronous method, such as SendDataAsync in my case, is with the await operator. I changed the method invocation from:

client.SendDataAsync(sd);

to:

response = await client.SendDataAsync(sd);

I also had to change void Main to async Main and the method works now:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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