WCF-方法重载

一、服务端重载

  一般写法直接重载,但是会报错,如下。

[ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        string GetData(string value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: 在此添加您的服务操作
    }

  修改后如下

ServiceContract]
    public interface IService1
    {

        [OperationContract(Name ="GetDataInt")]
        string GetData(int value);

        [OperationContract(Name ="GetDataString")]
        string GetData(string value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: 在此添加您的服务操作
    }

 发现可以了

自动生成代理,方法名字和方法契约名字一样了。

[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService1/GetDataInt", ReplyAction="http://tempuri.org/IService1/GetDataIntResponse")]
        string GetDataInt(int value);
        
        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService1/GetDataInt", ReplyAction="http://tempuri.org/IService1/GetDataIntResponse")]
        System.Threading.Tasks.Task<string> GetDataIntAsync(int value);
        
        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService1/GetDataString", ReplyAction="http://tempuri.org/IService1/GetDataStringResponse")]
        string GetDataString(string value);
        
        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService1/GetDataString", ReplyAction="http://tempuri.org/IService1/GetDataStringResponse")]
        System.Threading.Tasks.Task<string> GetDataStringAsync(string value);

 

二、客户端方法重载

代理类中方法,进行修改

修改前(代码中可以new一个服务实例,然后把方法打出来,F12过去)

 public string GetDataInt(int value) {
            return base.Channel.GetDataInt(value);
        }
        
        public System.Threading.Tasks.Task<string> GetDataIntAsync(int value) {
            return base.Channel.GetDataIntAsync(value);
        }
        
        public string GetDataString(string value) {
            return base.Channel.GetDataString(value);
        }

修改后

 

 public string GetData(int value) {
            return base.Channel.GetDataInt(value);
        }
        
        public System.Threading.Tasks.Task<string> GetDataIntAsync(int value) {
            return base.Channel.GetDataIntAsync(value);
        }
        
        public string GetData(string value) {
            return base.Channel.GetDataString(value);
        }

 

 

 

转载于:https://www.cnblogs.com/wudequn/p/7081143.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值