您的位置:首页 > 其它

WCF 学习总结3 -- 实例模式

2016-03-25 12:24 330 查看
通过WCF的ServiceBehaviorAttribute设定InstanceContextMode有下面的3中模式:
1. Single —— 表示所有的客户端共享一个会话(服务对象)(服务关闭时才会销毁服务对象)
2. PerCall —— 表示每次调用都会创建一个会话(服务对象)(调用完毕后就会销毁服务对象)
3. PerSession —— 表示为每个连接(每个客户端代理对象) 创建一个会话(服务对象),只有指定IsTerminating=true的操作被调用,或者是设定的SessionTimeout超时的时候,服务对象会被销毁。但支持Session的Binding只有:WSHttpBinding、WSDualHttpBinding、WSFederationHttpBinding、NetTcpBinding。

测试一下上述行为,设计3个Service,每个Service都在构造函数中输出内容并实现IDispose接口,在Dispose()里输出内容。
(准备) Host实现代码:


[c-sharp] view plain copy

static void Main(string[] args)

{

ServiceHost host1 = new ServiceHost(typeof(SingleService));

host1.Open();

Console.WriteLine("SingleService Opened!");

ServiceHost host2 = new ServiceHost(typeof(PeerCallService));

host2.Open();

Console.WriteLine("PeerCallService Opened!");

ServiceHost host3 = new ServiceHost(typeof(PeerSessionService));

host3.Open();

Console.WriteLine("PeerSessionService Opened!");

Console.Read();

}

(一) Single服务契约——服务端启动时实例化

[c-sharp] view plain copy

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]

public class SingleService : IService1, IDisposable

{

public SingleService()

{

Console.WriteLine("SingleService ({0}) [{1:mm:ss.fff}] ctor", this.GetHashCode(), DateTime.Now);

}

public string GetData(string value)

{

return string.Format("SingleService ({0}) GetData [{1}]", this.GetHashCode(), value);

}

public void Dispose()

{

Console.WriteLine("SingleService ({0}) [{1:mm:ss.fff}] dispose", this.GetHashCode(), DateTime.Now);

}

}


启动Host调用SingleService.GetData,服务端的输出:



上图说明SingleService在服务启动时就创建了。

(二) PerCall服务契约——每次调用都重新实例化


[c-sharp] view plain copy

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]

public class PeerCallService : IService1, IDisposable

{

public PeerCallService()

{

Console.WriteLine("PeerCallService ({0}) [{1:mm:ss.fff}] ctor", this.GetHashCode(), DateTime.Now);

}

public string GetData(string value)

{

return string.Format("PeerCallService ({0}) GetData [{1}]", this.GetHashCode(), value);

}

public void Dispose()

{

Console.WriteLine("PeerCallService ({0}) [{1:mm:ss.fff}] dispose", this.GetHashCode(), DateTime.Now);

}

}


调用PeerCallService.GetData




(三) PerSession服务契约——客户端第一次调用时实例化

[c-sharp] view plain copy

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]

public class PeerSessionService : IService1, IDisposable

{

public PeerSessionService()

{

Console.WriteLine("PeerSessionService ({0}) [{1:mm:ss.fff}] ctor", this.GetHashCode(), DateTime.Now);

}

public string GetData(string value)

{

return string.Format("PeerSessionService ({0}) GetData [{1}]", this.GetHashCode(), value);

}

public void Dispose()

{

Console.WriteLine("PeerSessionService ({0}) [{1:mm:ss.fff}] dispose", this.GetHashCode(), DateTime.Now);

}

}

服务端的Session的Timeout是由binding属性receiveTimeout和inactivityTimeout控制的。


[b][b][xhtml] view plain copy[/b][/b]

<bindings>

<wsHttpBinding>

<binding name="wsHttp" receiveTimeout="00:00:15">

<reliableSession enabled="true" inactivityTimeout="00:00:15" />

</binding>

</wsHttpBinding>

</bindings>

关于这两个属性的配置,更详细的说明请参考MSDN,按照配置服务对象在客户端没有调用后15秒销毁:





需要注意的是,每次 new WcfSvcPeerSession.Service1Client() 都是一个新的Session。


接下来看看怎么通过 OperationContact 的 IsInitiating 和 IsTerminating 来控制Session。IsInitiating 表示该方法是否可以初始化 Session,IsTerminating 表示该方法是否可以终止 Session。默认设置 IsInitiating=true,IsTerminating=false。另外通过IsInitiating和IsTerminating 控制的时候,必须设置 ServiceContract 的 SessionMode.Required
服务契约的定义

[c-sharp] view plain copy

[ServiceContract(SessionMode = SessionMode.Required)]

interface IService2

{

[OperationContract(IsInitiating=true)]

void LogIn();

[OperationContract]

void DoSth();

[OperationContract(IsTerminating=true)]

void LogOff();

}

[b]服务契约的实现:
[/b]

[c-sharp] view plain copy

public class PeerSessionService2 : IService2

{

public void LogIn()

{

var sessionId = OperationContext.Current.SessionId;

Console.WriteLine("{0} LogIn.", sessionId);

}

public void DoSth()

{

var sessionId = OperationContext.Current.SessionId;

Console.WriteLine("{0} DoSth.", sessionId);

}

public void LogOff()

{

var sessionId = OperationContext.Current.SessionId;

Console.WriteLine("{0} LogOff.", sessionId);

}

}

一旦LogOff即IsTerminating,Session就结束了。再次调用任何服务端方法都会引发异常。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: