不能在Kephas的开放式通用服务中使用构造函数 [英] Cannot use constructors in open generic services with Kephas

查看:60
本文介绍了不能在Kephas的开放式通用服务中使用构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以某种方式,我无法使其具有通过构造函数实现的开放通用导出的服务而工作。我试图将[CompositionConstructor]属性添加到它毫无用处。它会抛出类似这样的内容:

Somehow I cannot get to make it work having a service exported as open generic with an implementation with a constructor. I tried to add the [CompositionConstructor] attribute to it to no avail. It throws something like:

System.Composition.Hosting.CompositionFailedException : No importing constructor was found on type 'MyType'.

有什么想法吗?

推荐答案

不幸的是,这是System.Composition库中的错误(请在此处检查 https ://github.com/dotnet/corefx/issues/40094 )。如我所见,此错误已添加到5.0里程碑中,意味着明年秋天。从我的角度来看,您有以下选择:

Unfortunately this is a bug in the System.Composition library (check here https://github.com/dotnet/corefx/issues/40094). As I can see, this bug is added to the 5.0 milestone, meaning next year in autumn. From my perspective you have the following options:


  1. 仅将实现类保留为默认构造函数。在这里,您可以使用AmbientServices.Instance.CompositionContainer.GetExport来获取所需的依赖项。我知道这不是最好的解决方案,但应该可以。警告:如果将服务标记为作用域,则如果您依赖其他作用域服务,则将无法获得正确的作用域。

  1. Leave the implementation class only with the default constructor. Here you can use AmbientServices.Instance.CompositionContainer.GetExport to get the dependencies you need. I know it is not the best solution, but it should work. Warning: if the service is marked as scoped, than if you depend on other scoped services you will not get the right scope for them.

Kephas 6.5版。 0带来了Kephas合成的Autofac实现,其中的事物按预期方式工作。在 https://github.com/kephas-software/kephas/releases获取最新版本并构建所需的nuget。我将尝试尽快在nuget上发布beta版本。

Kephas version 6.5.0 brings an Autofac implementation for Kephas composition, where the things work as they suppose to be. Check the sources at https://github.com/kephas-software/kephas/releases to get the latest version and build the nugets you need. I will try to publish a beta version on nuget soon.

为您使用的版本带来自己的组合实现,并选择DI容器。您可以将Autofac实现作为参考,尽管最新版本中有一些重大更改。选择DI容器时,请确保它支持开放的泛型,元数据以及支持IExportFactory接口的方法。

Bring your own implementation of composition for the version you use, with your DI container of choice. You could take as reference the Autofac implementation, although there are a couple of breaking changes in the latest version. When choosing a DI container, make sure it supports open generics, metadata, and a way to support the IExportFactory interfaces.

这篇关于不能在Kephas的开放式通用服务中使用构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆