在WCF实例上下文模式中,PreCal模式是一个比较重要的模式。我们在这篇文章中将会针对WCF PreCal模式的相关概念及应用技巧做一个详细的阐述,希望朋友们能从中获得一些帮助。
在WCF PreCal模式下,即便使用同一个代理对象,也会为每次调用创建一个服务实例。调用结束后,服务实例被立即释放(非垃圾回收)。对于不支持 Session 的 Binding,如 BasicHttpBinding,其缺省行为就是 PreCall。
[ServiceContract]
public interface IMyService
{
[OperationContract]
void Test();
}
[ServiceBehavior(InstanceContextModeInstanceContextMode =
InstanceContextMode.PerCall)]
public class MyServie : IMyService, IDisposable
{
public MyServie()
{
Console.WriteLine("Constructor:{0}", this.GetHashCode());
}
[OperationBehavior]
public void Test()
{
Console.WriteLine("Test:{0}", OperationContext.Current.SessionId);
}
public void Dispose()
{
Console.WriteLine("Dispose");
}
}
public class WcfTest
{
public static void Test()
{
AppDomain.CreateDomain("Server").DoCallBack(delegate
{
ServiceHost host = new ServiceHost(typeof(MyServie),
new Uri("http://localhost:8080/MyService"));
host.AddServiceEndpoint(typeof(IMyService), new WSHttpBinding(), "");
host.Open();
});
//-----------------------
IMyService channel = ChannelFactory<IMyService>.
CreateChannel(new WSHttpBinding(),
new EndpointAddress("http://localhost:8080/MyService"));
using (channel as IDisposable)
{
channel.Test();
channel.Test();
}
}
}
- 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.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
输出:
Constructor:30136159
Test:urn:uuid:df549447-52ba-4c54-9432-31a7a533d9b4
Dispose
Constructor:41153804
Test:urn:uuid:df549447-52ba-4c54-9432-31a7a533d9b4
Dispose
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
以上就是我们为大家介绍的WCF PreCal模式的相关介绍。
【编辑推荐】