首页 > 解决方案 > 使用 WCF 的简单注入器

问题描述

我是 Simple Injector 的新手。我有一个 WCF 服务,其类Testservice实现了接口ITestService。根据 Simple Injector 文档,在 svc 标记中,我将工厂属性添加为"SimpleInjector.Integration.Wcf.SimpleInjectorServiceHostFactory, SimpleInjector.Integration.Wcf"。同样在 AppStart 中,我使用以下内容注册了容器

container.RegisterWcfServices(Assembly.GetExecutingAssembly()); &
container.Register<ITestService, TestService>(Lifestyle.Scoped);

WCF 工作正常。现在我需要从我的 MVC 应用程序中使用服务 TestService。在我的 MVC 应用程序中,我通过 Nuget 添加了SimpleInjector.Integration.WebSimpleInjector.Integration.Web.MVC,还添加了 WCF 服务引用。

我对在我的 MVC 应用程序的 App Start 中注册TestService类以注入我的控制器感到震惊。容器需要注册为

container.Register(ITestService, <TImplementation>);

但我无法解决或找出我需要在TImplementation上提供什么。它需要一个实现类,它是 TestService,但 TestService 在 WCF 组件中可用,并且只有我的 MVC 应用程序中有接口参考。

有人可以指导我的方法是否正确以及上述解决方案。提前致谢。

标签: asp.net-mvcwcfsimple-injector

解决方案


您可以在用于构建对象的 register 方法中为 SimpleInjector 提供工厂方法。

container.Register<ITestService, TestService>(Lifestyle.Scoped);

可以写成

container.Register<ITestService>(() => new TestServiceClient(), Lifestyle.Scoped);

然后,您可以自由选择要在工厂方法中使用的构造函数


推荐阅读