使用 WCF 的简单注入器
Simple Injector using WCF
我是 Simple Injector 的新手。
我有一个带有 class Testservice 实现接口 ITestService 的 WCF 服务。根据 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.Web 和 SimpleInjector.Integration.Web.MVC 并且还添加了 WCF 服务参考。
我对在我的 MVC 应用程序的 App Start 中注册 TestService class 以便注入我的控制器感到震惊。容器需要注册为
container.Register(ITestService, <TImplementation>);
但我无法解决或找出我需要在 TImplementation 上提供的内容。它需要一个实现 class,它是 TestService,但 TestService 在 WCF componebt 中可用,只有我在我的 MVC 应用程序中有接口引用。
有人可以指导我的方法是否正确以及上述解决方案。提前致谢。
您可以在注册方法中为 SimpleInjector 提供一个工厂方法,它将用于构建您的对象。
container.Register<ITestService, TestService>(Lifestyle.Scoped);
可以写成
container.Register<ITestService>(() => new TestServiceClient(), Lifestyle.Scoped);
然后您可以自由选择要在工厂方法中使用的构造函数
我是 Simple Injector 的新手。 我有一个带有 class Testservice 实现接口 ITestService 的 WCF 服务。根据 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.Web 和 SimpleInjector.Integration.Web.MVC 并且还添加了 WCF 服务参考。
我对在我的 MVC 应用程序的 App Start 中注册 TestService class 以便注入我的控制器感到震惊。容器需要注册为
container.Register(ITestService, <TImplementation>);
但我无法解决或找出我需要在 TImplementation 上提供的内容。它需要一个实现 class,它是 TestService,但 TestService 在 WCF componebt 中可用,只有我在我的 MVC 应用程序中有接口引用。
有人可以指导我的方法是否正确以及上述解决方案。提前致谢。
您可以在注册方法中为 SimpleInjector 提供一个工厂方法,它将用于构建您的对象。
container.Register<ITestService, TestService>(Lifestyle.Scoped);
可以写成
container.Register<ITestService>(() => new TestServiceClient(), Lifestyle.Scoped);
然后您可以自由选择要在工厂方法中使用的构造函数