解决 Prism Desktop 应用程序中的 Unity 依赖性
根据我在 StackOverFlow 上发布的上一个问题,我的 Prism 桌面应用程序具有以下项目结构。
类库:Application.Common -->这包含我的所有 DTO 和在 WCF 服务层中定义的服务契约,这是一个完全不同的解决方案。
模块: Application.Modules.ServicesModule -- >在这里,我使用“添加服务引用”添加了指向 WCF 实现的链接。我还注册了在 Application.Common 程序集中定义的类型 IMyServiceContract 的实现,因此初始化方法如下所示: -
public void Initialise()
{
_container.RegisterType<IMyService, MyServiceClient>(new InjectionConstructor());
}
最后我有另一个模块 Application.Modules.FunctionalityModule 它的构造函数定义如下
public FunctionalityModule(IMyService myService){}
当应用程序尝试在运行时解决FunctionalityModule中的依赖项时,会发生以下错误
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, AccountsSln.Common.ServiceContract.IMyService, is an interface and cannot be constructed. Are you missing a type mapping?
通常,我看到此错误是因为尚未注册依赖项,但在这种情况下,我知道它在ServicesModule中。这是否与注册在不同的模块中有关?关于如何实现项目结构以支持 Prism 桌面应用程序中的 WCF 服务,还有其他建议吗?
谢谢
亚历克斯
编辑: 由于我想使用通用程序集来定义我的服务合同,因此在使用“添加服务引用”时遇到了问题。事实证明,如果您使用“添加服务引用”,生成的代码将使用元数据来创建客户端类型。它们与公共程序集中的签名具有相同的签名,但有所不同。为了使我能够在通用程序集中使用合约,我从这篇文章 http: //xaml.geek.nz/working-slightly-smarter-with-wcf。这让我朝着正确的方向开始,但我认为我必须考虑使代码更适合生产环境。
following a previous question I posted on StackOverFlow I have the following project structure to my Prism desktop application.
Class Library : Application.Common --> this contains all my DTOs and the service contracts that are defined in my WCF service layer which is an entirely different solution.
Modules: Application.Modules.ServicesModule --> in here I have added links to my WCF implementation using Add Service Reference. I also register the implementations of my types IMyServiceContract is defined in the Application.Common assembly so the initialise method looks as below:-
public void Initialise()
{
_container.RegisterType<IMyService, MyServiceClient>(new InjectionConstructor());
}
Finally I have another module Application.Modules.FunctionalityModule this has a constructor defined as follows
public FunctionalityModule(IMyService myService){}
when the application is trying to resolve the dependency in FunctionalityModule at runtime the following error occurs
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, AccountsSln.Common.ServiceContract.IMyService, is an interface and cannot be constructed. Are you missing a type mapping?
Normally, I have seen this error because a dependency has not been registered but on this occassion I know it has in the ServicesModule. Is this something to do with the registering being in different modules? Are there any other suggestions on how I can implement my project structure to support WCF services in a Prism desktop application?
Thanks
Alex
Edit:
As I wanted to use a Common assembly to define my service contracts I was having problems when using Add Service Reference. It turns out that if you use Add Service Reference the generated code uses the metadata to create client side types. These have the same signature as the ones in the Common assembly but are different. To enable me to use the contracts in the Common assembly I took inspiration from this post http://xaml.geek.nz/working-slightly-smarter-with-wcf. This got me started in the right direction but I think I will have to look into making the code more suited to a production environment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您没有指定从接口到实现类型所需的映射。 (我更喜欢使用UnityContainerExtensions)。
您还需要指定模块依赖项。您的
FunctionalityModule
依赖于ServicesModule
。如何实现这一点取决于ModuleCatalog
的创建方式。所以要么;在 Bootstrapper 的 CreateModuleCatalog 中使用 moduleCatalog.AddModule,或者使用 ModuleAttribute 和 ModuleDependencyAttribute 修饰 IModules > 如果使用
DirectoryModuleCatalog
。当然,UnityContainer可以通过app.config文件进行配置。
The problem is that you have not specified the required mapping from the interface to the implementation type. (I prefer using
UnityContainerExtensions
).You also need to specify module dependencies. Your
FunctionalityModule
is dependent on theServicesModule
. How this is accomplished depends on how theModuleCatalog
is created.So either; in Bootstrapper's
CreateModuleCatalog
usemoduleCatalog.AddModule
, or decorating theIModules
withModuleAttribute
andModuleDependencyAttribute
if using theDirectoryModuleCatalog
.Of course the
UnityContainer
can be configured through the app.config file.生成的服务代理类是部分类。您也许可以通过在服务引用所在的项目中添加如下内容,使它们从 Common 程序集中的
IMyService
继承:The generated service proxy classes are partial classes. You might be able to just make them inherit from the
IMyService
in the Common assembly, by adding something like following in the project where the service references are: