天天看點

WCF實作方法重載

一、服務契約(包括回調契約)通過指定不同的OperationContract.Name來實作重載方法,當然代碼部份還是必需符合C#的重載要求,即相同方法名稱,不同的參數個數或參數類型 namespace Contract { [ServiceContract(Name = "HellworldService", Namespace = "http://www.zuowenjun.cn")] public interface IHelloWorld { [OperationContract(Name = "GetHelloWorldWithoutParam")] string GetHelloWorld(); [OperationContract(Name = "GetHelloWorldWithParam")] string GetHelloWorld(string name); }

一、服務契約(包括回調契約)通過指定不同的OperationContract.Name來實作重載方法,當然代碼部份還是必需符合C#的重載要求,即相同方法名稱,不同的參數個數或參數類型

namespace Contract
{
    [ServiceContract(Name = "HellworldService", Namespace = "http://www.zuowenjun.cn")]
    public interface IHelloWorld
    {
        [OperationContract(Name = "GetHelloWorldWithoutParam")]
        string GetHelloWorld();

        [OperationContract(Name = "GetHelloWorldWithParam")]
        string GetHelloWorld(string name);           
    }
}

      

二、建立服務寄宿程式與不含重載方法的WCF服務相同,在此不再重述。

三、在用戶端調用WCF服務

A.若直接通過引用WCF服務的方法生成的相關服務類,則使用方法如下(說明:服務方法名稱不再是我們之前定義的方法名,而是OperationContract.Name)

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            using (HellworldServiceClient helloWorldProxy = new HellworldServiceClient())
            {
                Console.WriteLine("服務傳回的結果是: {0}", helloWorldProxy.GetHelloWorldWithoutParam());
                Console.WriteLine("服務傳回的結果是: {0}", helloWorldProxy.GetHelloWorldWithParam("Zuowenjun"));
            }

            Console.ReadLine();
        }
    }
}

      

B.若想保持與服務契約定義的方法名稱相同,做到真正意義上的方法重載,則可自己實作用戶端代理類,而不是通過引用後由VS代碼生成。

using Contract;
using System.ServiceModel;
namespace Client
{
    class HellworldServiceClient : ClientBase<IHelloWorld>, IHelloWorld
    {
        #region IHelloWorld Members
        public string GetHelloWorld()
        {
            return this.Channel.GetHelloWorld();
        }

        public string GetHelloWorld(string name)
        {
            return this.Channel.GetHelloWorld(name);
        }
        #endregion 
    }
}
      

注意:IHelloWorld服務契約接口必須先自己重新定義或引用之前服務契約接口類庫,建議将服務契約接口單獨放在一個類庫,這樣就可以減少重寫接口的時間。

同理,若想保持服務契約接口的繼承,也需要在用戶端重新定義服務契約及用戶端服務代理類,這裡直接引用Learning hard的文章内容:

// 自定義代理類
    public class SimpleInstrumentationClient : ClientBase<ICompleteInstrumentation>, ISimpleInstrumentation
    {
        #region ISimpleInstrumentation Members
        public string WriteEventLog() 
        {
            return this.Channel.WriteEventLog();
        }
        #endregion 
    }

 public class CompleteInstrumentationClient:SimpleInstrumentationClient, ICompleteInstrumentation
    {
        public string IncreatePerformanceCounter()
        {
            return this.Channel.IncreatePerformanceCounter();
        }
    }

//調用服務
class Program
    {
        static void Main(string[] args)
        {
            using (SimpleInstrumentationClient proxy1 = new SimpleInstrumentationClient())
            {
                Console.WriteLine(proxy1.WriteEventLog());
            }
            using (CompleteInstrumentationClient proxy2 = new CompleteInstrumentationClient())
            {
                Console.WriteLine(proxy2.IncreatePerformanceCounter());
            }
      
            Console.Read();
        }
    }

      

當然也可以通過配置不同的endpoint終結點的,contract設為相應的服務契約接口

<configuration>
    <system.serviceModel>
        <client>
            <endpoint address="http://localhost:9003/instrumentationService/mex"
                binding="mexHttpBinding" contract="ClientConsoleApp.ISimpleInstrumentation"
                name="ISimpleInstrumentation" />
            <endpoint address="http://localhost:9003/instrumentationService/mex"
                binding="mexHttpBinding" contract="ClientConsoleApp.ICompleteInstrumentation"
                name="ICompleteInstrumentation" />
        </client>
    </system.serviceModel>
</configuration>

      

調用如下:

class Program
    {
        static void Main(string[] args)
        {
            using (ChannelFactory<ISimpleInstrumentation> simpleChannelFactory = new ChannelFactory<ISimpleInstrumentation>("ISimpleInstrumentation"))
            {
                ISimpleInstrumentation simpleProxy = simpleChannelFactory.CreateChannel();
                using (simpleProxy as IDisposable)
                {
                    Console.WriteLine(simpleProxy.WriteEventLog());
                }
            }
            using (ChannelFactory<ICompleteInstrumentation> completeChannelFactor = new ChannelFactory<ICompleteInstrumentation>("ICompleteInstrumentation"))
            {
                ICompleteInstrumentation completeProxy = completeChannelFactor.CreateChannel();
                using (completeProxy as IDisposable)
                {
                    Console.WriteLine(completeProxy.IncreatePerformanceCounter());
                }
            }
            
            Console.Read();
        }
    }

      

這篇文章參考和引用如下作者的文章内容:

跟我一起學WCF(5)——深入解析服務契約[上篇]

跟我一起學WCF(6)——深入解析服務契約[下篇]

文章同步發表于我的個人網站:http://www.zuowenjun.cn/post/2015/03/25/135.html