如何使用Structuremap创建Singleton WCF代理

时间:2012-04-25 01:31:38

标签: c# wcf dependency-injection structuremap

  

在WCF中,创建代理是一项繁重的操作,因此如果您遇到性能下降,请务必查看此区域。解决此问题的可能解决方案之一是在应用程序线程中重用代理,实现单例或池

来自Javi's blog on creating WCF proxy as singleton using Castle

。有人可以使用Structuremap提供实现吗?

1 个答案:

答案 0 :(得分:0)

这对于StructureMap来说非常简单,即使你需要做一些测试,这应该可行:

ObjectFactory.Configure(
            x =>
            {
                x.For<GetFilesService.Service1Client>().HybridHttpOrThreadLocalScoped().Use(ctx =>
                    {
                        // Setup logic goes here
                        return new GetFilesService.Service1Client("NetTcpBinding_IService1", "net.tcp://localhost:8089/test");
                    });
            }
        );

这里的秘密是使用线程局部范围(HybridHttpOrThreadLocalScoped

然后解决:

var client = ObjectFactory.GetInstance<GetFilesService.Service1Client>();