C# 将接口传递给Startup.cs ConfigureServices中的另一个服务
我需要将接口作为参数传递给另一个服务的构造函数 MSAuthService构造函数需要3个参数:C# 将接口传递给Startup.cs ConfigureServices中的另一个服务,c#,.net,asp.net-core,.net-core,C#,.net,Asp.net Core,.net Core,我需要将接口作为参数传递给另一个服务的构造函数 MSAuthService构造函数需要3个参数: public MSAuthService(string jwtSecret, int jwtLifespan, IUserService userService) { this._jwtSecret = jwtSecret; this._jwtLifespan = jwtLifespan; this._userService = userService; } Startup
public MSAuthService(string jwtSecret, int jwtLifespan, IUserService userService)
{
this._jwtSecret = jwtSecret;
this._jwtLifespan = jwtLifespan;
this._userService = userService;
}
Startup.cs:
services.AddScoped<IUserService, UserManager>();
....
services.AddSingleton<IAuthService>(
new MSAuthService(
MyConfigurationManager.GetJWTSecretKey(),
MyConfigurationManager.GetJWTLifespan(),
// I want to pass IUserService as parameter here
)
);
services.addScope();
....
服务.AddSingleton(
新MSAuthService(
MyConfiguration Manager.GetJWTSecretKey(),
MyConfiguration Manager.GetJWTLifespan(),
//我想在这里将IUserService作为参数传递
)
);
我不知道如何将IUserService传递给MSAuthService的构造函数。我不想将UserManager(concrete)类作为参数传递。您是否尝试在
Configure
方法中使用此选项
var scope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope();
scope.ServiceProvider.GetService<IUserService>();
var scope=app.ApplicationServices.GetRequiredService().CreateScope();
scope.ServiceProvider.GetService();
您是否尝试在Configure
方法中使用此选项
var scope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope();
scope.ServiceProvider.GetService<IUserService>();
var scope=app.ApplicationServices.GetRequiredService().CreateScope();
scope.ServiceProvider.GetService();
AddSingleton
具有接受函数的重载
您可以使用IServiceProvider
检索已注册的依赖项,使用GetRequiredService
:
services.AddSingleton<IAuthService>(sp =>
new MSAuthService(
MyConfigurationManager.GetJWTSecretKey(),
MyConfigurationManager.GetJWTLifespan(),
sp.GetRequiredService<IUserService>()
)
);
services.AddSingleton(sp=>
新MSAuthService(
MyConfiguration Manager.GetJWTSecretKey(),
MyConfiguration Manager.GetJWTLifespan(),
sp.GetRequiredService()
)
);
AddSingleton
具有接受函数的重载
您可以使用IServiceProvider
检索已注册的依赖项,使用GetRequiredService
:
services.AddSingleton<IAuthService>(sp =>
new MSAuthService(
MyConfigurationManager.GetJWTSecretKey(),
MyConfigurationManager.GetJWTLifespan(),
sp.GetRequiredService<IUserService>()
)
);
services.AddSingleton(sp=>
新MSAuthService(
MyConfiguration Manager.GetJWTSecretKey(),
MyConfiguration Manager.GetJWTLifespan(),
sp.GetRequiredService()
)
);
我试过你的答案。我收到以下错误:“无法从根提供程序解析作用域服务“ABC.Business.Abstract.IUserService”,并且您已注册了IUserService
的实现?是。我有UserManager.cs类。@emert117作用域服务不能以这种方式注入singleton。您需要将其中一个服务的生存期范围更改为services.AddSingleton…
到services.AddScoped
。谢谢,我试过你的答案了。我收到以下错误:“无法从根提供程序解析作用域服务“ABC.Business.Abstract.IUserService”,并且您已注册了IUserService
的实现?是。我有UserManager.cs类。@emert117作用域服务不能以这种方式注入singleton。您需要将其中一个服务的生存期范围更改为services.AddSingleton…
到services.AddScoped
。谢谢,很好的解决方案,我的老朋友@AmerllicA…:)嗯,很好的解决方案我的老朋友@AmerllicA…:)