一、服务契约(包括回调契约)通过指定不同的OperationContract.Name来实现重载方法,当然代码部份还是必需符合C#的重载要求,即相同方法名称,不同的参数个数或参数类型
1
2
3
4
5
6
7
8
9
10
11
12
|
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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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代码生成。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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的文章内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
// 自定义代理类
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设为相应的服务契约接口
1
2
3
4
5
6
7
8
9
10
11
12
|
<
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
>
|
调用如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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();
}
}
|
这篇文章参考和引用如下作者的文章内容:
文章同步发表于我的个人网站:http://www.zuowenjun.cn/post/2015/03/25/135.html
本文转自 梦在旅途 博客园博客,原文链接:http://www.cnblogs.com/zuowj/p/4366966.html ,如需转载请自行联系原作者