简体   繁体   English

Web服务/ WCF新手:针对基于Java的SOAP Web服务创建WCF代理客户端/对象

[英]Web Service/WCF Newbie: Create WCF Proxy Client/Object against Java-based SOAP Web Service

I have a .Net web application that needs to interact with a Java-based system via SOAP. 我有一个.Net Web应用程序需要通过SOAP与基于Java的系统交互。

I have not worked with web services other than some basic WCF and would appreciate any guidance on this. 除了一些基本的WCF之外,我还没有使用Web服务,并且对此有任何指导。

Basically, I was thinking about creating a WCF proxy client to connect to the SOAP web services. 基本上,我正在考虑创建一个WCF代理客户端来连接到SOAP Web服务。 The system is a 3rd party Java-based system that provides a SOAP interface. 该系统是基于Java的第三方系统,提供SOAP接口。

The sample code provided used wsdl to generate the proxy, but isn't that before the times of WCF? 提供的示例代码使用wsdl生成代理,但不是在WCF时代之前?

UsernameToken aToken = new UsernameToken("root", "root", PasswordOption.SendPlainText);

MetadataService.MetadataService aMetadataService = new MetadataService.MetadataService();

SoapContext aContext = aMetadataService.RequestSoapContext;

aContext.Security.Tokens.Add(aToken);

String aXmp = aMetadataService.s_getXmpFromRecordID(wAssetId.Text);

Any suggestions? 有什么建议么? Thank you! 谢谢!

The WSDL is all you need to generate a client proxy for the web service. WSDL是为Web服务生成客户端代理所需的全部内容。 And no, this is not before the times of WCF. 不,这不是在WCF时代之前。 WSDL was designed specifically for this purpose. WSDL专门为此目的而设计。

The simplest solution is to use svcutil to create the code. 最简单的解决方案是使用svcutil来创建代码。

You call svcutil with the WSDL of the web service and you get back the client code along with the configuration for it. 使用Web服务的WSDL调用svcutil,然后返回客户端代码及其配置。

You then call the operations of the web service as methods on the client instance. 然后,您将Web服务的操作作为客户端实例上的方法调用。 It's as simple as that! 就这么简单!

For example, lets consider a basic web service like this one: http://www.startvbdotnet.com/web/sample2.asmx (it's basic math exposed as a web service which I found by google-ing). 例如,让我们考虑一个像这样的基本Web服务: http//www.startvbdotnet.com/web/sample2.asmx (它是基于Google服务我发现的基本数学公式)。

To generate a client for this I would use svcutil like so: 要为此生成客户端,我会像这样使用svcutil:

svcutil http://www.startvbdotnet.com/web/sample2.asmx?wsdl

This command will generate the Sample.cs and output.config files (the code and configuration for the client). 此命令将生成Sample.csoutput.config文件(客户端的代码和配置)。

You add these to your project (along with needed assemblies like System.ServiceModel and System.Runtime.Serialization ) and now you can call the web service with code like this one: 您将这些添加到您的项目(以及所需的程序集,如System.ServiceModelSystem.Runtime.Serialization ),现在您可以使用以下代码调用Web服务:

using (SampleSoapClient proxy = new SampleSoapClient())
{
    Console.WriteLine(proxy.Add(6, 2));
    Console.WriteLine(proxy.Substract(6, 2));
    Console.WriteLine(proxy.Divide(6, 2));
    Console.WriteLine(proxy.Multiply(6, 2));
}

WCF makes things easy. WCF使事情变得简单。 Adding security is also simple with use of proper configuration or attributes on the service classes. 通过在服务类上使用适当的配置或属性,添加安全性也很简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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