问题
I have a SOAP web service. I want to consume it in my WPF app. I am using .NET Core 3.0, Visual Studio 2019. So I used the Microsoft WCF Web Service Reference Provider to add reference to my project. This created the following classes: and
Now, how do I use these to get my data by calling the web service? I tried the following; is this the correct way to call the web service? In many examples that I have seen on the internet, the client constructor does not need the EndpointConfiguration parameter. Why is this required in my case? Can I use the InsertRecordResponse class directly without creating the 'client' first? If yes, how would I do that (to keep the code simpler). Finally, what is the use for InsertRecordRequest and InsertRecordRequestBody and what is the differnce?
var client = new mserviceSoapClient(mserviceSoapClient.EndpointConfiguration.mserviceSoap);
Task<InsertRecordResponse> x = client.InsertRecordAsync("", "", "", "", "",""); //call InsertRecord asynchronously
回答1:
This is because you checked the Always Generate Message Contract option when you generated the client proxy class. Usually this is not necessary. You can directly generate asynchronous methods and use the asynchronous programming model to build your application logic. This does not cause a delay crash of WPF.
You could also generate synchronous methods by selected the below option, just like the dotnetframework-based project.
Besides, Dotnetframework-based projects can be configured directly from webconfig files. Since this project is Core-based, no webconfig file is generated, so the default constructor takes a parameter. This parameter specifies an enumeration, and the constructor gets endpoint and binding information based on the enumeration type.
Please refer to my example.
//with Message Contract
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client(ServiceReference1.Service1Client.EndpointConfiguration.BasicHttpBinding_IService1);
var request = new ServiceReference1.GetDataRequest(34);
var response1 = client.GetDataAsync(request);
MyLabel.Content = response1.Result.GetDataResult;
//Without checked MessageContract
ServiceReference2.Service1Client client1 = new ServiceReference2.Service1Client(ServiceReference2.Service1Client.EndpointConfiguration.BasicHttpBinding_IService1);
var response2 = client1.GetDataAsync(34);
MyLabel.Content += " " + response2.Result;
Feel free to let me know if there is anything I can help with.
来源:https://stackoverflow.com/questions/57445647/how-to-consume-soap-web-service-from-net-core-3-0-wpf-app