WCF实现方法重载

时间:2024-01-15 18:02:14

一、服务契约(包括回调契约)通过指定不同的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