OWIN Startup类与WebAPIConfig.Register方法一起执行 [英] OWIN Startup class is being executed along with WebAPIConfig.Register method

查看:112
本文介绍了OWIN Startup类与WebAPIConfig.Register方法一起执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在集成测试项目之一中使用 Microsoft.Owin.Hosting 自托管Web API,以测试端到端功能.

I am using Microsoft.Owin.Hosting in my one of integration test project to self-host the Web API in order to test end to end functionality.

[TestMethod]
public void GetLoanApplications()
{
    using (WebApp.Start<Startup>(url: url))
    {
        using (var client = new HttpClient())
        {
            // Create httpclient and send request-and-response-metadata-in-aspnet-web-api
        }
    }
}

我能够自行托管Web API,并能够调用控制器操作.Owin需要一些 Startup 类配置,如下所示:

I am able to self-host the web API and able to invoke the controller action. Owin requires some Startup class configuration, which is as follows:

[assembly: OwinStartup(typeof(MyService.App_Start.Startup))]
namespace MyService.App_Start
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            HttpConfiguration config = new HttpConfiguration();
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
            app.UseWebApi(config);
        }
    }
}

这是我的Web API Config方法,如下所示:

Here is my Web API Config method looks like:

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);
    }
}

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );            
    }
}

问题

  • 当我运行主应用程序(而不是测试项目)时,会同时调用Owin启动和Web API配置方法.
  • 如果我要配置一些服务或过滤器,它将被调用两次.
  • 我以为如果我正在运行测试项目,它应该只调用owin启动文件(它现在正在执行),而当我调试主应用程序时,它应该仅调用Web API配置注册方法.
  • When I run my main application (not the test project) both the Owin startup and web API config methods are getting called.
  • Let's say if I have some services or filters to be configured, it will get invoked twice.
  • What I thought is IF I am running test project it should only invoke owin startup file (which it is doing right now) and when I am debugging my main app it should only call web API config register method.

有人知道这是应该起作用的方式还是我做错了什么?

Any idea on is this the way it should work or I am doing something wrong?

推荐答案

首先,系统需要一种区分环境的方法.

First the system would need a way to differentiate between the environments.

<appSettings>
    <add key="APP_ENVIRONMENT" value="Development" />
    <!-- ... -->
</appSettings>

由于在生产环境中运行Owin启动和Web api配置方法时,都已将OWIN配置为项目中的一等公民.

Since when run in production both the Owin startup and web api config methods are getting called then OWIN is already configured as a first class citizen in the project.

我建议将代码从 Application_Start 移至owin启动

I suggest moving the code from Application_Start into owin start up

public class WebApiApplication : System.Web.HttpApplication {
    protected void Application_Start() {
        //REMOVE THIS AND LET OWIN STARTUP HANDLE SETUP
        //GlobalConfiguration.Configure(WebApiConfig.Register);
    }
}

,以便仅根据配置的环境调用一次.

so that it is only invoked once depending on the configured environment.

[assembly: OwinStartup(typeof(MyService.App_Start.Startup))]
namespace MyService.App_Start {
    public class Startup {
        const string EnvironmentKey = "APP_ENVIRONMENT";
        const string PRODUCTION = "Production";
        const string TEST = "Test";

        public void Configuration(IAppBuilder app) {
            string ENVIRONMENT = ConfigurationManager.AppSettings[EnvironmentKey] 
                                    ?? Production;
            if(ENVIRONMENT == TEST) {
                var config = new HttpConfiguration();
                WebApiConfig.Register(config);
                app.UseWebApi(config);
            } else {
                GlobalConfiguration.Configure(WebApiConfig.Register);
            }
        }
    }
}

请注意 WebApiConfig.Register 的重用,以避免重复代码.如果要实施其他配置(例如,开发,测试,登台,起诉...等),则相同的配置将应用于两种环境,并且都将位于同一位置.

Note the reuse of the WebApiConfig.Register to avoid repeated code. The same configuration will be applied for either environment and all in the same place if additional configurations were to be implemented (ie Development, Testing, Staging, Procustion...etc)

测试项目 app.config 将包括当前环境的设置

The test project app.config would include the current environment's setting

<appSettings>
    <add key="APP_ENVIRONMENT" value="Test" />
    <!-- ... -->
</appSettings>

并调用用于自托管的启动配置,否则它将默认返回到生产设置,但也已在 web.config

and invoke the start up configuration for self-hosting otherwise it will default back to production settings, but would have also been configured in the web.config

<appSettings>
    <add key="APP_ENVIRONMENT" value="Production" />
    <!-- ... -->
</appSettings>

这篇关于OWIN Startup类与WebAPIConfig.Register方法一起执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆