自托管 owin webapi 应用程序无法在 web.config 中找到连接字符串
Self hosted owin webapi app cannot find connection string in web.config
我的 webapi 应用程序在从 visual studio 启动时运行良好,但当 运行 在 OWIN 自身主机上时抛出以下错误:
"No connection string named 'ProductsDbContext' could be found in the
application config file."
OWIN 是否需要一些额外的配置才能访问 Web.config 文件?
这是我的 Startup.cs:
public void Configuration(IAppBuilder appBuilder)
{
// Configure Web API for self-host.
var config = new HttpConfiguration();
WebApiConfig.Register(config);
UnityConfig.RegisterComponents(config);
appBuilder.UseWebApi(config);
}
和我自己的主机代码:
[TestMethod]
public void ListScorecardSlices()
{
var baseAddress = "http://localhost:9000/";
// Start OWIN host
using (WebApp.Start<Startup>(baseAddress))
{
HttpClient client = new HttpClient();
var response = client.GetAsync(baseAddress + "api/products(2)").Result;
var responseBody = response.Content.ReadAsStringAsync().Result;
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
}
如果您在某种服务或其他非 Web 应用程序中托管 OWIN(这很可能,因为否则您为什么需要自托管),那么您不需要 web.config
文件,但应用程序配置文件,名称为 <your executable name with extension>.config
。如果将 app.config
文件添加到可执行项目,Visual Studio 将自动将其与可执行文件放在一起。
我的 webapi 应用程序在从 visual studio 启动时运行良好,但当 运行 在 OWIN 自身主机上时抛出以下错误:
"No connection string named 'ProductsDbContext' could be found in the application config file."
OWIN 是否需要一些额外的配置才能访问 Web.config 文件?
这是我的 Startup.cs:
public void Configuration(IAppBuilder appBuilder)
{
// Configure Web API for self-host.
var config = new HttpConfiguration();
WebApiConfig.Register(config);
UnityConfig.RegisterComponents(config);
appBuilder.UseWebApi(config);
}
和我自己的主机代码:
[TestMethod]
public void ListScorecardSlices()
{
var baseAddress = "http://localhost:9000/";
// Start OWIN host
using (WebApp.Start<Startup>(baseAddress))
{
HttpClient client = new HttpClient();
var response = client.GetAsync(baseAddress + "api/products(2)").Result;
var responseBody = response.Content.ReadAsStringAsync().Result;
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
}
如果您在某种服务或其他非 Web 应用程序中托管 OWIN(这很可能,因为否则您为什么需要自托管),那么您不需要 web.config
文件,但应用程序配置文件,名称为 <your executable name with extension>.config
。如果将 app.config
文件添加到可执行项目,Visual Studio 将自动将其与可执行文件放在一起。