WCF (Windows Communication Foundation) Create & Implement a WCF Client

创建一个 WCF Client 也可以通过2种方式,直接使用代码 或 使用工具来创建。


(源代码实例下载)


wcf_client_dia



方式 一:直接使用代码

第一步:创建一个简单的 Windows Console 工程。加入一个和WCF Service端一致的接口文件。其中也需要引入 System.Model和使用关键字 [ServiceContract]来描述接口。


using System;
using System.ServiceModel;

[ServiceContract]
public interface IAddTax
{
    [OperationContract]
    decimal AddTax(decimal productCost, double taxRate);
}


第二步:创建ChannelFactory Class,用以创建Channel和Service端进行通信。

        EndpointAddress address = new EndpointAddress("http://localhost:8080/AddTaxService");
        BasicHttpBinding binding = new BasicHttpBinding();

        ChannelFactory<IAddTax> myChannelFactory = new ChannelFactory<IAddTax>(binding, address);

        IAddTax wcfClient = myChannelFactory.CreateChannel();


第三步:创建一些测试性的代码用以和WCF Service 进行通信和测试。

        decimal price = default(decimal);
        decimal total = default(decimal);
        double rate = default(double);

        Console.Write("Enter the product cost: $");
        price = Convert.ToDecimal(Console.ReadLine());
        Console.Write("Enter the tax rate(double): %");
        rate = Convert.ToDouble(Console.ReadLine());

        total = wcfClient.AddTax(price, rate);
        Console.WriteLine("Total price=", total);

        Console.ReadLine();
        myChannelFactory.Close();


使用代码创建一个WCF Client相对简单。


实例代码下载:使用代码实现的 WCF Client Application。



方式 二:使用工具创建WCF Client

通过使用工具主要是创建 Proxy 和 Configuration File。

工具:VS2010下的 Add Service Reference 简称 ASR 工具。

ASR 工具的主要作用是获取 WCF Service 端的metadata来生成 WCF Client端的Proxy和Configuration File(其中存储了 address,binding,和contract)。

ASR 使用svcutil.exe来创建Proxy。这个程序可以和 Metadata Exchange来进行通信,来获取 Service端的接口定义信息。Client端根据这些信息可以知道怎样和Service端进行通信了。


第一步:添加一个ASR。

注意:此时一定要启动 目的 WCF Service。


step_1_dia


asr_cfg_gui



第二步:通过WCF Service的地址我们可以找到WCF Service


find_wcf_service


第三步:系统自动创建了 WCF Client 的配置文件 app.config。

下面的文件是由系统自动创建的,其中包括了和 WCF Service端通信的基本信息。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IAddTax" 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>
        </bindings>
        <client>
            <endpoint address="http://localhost:8080/AddTaxService" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IAddTax" contract="ServiceReference1.IAddTax"
                name="BasicHttpBinding_IAddTax" />
        </client>
    </system.serviceModel>
</configuration>


第四步:



除了使用VS2010图形工具外,我们还可以直接使用 svcutil.exe命令行来创建。


svc_cmd_gui



ASR tool 还支持为ASMX Service创建Client,使用方式与上述方法一致


asmx_svc_dia



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值