我想保留 Kephas 提供的 DI 抽象层,但在我的特殊情况下,我需要注册一个从第三方库导入的服务。鉴于此,我无法使用[AppServiceContract]服务注册所需的属性来注释服务。有没有办法做到这一点?
1 回答
1
就在这里。以下是要遵循的步骤:
- 定义一个实现
IConventionsRegistrar. - 使用约定构建器来注册所需的服务。
示例代码:
public class MyConventionsRegistrar : IConventionsRegistrar
{
/// <summary>
/// Registers the conventions.
/// </summary>
/// <param name="builder">The registration builder.</param>
/// <param name="candidateTypes">The candidate types which can take part in the composition.</param>
/// <param name="registrationContext">Context for the registration.</param>
public void RegisterConventions(
IConventionsBuilder builder,
IEnumerable<TypeInfo> candidateTypes,
ICompositionRegistrationContext registrationContext)
{
//... here you can use the conventions builder to register your services using the fluent API. The candidate types are provided if you need the identified application types. A couple of examples are provided below:
// shared/singleton service exported as IMyService with MyService implementation.
builder.ForType(typeof(MyServiceImpl))
.Export(b => b.AsContractType(typeof(IMyService)))
.Shared();
// instance-based multiple services exported as IMultiImplService
builder.ForTypesDerivedFrom(typeof(IMultiImplService))
.Export(b => b.AsContractType(typeof(IMultiImplService)));
}
还有第二种注册服务的方法,但是通过服务描述符而不是流畅的 API。对于这种情况,请执行以下步骤:
- 定义一个实现
IAppServiceInfoProvider. - 返回所需服务的描述符。
与上述注册相同的示例代码:
public class MyAppServiceInfoProvider : IAppServiceInfoProvider
{
/// <summary>
/// Gets an enumeration of application service information objects.
/// </summary>
/// <param name="candidateTypes">The candidate types which can take part in the composition.</param>
/// <param name="registrationContext">Context for the registration.</param>
/// <returns>
/// An enumeration of application service information objects and their associated contract type.
/// </returns>
public IEnumerable<(TypeInfo contractType, IAppServiceInfo appServiceInfo)> GetAppServiceInfos(IEnumerable<TypeInfo> candidateTypes, ICompositionRegistrationContext registrationContext)
{
yield return (typeof(IMyService).GetTypeInfo(),
new AppServiceInfo(typeof(IMyService),
typeof(MyServiceImpl),
AppServiceLifetime.Shared));
yield return (typeof(IMultiImplService).GetTypeInfo(),
new AppServiceInfo(typeof(IMultiImplService),
AppServiceLifetime.Instance,
allowMultiple: true));
}
这两种情况都是自动发现的,并在适当的时候调用注册方法。但是,在第二种情况下,优点是注册可用作服务元数据以供以后查询。
于 2019-03-20T21:59:48.430 回答