Да, есть.Вот следующие шаги:
- Определите класс, реализующий
IConventionsRegistrar
. - Используйте конструктор соглашений для регистрации желаемых служб.
Примерcode:
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));
}
Оба случая автоматически обнаруживаются, и в надлежащее время вызываются методы регистрации.Однако во втором случае преимущество заключается в том, что регистрации доступны как метаданные службы для запроса в более позднее время.