Integrating WCF Clients with ASMX Services

本文介绍如何使用WCF客户端与ASMX服务进行整合,包括创建ASMX服务、生成客户端代理和配置文件的过程,以及如何利用SvcUtil工具指定使用XmlSerializer生成配置。

Ref: http://nayyeri.net/blog/integrating-wcf-clients-with-asmx-services/

Back in December 2006 I linked to Thom Robbins' blog post about integrating WCF with ASMX services.  The other side of integration between WCF and ASMX services is integration between WCF clients and ASMX services which is the title of this post.

First I create an ASMX service to build my WCF client for it.

using System;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class Service : System.Web.Services.WebService

{

public Service()

    {

    }

    [WebMethod]

public int Method1(int x, int y)

    {

return ((x + y) * 2);

    }

    [WebMethod]

public int Method2(int x, int y)

    {

return ((x - y) * 2);

    }

}

At this point I deploy my ASMX service to IIS and test it.

ASMX Service

Now my service is ready to generate client proxy and configuration files.  Before generating these files, keep in mind that ASMX services use BasicHttpBinding that we can use in our clients easily.  In order to generate proxy and configuration files, you can use SvcUtil command tool with a switch that specifies the usage of XmlSerializer.  This switch is /serializer:XmlSerializer.  Therefore my command should look like this:

svcutil /serializer:XmlSerializer http://localhost/ASMXService/service.asmx

This command generates a proxy class as well as a configuration file for my client.  You can see the generated configuration file below.

<?xml version="1.0" encoding="utf-8"?>

<configuration>

    <system.serviceModel>

        <bindings>

            <basicHttpBinding>

                <binding name="ServiceSoap" closeTimeout="00:01:00" openTimeout="00:01:00"

receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"

bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"

maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"

messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"

useDefaultWebProxy="true">

                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"

maxBytesPerRead="4096" maxNameTableCharCount="16384" />

                    <security mode="None">

                        <transport clientCredentialType="None" proxyCredentialType="None"

realm="" />

                        <message clientCredentialType="UserName" algorithmSuite="Default" />

                    </security>

                </binding>

            </basicHttpBinding>

            <customBinding>

                <binding name="ServiceSoap12">

                    <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"

messageVersion="Soap12" writeEncoding="utf-8">

                        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"

maxBytesPerRead="4096" maxNameTableCharCount="16384" />

                    </textMessageEncoding>

                    <httpTransport manualAddressing="false" maxBufferPoolSize="524288"

maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous"

bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"

keepAliveEnabled="true" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous"

realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"

useDefaultWebProxy="true" />

                </binding>

            </customBinding>

        </bindings>

        <client>

            <endpoint address="http://localhost/ASMXService/service.asmx"

binding="basicHttpBinding" bindingConfiguration="ServiceSoap"

contract="ServiceSoap" name="ServiceSoap" />

            <endpoint address="http://localhost/ASMXService/service.asmx"

binding="customBinding" bindingConfiguration="ServiceSoap12"

contract="ServiceSoap" name="ServiceSoap12" />

        </client>

    </system.serviceModel>

</configuration>

Here I write my code logic for the Console Application that I'm building as my WCF client.  There is no difference between my client code with other WCF clients but there is an important point here: as you can see in above configuration code, there are two bindings available for my client so I must specify the name of binding section when I create an instance of ServiceSoapClient.  Here I use ServiceSoap binding section.

static void Main(string[] args)

{

Console.Title = "Integrating WCF Clients with ASMX Services";

Console.WriteLine("Enter x:");

int x = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter y:");

int y = Convert.ToInt32(Console.ReadLine());

using (ServiceSoapClient proxy = new ServiceSoapClient("ServiceSoap"))

    {

Console.WriteLine("(({0} + {1}) * 2) = {2}", x, y,

            proxy.Method1(x, y).ToString());

Console.WriteLine("(({0} - {1}) * 2) = {2}", x, y,

            proxy.Method2(x, y).ToString());

    }

Console.ReadLine();

}

Output

Now playing: Pink Floyd - What Do You Want From Me

转载于:https://www.cnblogs.com/javafun/archive/2008/05/04/1181881.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值