自托管的owin webapi应用程序在web.config中找不到连接字符串

时间:2015-09-25 19:17:57

标签: entity-framework asp.net-web-api owin dbcontext

我的webapi应用程序在从Visual Studio启动时运行正常,在OWIN自我主机上运行时会抛出以下错误:

  

“找不到名为'ProductsDbContext'的连接字符串   应用程序配置文件。“

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);
        }
    }

1 个答案:

答案 0 :(得分:2)

如果您在某种服务或其他非Web应用程序中托管您的OWIN(很可能,因为为什么还需要自托管),那么您不需要web.config文件,但应用程序配置文件,名称为<your executable name with extension>.config。如果将app.config文件添加到可执行项目中,Visual Studio将自动将其与可执行文件放在一起。

相关问题