ChannelFactory Reuse Strategies

雨燕双飞 提交于 2019-12-10 18:54:21

问题


I've been reading that ChannelFactory creation is expensive and that unless there is a technical reason not to, one should reuse ChannelFactories when possible either by caching them someway, or by using static instances of the factories.

In your experience, what ChannelFactory reuse strategies have you found to be the most useful and robust within the context of an ASP.NET application?


回答1:


If you're using .NET 3.0 SP1 and up and don't need special stuff that requires handling channels directly, then the best option would be to just use client-side proxy classes derived from ClientBase<T> (like the ones generated when importing services). Those already cache the factory underneath. See here for the details.

If not, then yeah, you'll need to stick the IChannelFactory<T> object somewhere, but you need to still make sure you handle sharing appropriately (I don't think there's any guarantees made by the stack that access of a factory is thread-safe), but other than that, it should be fairly straight-forward.




回答2:


Darin Damitrov posted a useful answer around re-use of channelfactories here:-

creating WCF ChannelFactory<T>

I've been looking at memory usage and performance in an application I've been working on recently. I applied the technique he advocated (using a dictionary to store a set of channelfactories) and had a good performance bump with it.

What I was seeing was that instantiating a channelfactory could take up to 70ms. If this is happening a lot it adds up quickly.

Currently, I'm being cautious and have not set up my DI container to serve up the same instances of the channelfactories for the lifetime of the application. Instead, I make them live for the duration of a single HTTP request (within which there may be many back-end service calls made).



来源:https://stackoverflow.com/questions/870600/channelfactory-reuse-strategies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!