关于WCF操作重载的个人总结

以下是WCF服务端的服务契约
    [ServiceContract]
    public interface IService1
    {
        [OperationContract(Name= "AddInt")]
        int Add(int arg1, int arg2);
        [OperationContract(Name="AddString")]
        String Add(string arg1, string arg2);
    }

客户端采用VS自带的导入工具,虽然可以使用生成的代理和契约,但还需要进行修改,使客户端代码支持操作重载。方法是将导入的代理和契约的方法名修改为重载的名称,并确保代理能够使用重载方法调用内部代理。

最后,在客户端使用导入契约的Name属性,指定别名并重载方法,使他与导入的操作名保持一致。如下图:

 
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute (ConfigurationName="IService1")]
public interface IService1
{
     
      [System.ServiceModel.OperationContractAttribu te(Name = "AddInt",Action=" http://tempuri.org/IService1/AddInt", ReplyAction=" http://tempuri.org/IService1/AddIntResponse" )]
      int Add(int arg1, int arg2);
     
      [System.ServiceModel.OperationContractAttribu te(Name = "AddString",Action=" http://tempuri.org/IService1/AddString", ReplyAction=" http://tempuri.org/IService1/AddStringResponse")]
      string Add(string arg1, string arg2);
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public interface IService1Channel : IService1, System.ServiceModel.IClientChannel
{
}
[System.Diagnostics.DebuggerStepThroughAttri bute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public partial class Service1Client : System.ServiceModel.ClientBase, IService1
{
     
      public Service1Client()
      {
      }
     
      public Service1Client(string endpointConfigurationNam e) :
                      base(endpointConfigurationNam e)
      {
      }
     
      public Service1Client(string endpointConfigurationNam e, string remoteAddress) :
                      base(endpointConfigurationNam e, remoteAddress)
      {
      }
     
      public Service1Client(string endpointConfigurationNam e, System.ServiceModel.EndpointAddress remoteAddress) :
                      base(endpointConfigurationNam e, remoteAddress)
      {
      }
     
      public Service1Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
                      base(binding, remoteAddress)
      {
      }
     
    public int Add(int arg1, int arg2)
    {
        return base.Channel.Add(arg1, arg2);
    }
   
    public string Add(string arg1, string arg2)
    {
        return base.Channel.Add(arg1, arg2);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值