zoukankan      html  css  js  c++  java
  • Method Overloading in WCF zt

     

    Method overloading is the process of implementing Polymorphism in Object-Oriented Programming. A method can be overloaded on the basis of type of parameters, number of parameters, and an order of parameters.

    As we know, WCF code is always coded on OOP's based programming language so that it does support method overloading.

    Service Interface

    [ServiceContract]
    public interface ITest
    {
        [OperationContract]
        string TestMethod(int para1,int para2);
        //Initail method
        [OperationContract]
        string TestMethod(string para1, string para2);
        //Overloading on the basis of type of parameters.
        [OperationContract]
        string TestMethod(int para1, string para2);
        //Overloading on the basis of  an order of parameters.
        [OperationContract]
        string TestMethod(int para1, string para2,double para3);
        //Overloading on the basis of the numbers of parameters
    }

    Service implementation

    public class Test : ITest
    {
    
        public string TestMethod(int para1, int para2)
        {
            return "TestMethod1";
        }
    
        public string TestMethod(string para1, string para2)
        {
            return "TestMethod2";
        }
    
        public string TestMethod(int para1, string para2)
        {
            return "TestMethod3";
        }
    
        public string TestMethod(int para1, string para2, double para3)
        {
            return "TestMethod4";
        }
    }

    Issues of method overloading in WCF

    Once test the above code on WCF test client, it will throw an error contract mismatch because of the WSDL that does n't allow to create duplicate methods for clients.

    Error: Cannot obtain Metadata from http://localhost:61489/Test.svc If this is a Windows (R) Communication Foundation 
    service to which you have access, please check that you have enabled metadata publishing at the specified address. 
    For help enabling metadata publishing, 
    please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata
    
    Exchange Error    URI: http://localhost:61489/Test.svc    Metadata contains a reference that cannot 
    be resolved: 'http://localhost:61489/Test.svc'.    The server did not provide a meaningful reply; this might 
    be caused by a contract mismatch, a premature session shutdown or an internal server error.
    
    HTTP GET Error    URI: http://localhost:61489/Test.svc    There was an error downloading 
    'http://localhost:61489/Test.svc'.    The request failed with the error message:--<html>
    <head>        <title>The type 'MethOverWCF.Test', provided as the Service attribute
    value in the ServiceHost directive, or provided in the configuration element 
    system.serviceModel/serviceHostingEnvironment/serviceActivations could not be found

    Solution of method overloading issue in WCF

    By adding unique operationcontract behavior, we can be achieved method overloading in WCF. OperationContract has the Name property that exposes the WCF methods to WSDL Schemas.

    [ServiceContract]
    public interface ITest
    {
        [OperationContract(Name="Method1")]
        string TestMethod(int para1,int para2);
        //Initail method
        [OperationContract(Name = "Method2")]
        string TestMethod(string para1, string para2);
        //Overloading on the basis of type of parameters.
        [OperationContract(Name = "Method3")]
        string TestMethod(int para1, string para2);
        //Overloading on the basis of  an order of parameters.
        [OperationContract(Name = "Method4")]
        string TestMethod(int para1, string para2,double para3);
        //Overloading on the basis of the numbers of parameters
    }

    Creating Client and Consuming overloaded WCF service methods

    In WCF client, all service methods are called by exact same name as define in the OperationContract but method overloaded methods are called by their attribute name. In the given sample code has four different attributes (Method1, Method2, Method3, and Method4) which are exposed by overloaded TestMethod of WCF.

    using WCFConsoleClentApp.MyTest;
    namespace WCFConsoleClentApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                TestClient client = new TestClient();
                string callMethod1 = client.Method1(1, 1);
                string callMethod2 = client.Method2("Test", "Test");
                string callMethod3 = client.Method3(1, "Test");
                string callMethod4 = client.Method4(1, "Test", 3.5);
            }
        }
    }
  • 相关阅读:
    北斗授时系统,GPS授时服务器—在行业应用及介绍
    GPS时间同步服务器,NTP时间同步服务器,——数据采集系统应用
    北斗卫星时钟(北斗授时服务器)厂家介绍及价格分析
    linux系统下ntp网络时钟服务器(NTP服务器)的搭建和使用
    2020年SAP项目艰辛曲折的开工历程 III
    2020年SAP项目艰辛曲折的开工历程 II
    2020年SAP项目艰辛曲折的开工历程 I
    2020年肺炎疫情期间看的几部古装电视剧
    做人不忘本,才能走得更远 --- 我看电视剧《雍正王朝》
    邬先生及时功成身退,是明哲保身的聪明做法 --- 我看电视剧《雍正王朝》
  • 原文地址:https://www.cnblogs.com/zeroone/p/3330566.html
Copyright © 2011-2022 走看看