У меня есть базовый класс обслуживания и разные подклассы наследуют от него, как я могу внедрить все сервисы, реализующие этот класс
public abstract class AppService
{
public string ServiceName {get;set;}
}
, и у меня есть другие классы
public class CountryService:AppService
{
public list<Countries> getCountryByName(string name){
return ......
}
}
public class TestService:AppService
{
public void Test(){
return ......
}
}
Как я могу автоматически внедрить любой класс, наследуемый от AppService, без необходимости добавлять этот класс в StartUp вручную
Обновление *****************
Я использую следующее зарегистрировать сервисы в startUp
services.Configure<ServiceConfig>(config =>
{
config.Services = new List<ServiceDescriptor>(services);
config.Path = "/listservices";
});
ContainerSetup.InitializeWeb(Assembly.GetExecutingAssembly(), services);
и в проекте Services здесь есть настройка контейнера:
public static IServiceProvider InitializeWeb(Assembly webAssembly, IServiceCollection services) =>
new AutofacServiceProvider(BaseAutofacInitialization(setupAction =>
{
setupAction.Populate(services);
setupAction.RegisterAssemblyTypes(webAssembly).AsSelf();
}));
public static IContainer BaseAutofacInitialization(Action<ContainerBuilder> setupAction = null)
{
var builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
.Where(t => t.BaseType == typeof(AppService))
.AsSelf();
setupAction?.Invoke(builder);
return builder.Build();
}
Все еще Получение ошибки Произошло необработанное исключение при обработке запроса. InvalidOperationException: невозможно разрешить службу для типа