我正在将一个项目从 .NET 4.52 移植到 .NET Core。该项目之前使用 Structuremap 进行依赖注入,并且在 Structuremap 中您不需要配置具体类型来启用依赖注入。有没有办法通过 .NET Core 中的内置依赖注入来做到这一点?
我试图创建所描述的,这将作为Windows服务运行的服务在这里。我的问题是示例 Web 主机服务构造函数只接受一个IWebHost参数。我的服务需要一个更像这样的构造函数:
public static class HostExtensions
{
public static void RunAsMyService(this IWebHost host)
{
var webHostService =
new MyService(host, loggerFactory, myClientFactory, schedulerProvider);
ServiceBase.Run(webHostService);
}
}
Run Code Online (Sandbox Code Playgroud)
我的Startup.cs文件看起来类似于:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables()
.AddInMemoryCollection();
this.Configuration = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
this.container.RegisterSingleton<IConfiguration>(this.Configuration);
services.AddSingleton<IControllerActivator>(
new SimpleInjectorControllerActivator(container));
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
app.UseSimpleInjectorAspNetRequestScoping(this.container);
this.container.Options.DefaultScopedLifestyle = new AspNetRequestLifestyle();
this.InitializeContainer(app, loggerFactory);
this.container.Verify();
if (env.IsDevelopment())
{ …Run Code Online (Sandbox Code Playgroud) 我在ASP.net Core中创建了一个示例项目使用了四个层,如下所示
我还在我的API项目中的startup.cs中实现了依赖注入.它的工作完美.
services.AddTransient<IUserRepository, UserRepository>();
Run Code Online (Sandbox Code Playgroud)
我有两个问题要问.
是否可以在我的Infrastructure层中进行依赖注入,而不是在API层中?如果是的话,你能指导我怎么做吗?
如果我错了,请纠正我,如果Asp.Net Core默认具有依赖注入,那么我们不需要Autofac(或类似的第三方DI插件).对?让我重新解释一下这个问题.在Asp Core中使用Autofac有什么用?
任何意见将是有益的.
谢谢.
c# dependency-injection autofac asp.net-core-mvc asp.net-core
我发现了一篇关于
https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.1
并解释了asp.net核心DI和服务寿命。
文章提到以下生命周期:
我试图找到一个真实的例子,或者至少是一个关于何时使用每个生命周期的更好的解释。
我有一个通用接口IPipelineBehavior<TRequest, TResponse>(来自 MediatR)。我正在尝试为此接口注册特定行为,如下所示:
services.AddTransient(typeof(IPipelineBehavior<,>),
typeof(ValidationMiddleware<,>));
Run Code Online (Sandbox Code Playgroud)
ValidationMiddlewareIPipelineBehavior像这样实现:
public class ValidationMiddleware<TRequest, TResponse>
: IPipelineBehavior<TRequest, Result<TResponse>>
Run Code Online (Sandbox Code Playgroud)
Result是一个自定义类,我希望所有 MediatR IRequestHandlers 返回。
当应用程序运行时,服务容器给出以下错误:
System.ArgumentException:实现类型“StudentManagement.App.Middleware.ValidationMiddleware
2[StudentManagement.App.Students.DisenrollStudentCommand,StudentManagement.App.Result1[System.Object]]”无法转换为服务类型“MediatR.IPipelineBehavior2[StudentManagement.App.Students.DisenrollStudentCommand,StudentManagement.App.Result1[System.Object]]”
我不明白为什么,因为它们显然在运行时都具有相同的类型参数。我的猜测是,该Result<TResponse>位由于某种原因导致了错误,因为其他行为在刚刚实现时工作正常IPipelineBehavior<TRequest, TResponse>。
有谁明白为什么我会收到此错误,以及我需要做什么来解决它?
IoC现在已经从头开始构建到ASP.NET 5中.使用第三方IoC容器(如Autofac或Unity)有什么好处?
使用第三方IoC容器会提供更好还是更差的性能?内置的IoC容器没有其他真正有用的功能吗?
我不能再看到使用它的好处,但想确保我没有遗漏某些东西.