简体   繁体   English

WCF服务代理生存期的最佳实践?

[英]Best Practice for WCF Service Proxy lifetime?

When working with WCF services, is it better to create a new instance of the service every time you use it? 使用WCF服务时,每次使用它时是否更好地创建服务的新实例? Or is it better to create one and re-use it? 或者创建一个并重新使用它会更好吗? Why is either approach better? 为什么两种方法都更好? Is it the same for asynchronous proxies? 异步代理是否相同?

Or is it better to create one and re-use it? 或者创建一个并重新使用它会更好吗?

Do not start to implement your own pooling implementation. 不要开始实现自己的池实现。 That has already been done in the framework. 这已经在框架中完成了。 A WCF proxy uses cached channels factories underneath. WCF代理使用下面的缓存通道工厂。 Therefore, creating new proxies is not overly expensive (but see Guy Starbuck's reply regarding sessions and security!). 因此,创建新代理并不是太昂贵(但请参阅Guy Starbuck关于会话和安全性的回复!)。

Also be aware that a proxy times out after a certain idle time (10mins by default). 还要注意,代理在一定的空闲时间(默认为10分钟)后超时。

If you want more explicit control you might consider using ChannelFactories and channels directly instead of the "easy to go, full out of the box" ClientBase proxies. 如果你想要更明确的控制,你可以考虑直接使用ChannelFactories和渠道而不是“易于使用,完全开箱即用”的ClientBase代理。

http://msdn.microsoft.com/en-us/library/ms734681.aspx http://msdn.microsoft.com/en-us/library/ms734681.aspx

And a "must read" regarding this topic is: http://blogs.msdn.com/wenlong/archive/2007/10/27/performance-improvement-of-wcf-client-proxy-creation-and-best-practices.aspx 关于这个主题的“必读”是: http//blogs.msdn.com/wenlong/archive/2007/10/27/performance-improvement-of-wcf-client-proxy-creation-and-best-practices的.aspx

One more point to consider is channel faults. 还有一点需要考虑的是通道故障。 By design WCF does not allow to use client proxy after unhandled exception happened. 根据设计,WCF不允许在未处理的异常发生后使用客户端代理。

IMyContract proxy = new MyContractClient( );
try
{
   proxy.MyMethod( );
}
catch
{}

//Throws CommunicationObjectFaultedException
proxy.MyMethod( );

in addition to the things Guy Starbuck mentioned a key factor would be the security model you're using (in conjunction with the session requirements) - if you don't re-use your proxy, you can't re-use a security sessions. 除了Guy Starbuck提到的一个关键因素,你要使用的安全模型(结合会话要求) - 如果你不重用你的代理,就不能重新使用安全会话。

This means that the client would have to authenticate itself with each call which is wasteful. 这意味着客户端必须通过每次调用进行身份验证,这是浪费的。

If, however, you decide this is what you wish to do, make sure to configure the client to not establish a security context (as you will never use it), this will save you a couple of roundtrips to the server :-) 但是,如果您认为这是您想要做的,请确保将客户端配置为不建立安全上下文(因为您永远不会使用它),这将为您节省几次往返服务器:-)

There is a corollary here to Server Activated Objects in .NET Remoting (one of the technologies that is replaced by WCF), which have two modes, "Single Call" (stateless) and "Singleton" (stateful). 这里有一个推论,即.NET Remoting中的服务器激活对象(被WCF取代的技术之一),它有两种模式,“单一呼叫”(无状态)和“单一”(有状态)。

The approach you take in WCF should be based on your performance and scaling requirements in conjunction with the needs of your consumers, as well as server-side design constraints. 您在WCF中采用的方法应该基于您的性能和扩展要求以及消费者的需求以及服务器端设计约束。

If you have to maintain state between calls to the service, then you will obviously want to have a stateful instance, but if you don't you should probably implement it so that it is static, which should scale better (you can more easily load balance, etc). 如果你必须维持对服务的调用之间的状态,那么你显然希望有一个有状态的实例,但如果你不这样做,你应该实现它以便它是静态的,它应该更好地扩展(你可以更容易地加载平衡等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM