asp.net-mvc - 使用 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.Web和SimpleInjector.Integration.Web.MVC,还添加了 WCF 服务引用。
我对在我的 MVC 应用程序的 App Start 中注册TestService类以注入我的控制器感到震惊。容器需要注册为
container.Register(ITestService, <TImplementation>);
但我无法解决或找出我需要在TImplementation上提供什么。它需要一个实现类,它是 TestService,但 TestService 在 WCF 组件中可用,并且只有我的 MVC 应用程序中有接口参考。
有人可以指导我的方法是否正确以及上述解决方案。提前致谢。
解决方案
您可以在用于构建对象的 register 方法中为 SimpleInjector 提供工厂方法。
container.Register<ITestService, TestService>(Lifestyle.Scoped);
可以写成
container.Register<ITestService>(() => new TestServiceClient(), Lifestyle.Scoped);
然后,您可以自由选择要在工厂方法中使用的构造函数
推荐阅读
- xml - 尝试构建多个变量的字符串时出现不需要的换行符
- java - 箭头的事件队列含义
- android - 如何删除添加到 FragmentManager 的第一个片段并启动新的 BackStack?
- python - 使用 python3、漂亮的汤和硒(也许)对 howlongtobeat.com 进行数据抓取
- python - 在表单中使用 MultipleChoiceField 作为 Integerfield
- xamarin.forms - Xamarin Forms 主题源
- python - 如何使用 tensorflow 编写这个函数?
- rest - 如果在发布子资源时资源不存在,则在 REST API 中使用哪个 HTTP 状态代码?
- php - PHP 会话变量在重定向时不可用
- amazon-s3 - 如何恢复在二进制文件中复制的 S3 的本地备份