如何从 .NET Core 3.0 WPF 应用程序使用 SOAP Web 服务 [英] How to consume SOAP web service from .NET Core 3.0 WPF app

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

问题描述

我有一个 SOAP 网络服务.我想在我的 WPF 应用程序中使用它.我使用的是 .NET Core 3.0,Visual Studio 2019.所以我使用 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 服务的正确方法吗?在我在 Internet 上看到的许多示例中,客户端构造函数不需要 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

推荐答案

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

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

此外,可以直接从 webconfig 文件配置基于 Dotnetframework 的项目.由于这个项目是基于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 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆