WCF从理论到实践(7)
而INormalJob的实现代码如下:
Duplex的交换模式需要现定义Callback的Contract接口,如下:
而服务端的Contract接口为:
Duplex的Contract实现为:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jillzhang.Messaging.Contract;
using System.ServiceModel;
namespace Jillzhang.Messaging.Service
{
[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Multiple)]
public class Job:IJob
{
public string Do(string jobName)
{
try
{
ICallback callback = OperationContext.Current.GetCallbackChannel<ICallback>();
System.Diagnostics.Stopwatch watcher = new System.Diagnostics.Stopwatch();
watcher.Start();
System.Threading.Thread.Sleep(1000);
Console.WriteLine("服务" + AppDomain.CurrentDomain.FriendlyName + "执行任务:" + jobName);
watcher.Stop();
callback.Done((int)watcher.ElapsedMilliseconds);
return "成功";
}
catch
{
return "失败";
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jillzhang.Messaging.Contract;
using System.ServiceModel;
namespace Jillzhang.Messaging.Service
{
[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Multiple)]
public class Job:IJob
{
public string Do(string jobName)
{
try
{
ICallback callback = OperationContext.Current.GetCallbackChannel<ICallback>();
System.Diagnostics.Stopwatch watcher = new System.Diagnostics.Stopwatch();
watcher.Start();
System.Threading.Thread.Sleep(1000);
Console.WriteLine("服务" + AppDomain.CurrentDomain.FriendlyName + "执行任务:" + jobName);
watcher.Stop();
callback.Done((int)watcher.ElapsedMilliseconds);
return "成功";
}
catch
{
return "失败";
}
}
}
}
下面,我们来看一下,如何创建承载服务的应用程序,首先在app.config做如下配置
而Host的代码如下:
而客户端的配置文件,如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="netTcpBinding" />
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:6987/Service/duplex" binding="netTcpBinding"
bindingConfiguration="netTcpBinding" contract="Jillzhang.Messaging.Contract.IJob"
name="NetTcpBinding">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="net.tcp://localhost:6987/Service/oneway" binding="netTcpBinding"
bindingConfiguration="netTcpBinding" contract="Jillzhang.Messaging.Contract.IOneWayJob"
name="NetTcpBinding">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="net.tcp://localhost:6987/Service/normal" binding="netTcpBinding"
bindingConfiguration="netTcpBinding" contract="Jillzhang.Messaging.Contract.INormalJob"
name="NetTcpBinding">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
http://hi.baidu.com/zhiwei%5F117/blog/item/bb09133932fde8c9d5622586.html<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="netTcpBinding" />
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:6987/Service/duplex" binding="netTcpBinding"
bindingConfiguration="netTcpBinding" contract="Jillzhang.Messaging.Contract.IJob"
name="NetTcpBinding">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="net.tcp://localhost:6987/Service/oneway" binding="netTcpBinding"
bindingConfiguration="netTcpBinding" contract="Jillzhang.Messaging.Contract.IOneWayJob"
name="NetTcpBinding">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="net.tcp://localhost:6987/Service/normal" binding="netTcpBinding"
bindingConfiguration="netTcpBinding" contract="Jillzhang.Messaging.Contract.INormalJob"
name="NetTcpBinding">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
加支付宝好友偷能量挖...