• Operation Overloading in WCF


    1. Is operation overloading valid in WCF?
        overloading means that two methods with the same name but with different parameters. e.g., the following is a valid C# interface definiton:
        interface ICaculator
        {
            int Add(int x,int y);
            double Add(double x,double y);
        }
            However, operation overloading is not valid if we simplely define such following operationcontract in WCF, it will throw an InvalidOperationException.
        interface ICaculator
        {
         int Add(int x,int y);
         double Add(double x,double y);
        //this is not allowed!
        }
    2 How can we overload operation in WCF manually?
            However, we can manualy enable operation overloading. The trick is using the Name property of OperationContract attribute to alias operation:
        [ServiceContract]
        interface ICaculator
        {
            [OperationContract(Name="AddInt")]
            int Add(int x,int y);
            [OperationContract(Name="AddDouble")]
            double Add(double x,double y);
        }
       -----------------------------------------------------------------------------------------------------------------------------------
        when the client imports the contracts and generates the proxy automatically, the imported operations will have aliased names:
        [ServiceContract]
        public interface ICalculator
        {
        [OperatonContract]
        int AddInt(int x,int y);
        [OperatonContract]
        double AddDouble(double x,double y);
        }
        public partial class CalCulatorClient:ClientBase<ICaculator>,ICaculator
        {
            public int AddInt(int x,int y)
            {
                return Channel.AddInt(x,y);
            }
             
            public double AddDouble(double x,double  y)
            {
                return Channel.AddDouble(x,y);    
            }
        }
        The client can use the generated proxy and contract as is, However, we can still rework those to realize operation overloading on the client side. and the trick is the same as we did on the server side using Name attribute.
        we can re-code like this.
        public interface ICalculator
        {
            [OperationContract(Name="AddInt")]
            int Add(int x,int y);
            [OperationContract(Name="AddDouble")]
            double Add(double x,double y);
        }
         public partial class CalCulatorClient:ClientBase<ICaculator>,ICaculator
        {
            public int Add(int x,int y)
            {
                return Channel.AddInt(x,y);
            }
             
            public double Add(double x,double  y)
            {
                return Channel.AddDouble(x,y);    
            }
        }

    and then, in the client side, we can consume the services.
        CalCulatorClient cal=new CalCulator();
        cal.Add(1,2);//equals 3
        cal.Add(1.1,2.3);//equals 3.4




  • 相关阅读:
    哈夫曼编码
    20182330《程序设计与设计结构》 第九周学习总结
    20182330 2019-2020-1 《数据结构与面向对象程序设计》实验七报告
    20182330 2019-2020-1 《数据结构与面向对象程序设计》实验八报告
    20182330《程序设计与设计结构》 第八周学习总结
    20182330《程序设计与设计结构》 第七周学习总结
    20182330 2019-2020-1 《数据结构与面向对象程序设计》实验六报告
    20182326 2018-2019-1《程序设计与数据结构》课程总结
    20182326 2019-2020-1 《数据结构与面向对象程序设计》实验九报告
    团队作业——学习心得
  • 原文地址:https://www.cnblogs.com/Winston/p/1158380.html
Copyright © 2020-2023  润新知