2016-05-16 18 views
0

我有一個OWA啓動類的WebApi。在啓動配置方法中獲取OWIN運行地址

我想調用配置方法內部的一個方法(爲了註冊web api),我需要服務的地址(如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 
      ... 
     } 
} 

回答

1

我做了這樣的事情:

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,但是這是要點的。