WCF手工编写客户端代理

1.服务端

[ServiceContract]
    public interface IService2_1
    {
        /*使用Name方式实现契约的重载。使用Name可以控制契约在WSDL中的名称不至于重复*/
        [OperationContract(Name = "AddInt", Action = "yo/IService2_1/Add", ReplyAction = "yo/IService2_1/AddResponse")]
        int Add(int a, int b);

        [OperationContract(Name = "AddDouble", Action = "yo/IService2_1/AddDouble", ReplyAction = "yo/IService2_1/AddDoubleResponse")]
        double Add(double a, double b);
    }

public class Service2_1 : IService2_1
    {

        #region IService2_1 成员

        public int Add(int a, int b)
        {
            return a + b;
        }

        public double Add(double a, double b)
        {
            return a + b;
        }

        #endregion
    }

2.客户端   注意:这段是必须的,因为要手动生成通信通道

[ServiceContract]
    public interface IService2_1
    {
        [OperationContract(Name = "AddInt")]
        int Add(int a, int b);

        [OperationContract(Name = "AddDouble")]
        int Add(double a, double b);
    }

    public class Service2_1Client : ClientBase<IService2_1>, IService2_1
    {

        #region IService2_1 成员

        public int Add(int a, int b)
        {
            return Channel.Add(a, b);
        }

        public int Add(double a, double b)
        {
            return Channel.Add(a, b);
        }

        #endregion
    }

 

            Binding binding = new WSHttpBinding();
            EndpointAddress address = new EndpointAddress("http://localhost:8732/Design_Time_Addresses/WcfService2_1/Service1/");
            IService2_1 proxy2_1 = ChannelFactory<IService2_1>.CreateChannel(binding, address);
            using (proxy2_1 as IDisposable)
            {
                Console.WriteLine("手动方式创建客户端代理,调用服务计算的值:{0}", proxy2_1.Add(1, 2));
            }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值