使用 WCF 和 .NET 4.0 在 C# 中实现 SOAP 客户端 [英] Implementing a SOAP client in C# with WCF and .NET 4.0

查看:36
本文介绍了使用 WCF 和 .NET 4.0 在 C# 中实现 SOAP 客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .NET 4.0 中开始使用 WCF 时遇到问题.这是我的情况:

I'm having trouble getting started with WCF in .NET 4.0. This is my situation:

我用 PHP 创建了一个小型 SOAP 服务器.我有一个 C# 项目,我想在其中连接到此服务器并启动 SOAP 通信.

I have created a small SOAP Server in PHP. I have a C# project in which I want to connect to this Server and initiate the SOAP communication.

我的问题是,我不知道如何在 C# 中做到这一点.我找不到正确的 WCF 介绍.有办法做到这一点.但是我找不到正确的类和引用来添加到我的 C# 项目中.是否有任何教程如何在 C# 中实现这一点?我搜索了很多,但没有找到对我有帮助的东西.

My problem is, I have no idea how to do this in C#. I can't find the right introduction to WCF. There are ways to do this. But I can't find the right classes and references to add to my C# project. Are the any tutorials how to achieve this in C#? I searched a lot and found nothing which helped me.

我想在运行时从我的 SOAP 服务器加载 WSDL,发出 SOAP 请求,检索答案并完成.但是我可以从哪里开始呢?关于 WCF 的 MSDN 站点只会让我更加困惑.

I want to load the WSDL from my SOAP Server at run time, make the SOAP request, retrieve the answer and be done. But where can I get started? The MSDN site about WCF is only confusing me more.

编辑:可能没有必要在运行时获取 WSDL 文件.所以不再需要了.
我使用 svcutil 创建该类并将其嵌入到我的项目中.我还没有能够测试它,因为我在使用 MySQL 数据库时遇到了一些问题(它正在运行并且可以从 mysql 命令行工具或 mysqladmin 访问,但我无法使用任何其他程序连接到它......).一旦我知道它是否有效,我会立即报告.

Edit: It probably is not necessary to fetch the WSDL file at run time. So that is not needed anymore.
I used svcutil to create the class and embedded it in my project. I haven't been able to test it yet, because I have some trouble with the MySQL database (It's running and accessible from the mysql command line tool or mysqladmin, but I can't connect to it with any other programm...). I'll report back as soon as I know if it works.

编辑 2:我遵循了 Kevs 方法,最终效果很好.我的最后一个问题是,我在 DLL 中使用了服务类.我也需要使用 DLL 的程序中的 app.config.在我这样做之后,效果很好.

Edit 2: I've followed Kevs approach and it worked out very good in the end. My final problem was, that I used the Service Class in a DLL. I needed the app.config in the programm which used the DLL too. After I did that it worked out well.

推荐答案

执行此操作的最快方法是右键单击客户项目中的引用"下的添加服务引用".将对话框指向 WSDL 的位置,然后单击 Go:

The quickest way to do this is to do right-click "Add Service Reference" in your client's project under References. Point the dialogue at the location of the WSDL and click Go:

我使用的 URL 用于 .NET 服务引用的样式,您需要替换为您的 PHP SOAP 服务用于公开其 WSDL 的任何内容.

The URL I used was for the style of a .NET service reference, you'll need to replace with whatever your PHP SOAP service uses to expose its WSDL.

这样做将生成一个客户端代理,您可以将其实例化以与您的网络服务进行通信.

Doing this will generate a client side proxy you can instantiate to communicate with your web service.

要访问该服务,您可以执行以下操作(尽管您的具体实现会有所不同):

To access the service you can then do something like (although your specific implementation won't be the same):

MyService.MyWebServiceSoapClient ws = new MyService.MyWebServiceSoapClient();
string result ws.DoThing();

要特别注意代理类名的SoapClient部分,这是由代理代码生成器添加到soap服务名中的.

Pay particular attention to the SoapClient part of the proxy class name, this is added to the name of the soap service name by the proxy code generator.

代理生成器还将在您的 web.configapp.config 文件中创建所有必要的配置.

The proxy generator will also create all the necessary configuration in your web.config or app.config file.

这篇关于使用 WCF 和 .NET 4.0 在 C# 中实现 SOAP 客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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