在启动时使用静态参数设置配置部分而不是 .net 核心中的 json 文件
Set configuration part in startup with static parameters not json file in .net core
我的测试解决方案中有这行代码。
我想在我的测试代码中配置 ravenoption。
public IHost host = null;
public IDocumentStore documentStore = null;
public TestHostBuilder()
{
ConfigureServer(new TestServerOptions() {FrameworkVersion = null});
documentStore = GetDocumentStore();
Environment.SetEnvironmentVariable("TEST_ENV", "on");
var hostBuilder = easy.api.Program.CreateHostBuilder(new string[0])
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder.UseTestServer();
}).ConfigureAppConfiguration(config =>
{
config.Configure<domain.Environments.RavenOptions>(new domain.Environments.RavenOptions() {PublicUrl=documentStore.Urls.ToString() });
})
.ConfigureServices(services =>
{
services.AddScoped<ICurrentUserService, InitRequest>();
services.AddScoped<ICacheStorage>(provider =>
{
return new Mock<ICacheStorage>().Object;
});
services.AddRavenDbAsyncSession(GetDocumentStore(new GetDocumentStoreOptions()));
services.AddTransient<IAsyncDocumentSession>((c) =>
{
return documentStore.OpenAsyncSession();
});
});
host = hostBuilder.Start();
}
所以在这一行
config.Configure<domain.Environments.RavenOptions>(new domain.Environments.RavenOptions() {PublicUrl=documentStore.Urls.ToString() });
我收到这个错误:
Severity Code Description Project File Line Suppression State
Error CS1929 'IConfigurationBuilder' does not contain a definition for 'Configure' and the best extension method overload
最后我创建了一个 Ioption 对象并将其注入到应用程序中:
.ConfigureServices(services =>
{
domain.Environments.RavenOptions appSettings = new domain.Environments.RavenOptions()
{
PublicDbName = documentStore.Database,
CertificateDirectory="",
IsHttps=false,
PublicUrl=documentStore.Identifier.Split("(").First(),
TseDbName=documentStore.Database,
ShardUrls=Enumerable.Repeat(documentStore.Identifier.Split("(").First(), 4).ToArray()
};
IOptions<domain.Environments.RavenOptions> options = Options.Create(appSettings);
services.AddScoped<IOptions<domain.Environments.RavenOptions>>((p) =>
{
return options;
});
我的测试解决方案中有这行代码。
我想在我的测试代码中配置 ravenoption。
public IHost host = null;
public IDocumentStore documentStore = null;
public TestHostBuilder()
{
ConfigureServer(new TestServerOptions() {FrameworkVersion = null});
documentStore = GetDocumentStore();
Environment.SetEnvironmentVariable("TEST_ENV", "on");
var hostBuilder = easy.api.Program.CreateHostBuilder(new string[0])
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder.UseTestServer();
}).ConfigureAppConfiguration(config =>
{
config.Configure<domain.Environments.RavenOptions>(new domain.Environments.RavenOptions() {PublicUrl=documentStore.Urls.ToString() });
})
.ConfigureServices(services =>
{
services.AddScoped<ICurrentUserService, InitRequest>();
services.AddScoped<ICacheStorage>(provider =>
{
return new Mock<ICacheStorage>().Object;
});
services.AddRavenDbAsyncSession(GetDocumentStore(new GetDocumentStoreOptions()));
services.AddTransient<IAsyncDocumentSession>((c) =>
{
return documentStore.OpenAsyncSession();
});
});
host = hostBuilder.Start();
}
所以在这一行
config.Configure<domain.Environments.RavenOptions>(new domain.Environments.RavenOptions() {PublicUrl=documentStore.Urls.ToString() });
我收到这个错误:
Severity Code Description Project File Line Suppression State
Error CS1929 'IConfigurationBuilder' does not contain a definition for 'Configure' and the best extension method overload
最后我创建了一个 Ioption 对象并将其注入到应用程序中:
.ConfigureServices(services =>
{
domain.Environments.RavenOptions appSettings = new domain.Environments.RavenOptions()
{
PublicDbName = documentStore.Database,
CertificateDirectory="",
IsHttps=false,
PublicUrl=documentStore.Identifier.Split("(").First(),
TseDbName=documentStore.Database,
ShardUrls=Enumerable.Repeat(documentStore.Identifier.Split("(").First(), 4).ToArray()
};
IOptions<domain.Environments.RavenOptions> options = Options.Create(appSettings);
services.AddScoped<IOptions<domain.Environments.RavenOptions>>((p) =>
{
return options;
});