在“启动配置"方法中获取OWIN运行地址 [英] Get OWIN running address inside Startup Configuration method

查看:89
本文介绍了在“启动配置"方法中获取OWIN运行地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有OWIN启动类的WebApi.

I have a WebApi with OWIN startup class.

我想在Configuration方法中调用一个方法(以便注册Web api),并且我需要服务的地址(如localhost:12345).

I want to call a method inside the Configuration method (in order to register the web api) and I need the address of the service (like localhost:12345).

我怎么能得到它?

public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var config = new HttpConfiguration();
            ...
            config.Formatters.Clear();
            config.Formatters.Add(new JsonMediaTypeFormatter());

            RegisterService(serviceAddress); // <- here
            ...
         }
}

推荐答案

我做了这样的事情:

public class WebServer : IDisposable
{
    private static IDisposable WebApplication = null;
    private static WebServer _Instance = null;
    public static GetInstance()
    {
        //other tests before here
        if(_Instance == null)
        {
             WebApplication = Microsoft.Owin.Hosting.WebApp.Start<WebServer>("localhost:12345");
             _Instance = new WebServer();
             _Instance._HostAddress = hostAddress;
        }
    }

    public void Configuration(IAppBuilder app)
    {
        HubConfiguration config = new HubConfiguration();
        config.EnableJSONP = true;
        app.UseCors(CorsOptions.AllowAll);
        app.MapSignalR(config);
        // other config
    }

    public void Dispose()
    {
        if (WebApplication != null)
            WebApplication.Dispose();
        WebApplication = null;
        _Instance = null;
    }
}

WebServer webserver = WebServer.GetInstance();
//later
webserver.Dispose();

我的与我的有一点不同,因为我使用多个端口,进行一些SSL检查并传入端口和IP,但这是要点.

Mine is a little different than this as I use multiple ports, have some SSL checks and pass in the port and IP, but this is the gist of it.

这篇关于在“启动配置"方法中获取OWIN运行地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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