从 Configure 中获取 RequiredService
GetRequiredService from within Configure
我正在尝试从 aspnet core Startup.cs 中的 Configure 调用中访问我的一项服务。我正在执行以下操作,但出现以下错误 "No service for type 'UserService' has been registered." 现在我知道它已注册,因为我可以在控制器中使用它,所以在这里使用它时我只是做错了。请有人能指出我正确的方向。如果有更好的方法来实现我想要的,我很乐意采用不同的方法来设置 Tus。
var userService = app.ApplicationServices.GetRequiredService<UserService>();
userService.UpdateProfileImage(file.Id);
下面是我想使用的地方
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
... Other stuff here...
app.InitializeSimpleInjector(container, Configuration);
container.Verify();
app.UseTus(httpContext =>
{
var restaurantEndpoint = "/restaurant/images";
var userEndpoint = "/account/images";
var endPoint = "/blank/images";
if (httpContext.Request.Path.StartsWithSegments(new PathString(restaurantEndpoint)))
{
endPoint = restaurantEndpoint;
}
if (httpContext.Request.Path.StartsWithSegments(new PathString(userEndpoint)))
{
endPoint = userEndpoint;
}
return new BranchTusConfiguration
{
Store = new TusDiskStore(@"C:\tusfiles\"),
UrlPath = endPoint,
Events = new Events
{
OnBeforeCreateAsync = ctx =>
{
return Task.CompletedTask;
},
OnCreateCompleteAsync = ctx =>
{
return Task.CompletedTask;
},
OnFileCompleteAsync = async ctx =>
{
var file = await ( (ITusReadableStore)ctx.Store ).GetFileAsync(ctx.FileId, ctx.CancellationToken);
var userService = app.ApplicationServices.GetRequiredService<UserService>();
userService.UpdateProfileImage(file.Id);
}
}
};
});
... More stuff here...
};
我的最终目标是将其移至 IApplicationBuilder 扩展以清理我的 startup.cs,但如果它在 startup.cs
中工作,则不会有任何影响
编辑:添加以显示 userService 的注册。在我遗漏的 InitializeSimpleInjector 方法中还有很多其他内容正在注册和交叉连接。如果需要可以全部添加..
public static void InitializeSimpleInjector(this IApplicationBuilder app, Container container, IConfigurationRoot configuration)
{
// Add application presentation components:
container.RegisterMvcControllers(app);
container.RegisterMvcViewComponents(app);
container.Register<UserService>(Lifestyle.Scoped);
container.CrossWire<IServiceProvider>(app);
container.Register<IServiceCollection, ServiceCollection>(Lifestyle.Scoped);
}
请仔细阅读 Simple Injector integration page for ASP.NET Core,因为 Simple Injector 与 ASP.NET Core 的集成非常不同,因为 Microsoft 记录了 DI 容器应如何集成。 Simple Injector 文档指出:
Please note that when integrating Simple Injector in ASP.NET Core, you do not replace ASP.NET’s built-in container, as advised by the Microsoft documentation. The practice with Simple Injector is to use Simple Injector to build up object graphs of your application components and let the built-in container build framework and third-party components
这意味着,由于内置容器仍然存在,因此使用 app.ApplicationServices.GetRequiredService<T>()
解析组件(当它们在 Simple Injector 中注册时)将不起作用。在那种情况下,您正在询问内置容器,但它不知道这些注册的存在。
相反,您应该使用 Simple Injector 解析您的类型:
container.GetInstance<UserService>()
我正在尝试从 aspnet core Startup.cs 中的 Configure 调用中访问我的一项服务。我正在执行以下操作,但出现以下错误 "No service for type 'UserService' has been registered." 现在我知道它已注册,因为我可以在控制器中使用它,所以在这里使用它时我只是做错了。请有人能指出我正确的方向。如果有更好的方法来实现我想要的,我很乐意采用不同的方法来设置 Tus。
var userService = app.ApplicationServices.GetRequiredService<UserService>();
userService.UpdateProfileImage(file.Id);
下面是我想使用的地方
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
... Other stuff here...
app.InitializeSimpleInjector(container, Configuration);
container.Verify();
app.UseTus(httpContext =>
{
var restaurantEndpoint = "/restaurant/images";
var userEndpoint = "/account/images";
var endPoint = "/blank/images";
if (httpContext.Request.Path.StartsWithSegments(new PathString(restaurantEndpoint)))
{
endPoint = restaurantEndpoint;
}
if (httpContext.Request.Path.StartsWithSegments(new PathString(userEndpoint)))
{
endPoint = userEndpoint;
}
return new BranchTusConfiguration
{
Store = new TusDiskStore(@"C:\tusfiles\"),
UrlPath = endPoint,
Events = new Events
{
OnBeforeCreateAsync = ctx =>
{
return Task.CompletedTask;
},
OnCreateCompleteAsync = ctx =>
{
return Task.CompletedTask;
},
OnFileCompleteAsync = async ctx =>
{
var file = await ( (ITusReadableStore)ctx.Store ).GetFileAsync(ctx.FileId, ctx.CancellationToken);
var userService = app.ApplicationServices.GetRequiredService<UserService>();
userService.UpdateProfileImage(file.Id);
}
}
};
});
... More stuff here...
};
我的最终目标是将其移至 IApplicationBuilder 扩展以清理我的 startup.cs,但如果它在 startup.cs
中工作,则不会有任何影响编辑:添加以显示 userService 的注册。在我遗漏的 InitializeSimpleInjector 方法中还有很多其他内容正在注册和交叉连接。如果需要可以全部添加..
public static void InitializeSimpleInjector(this IApplicationBuilder app, Container container, IConfigurationRoot configuration)
{
// Add application presentation components:
container.RegisterMvcControllers(app);
container.RegisterMvcViewComponents(app);
container.Register<UserService>(Lifestyle.Scoped);
container.CrossWire<IServiceProvider>(app);
container.Register<IServiceCollection, ServiceCollection>(Lifestyle.Scoped);
}
请仔细阅读 Simple Injector integration page for ASP.NET Core,因为 Simple Injector 与 ASP.NET Core 的集成非常不同,因为 Microsoft 记录了 DI 容器应如何集成。 Simple Injector 文档指出:
Please note that when integrating Simple Injector in ASP.NET Core, you do not replace ASP.NET’s built-in container, as advised by the Microsoft documentation. The practice with Simple Injector is to use Simple Injector to build up object graphs of your application components and let the built-in container build framework and third-party components
这意味着,由于内置容器仍然存在,因此使用 app.ApplicationServices.GetRequiredService<T>()
解析组件(当它们在 Simple Injector 中注册时)将不起作用。在那种情况下,您正在询问内置容器,但它不知道这些注册的存在。
相反,您应该使用 Simple Injector 解析您的类型:
container.GetInstance<UserService>()