CrazyDog
CrazyDog

Reputation: 321

Get OWIN running address inside Startup Configuration method

I have a WebApi with OWIN startup class.

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).

How could I be able to get it?

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

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

Upvotes: 0

Views: 420

Answers (1)

Steven Edison
Steven Edison

Reputation: 527

I did something like this:

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

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.

Upvotes: 1

Related Questions