简体   繁体   English

使用WCF转换旧代码以连接到Web服务

[英]converting old code to connect to web-service using WCF

I have a ASMX client application that i am trying to convert to using WCF. 我有一个ASMX客户端应用程序,我试图将其转换为使用WCF。 So i added a service reference, and it gave me a SoapClient class. 所以我添加了一个服务引用,它给了我一个SoapClient类。 My old code was using a class derived from System.Web.Services.Protocols.SoapHttpClientProtoco l and it had two properites. 我的旧代码使用的是派生自System.Web.Services.Protocols.SoapHttpClientProtoco的类,它有两个属性。 Url and ClientCertificates . 网址和ClientCertificates Are there any equivalent of these in this SoaClient class that I get in WCF world ? 在WCF世界中,在此SoaClient类中是否有这些等效项?

The equivalent to the Url property is the endpoint address which you can set through the the constructor of your proxy object, or through the configuration (use the WCF Service Configuration Editor to edit the config). 与Url属性等效的是端点地址,您可以通过代理对象的构造函数或通过配置(使用WCF服务配置编辑器来编辑配置)进行设置。 For ClientCertificates, use the ClientCredentials property of your proxy object. 对于ClientCertificates,请使用代理对象的ClientCredentials属性。

Here is some sample code which will get you started. 这是一些示例代码,可以帮助您入门。

BasicHttpBinding basicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
System.ServiceModel.Channels.Binding binding = basicHttpBinding;
MyServiceSoapClient myService =
    new MyServiceSoapClient(binding, new EndpointAddress(url));
myService.ClientCredentials.ClientCertificate.Certificate = new X509Certificate2(Certificate);

All of this can also be done through configuration rather than code, using the WCF Service Configuration Editor to set up the config file; 使用WCF服务配置编辑器来设置配置文件,还可以通过配置而不是代码来完成所有这些操作。 this makes it much easier to reconfigure the service (does not require code changes). 这样可以更轻松地重新配置服务(不需要更改代码)。

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

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