【发布时间】:2019-09-26 19:42:16
【问题描述】:
我有这个配置文件:
public class Config: IDataConfig, IIdentityServerConfig, IStorageConfig
{
public string ConnectionString { get; set; }
public string Authority { get; set; }
public string AzureStorageConnectionString { get; set; }
}
在我的一堂课中,我有这个:
public class StorageClient : IStorageClient
{
private readonly CloudBlobClient _blobClient;
public StorageClient(IStorageConfig config)
{
var storageAccount = CloudStorageAccount.Parse(config.AzureStorageConnectionString);
_blobClient = storageAccount.CreateCloudBlobClient();
}
public CloudBlobContainer GetContainerReference(string name) => _blobClient.GetContainerReference(name);
}
如您所见,它希望将IStorageConfig 的实例传递给它。
在过去,您会使用 autofac 并将配置注册为所有已实现的接口。
在 .net 核心中,我想知道如何做到这一点。目前我有这个:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(m => m.AddPolicy("AllowAll", o => o.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()));
services.Configure<Config>(Configuration.GetSection("ConnectionStrings"));
services.Configure<Config>(Configuration.GetSection("Options"));
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
var buildServiceProvider = services.BuildServiceProvider();
var config = buildServiceProvider.GetService<IOptions<Config>>();
services.AddTransient(typeof(IGenericService<>), typeof(GenericService<>));
services.AddTransient<IContainerProvider, ContainerProvider>();
services.AddSingleton<IComparer, Comparer>();
services.AddSingleton<IDataTypeFactory, DataTypeFactory>();
services.AddSingleton<IFilterProvider, FilterProvider>();
services.AddSingleton<IJsonClient, JsonClient>();
services.AddSingleton<INumericFactory, NumericFactory>();
services.AddSingleton<IStorageClient, StorageClient>();
services.AddSingleton<IStringFactory, StringFactory>();
services.AddSingleton<IValidator, Validator>();
services.AddSingleton<HttpClient>();
services.AddDbContext<DatabaseContext>
(options => options.UseSqlServer(config.Value.ConnectionString));
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new Info {Title = "Situ Experience Platform API", Version = "v1"});
options.IncludeXmlComments($"{System.AppDomain.CurrentDomain.BaseDirectory}\\Api.xml");
});
services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options =>
{
options.Authority = config.Value.Authority;
options.RequireHttpsMetadata = false;
options.ApiName = "Sxp";
});
services.AddMvc()
.ConfigureApiBehaviorOptions(options => { options.SuppressModelStateInvalidFilter = true; });
}
有谁知道我怎样才能实现我所追求的目标?
这是我的 appsettings 文件:
{
"ConnectionStrings": {
"ConnectionString": "Server=localhost;Database=sxp_master;Trusted_Connection=True;",
"Storage": "DefaultEndpointsProtocol=https;AccountName=sxp;AccountKey=moo"
},
"Options": {
"Authority": "https://localhost:44362"
},
"Logging": {
"Debug": {
"LogLevel": {
"Default": "Information"
}
},
"Console": {
"IncludeScopes": false,
"LogLevel": {
"Microsoft.AspNetCore.Mvc.Razor.Internal": "Warning",
"Microsoft.AspNetCore.Mvc.Razor.Razor": "Debug",
"Microsoft.AspNetCore.Mvc.Razor": "Error",
"Default": "Information"
}
},
"LogLevel": {
"Default": "Debug"
}
},
"AllowedHosts": "*"
}
【问题讨论】:
-
@Nkosi 它不会覆盖,而是将它们添加到集合中。但这不是这里的问题。我希望能够注入我的配置接口而不是做
IOptions<Config>,因为我不想/喜欢注入具体的类。特别是因为其中一些类“可能”是外部的。 -
好的,我会删除该评论。根据更新,我可以看到
ConnectionString和Authority来自哪里,但不是AzureStorageConnectionString -
我什至会满足于
IOptions<IStorageConfig>但不,IOptions 要求对象具有构造函数(无参数)
标签: c# asp.net-core dependency-injection