New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
注入会不成功。 #94
Comments
控制器默认是单实例,需要发这样配置 [Controller(SingleInstance =false)] |
谢谢。 |
[Controller(SingleInstance =false)] 使用了此方法。但是发现,还是注册成了单例,是我写的不对吗? |
不是单例,但是会有一个bug。F5启动的时候,会创建controller, |
可以查看 https://github.com/IKende/FastHttpApi/blob/master/src/ActionContext.cs 代码,你这种情况只有注入构建不成功就会拿单例 |
可以尝试发布版测试,如果调试才有可能是DI在调试时注入的方式有些问题 |
发布版的。我也试了,一半成功,一半失败。因为我设置的是每次请求两个。
|
Controller的代码
FROM [tb_Mall_HomeSliderAds] |
我加了一句话在Controller里面然后看了日志 this._dbConnection = dbConnection; HttpApiServer.Register 这里调用。会导致注册。 |
你把 |
我建议你直接注入个 |
好嘛,我直接注入连接字符串好了。 |
var builder = new HostBuilder()
.ConfigureServices((hostContext, services) =>
{
services
.UseBeetlexHttp(....)
.AddScoped(db => new SqlConnection(
config.GetConnectionString("Db")))
});
builder.Build().Run();
按此方式注入的IDbConnection,一个客户端一次一次调用是可以的,但是不能并发。多个客户端调用,使用的是同一个IDbConnection对象。这是为什么?
private readonly IDbConnection _dbConnection;
private readonly string _HttpServerUrl;
public MoneyController(IDbConnection dbConnection, IOptions httpServerUrl)
{
this._dbConnection = dbConnection;
this._HttpServerUrl = httpServerUrl.Value.HttpServerUrl;
}
然后在asp.net core 中同样的方式就可以。是我那里写错了吗?下面是asp.net core的方式。
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped(db => new SqlConnection(
Configuration.GetConnectionString("Db")));
}
public MemberController(ILogger logger, IDbConnection dbConnection, IConfiguration configuration)
{
this._logger = logger;
this._dbConnection = dbConnection;
this._configuration = configuration;
this.sign_key = this._configuration["sign_key"];
The text was updated successfully, but these errors were encountered: