本文介绍了如何从.NET Core 3.0 WPF应用程序使用SOAP Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SOAP Web服务.我想在我的WPF应用程序中使用它.我正在使用.NET Core 3.0,Visual Studio2019.因此,我使用了Microsoft WCF Web服务引用提供程序来添加对项目的引用.这创建了以下类:和

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

现在,如何通过调用Web服务使用它们来获取数据?我尝试了以下方法;这是调用Web服务的正确方法吗?在互联网上看到的许多示例中,客户端构造函数不需要EndpointConfiguration参数.为什么在我的情况下需要这样做?我可以直接使用InsertRecordResponse类而不先创建客户端"吗?如果是,我该怎么做(以使代码更简单).最后,InsertRecordRequest和InsertRecordRequestBody的用途是什么?

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

推荐答案

这是因为在生成客户端代理类时选中了始终生成消息合同"选项.通常这不是必需的.您可以直接生成异步方法,并使用异步编程模型来构建应用程序逻辑.这不会导致WPF的延迟崩溃.

您也可以通过选择以下选项来生成同步方法,就像基于dotnetframework的项目一样.

此外,基于Dotnetframework的项目可以直接从webconfig文件进行配置.由于此项目是基于Core的,因此不会生成任何webconfig文件,因此默认构造函数将使用一个参数.此参数指定枚举,构造函数根据枚举类型获取终结点和绑定信息.

请参考我的示例.

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.

这篇关于如何从.NET Core 3.0 WPF应用程序使用SOAP Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 06:51