这是我的项目结构
**MyApp.API**
/controllers
/repositories
/models
/helpers
startup.cs
startup.cs包含
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddMvc();
services.AddTransient<FactoryActivatedMiddleware>();
services.AddDetection();
services.AddScoped<IAccountsRepository, AccountsRepository>();
services.AddScoped<IMetaRepository, MetaRepository>();
services.AddScoped<INotificationsRepository, NotificationsRepository>();
services.AddScoped<ILogsRepository, LogsRepository>();
...
...
}
我之所以在上面的API项目中有startup.cs,是因为它作为一个项目由另一个开发人员管理。
我想把API项目和web项目作为一个单独的解决方案一起添加。
**MyApp.Web**
/controllers
/views
/webroot
startup.cs
program.cs
startup.cs包含
public void ConfigureServices(IServiceCollection services)
{
var controllerAssembly = Assembly.Load(new AssemblyName("MyApp.API"));
services.AddMvc()
.AddApplicationPart(controllerAssembly)
.AddControllersAsServices();
}
我是从
this link
. 但是,它抛出了错误
处理请求时发生未处理的异常。
InvalidOperationException:无法解析类型的服务
“MyApp.API.Repository.IAccountsRepository”正在尝试
激活“MyApp.API.Controllers.AccountsController”。
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(类型
服务类型,类型实现类型,调用站点链调用站点链,
ParameterInfo[]参数,bool throwIfCallSiteNotFound)
看起来我将在Web启动中添加所有来自API的服务。我觉得注册每项服务的想法并不理想。
我想得越多-我将不得不在API中使用一些静态方法,可以将Web启动服务注入其中。这样我就可以单独运行API,也可以在Web项目中运行API
有没有更好的方法来加载所有MyApp.API所需的类,而不必在MyApp.Web启动中手动执行?