2016-05-16 19 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,但是这是要点的。