2017-09-08 4 views
3

Ich möchte meine Lösung so ändern, dass sie unter einer selbst gehosteten OWIN-Instanz ausgeführt wird, aber ich benötige sie auch unter http://localhost, wenn nötig.Self-Hostable und IIS-hostable OWIN-Projekt

Wie sollte ich meine Startup-Klasse strukturieren, um von beiden erkannt zu werden?

Derzeit habe ich das Web-API-Projekt als Konsolenanwendung mit der Projekt-URL als http://localhost:2746 und dies ist mein Startklasse:

[assembly: OwinStartup(typeof(Startup))] 
namespace Books 
{ 
    public class Startup 
    { 
     public static void Main(string[] args) 
     { 
      const int port = 2746; 
      var url = $"http://localhost:{port}/"; 

      using (WebApp.Start<Startup>(new StartOptions(url) { ServerFactory = "Microsoft.Owin.Host.HttpListener" })) 
      { 
       var client = new HttpClient { BaseAddress = new Uri(url) }; 
       Console.ReadLine(); 
      } 
     } 

     public void Configuration(IAppBuilder app) 
     { 
      HttpConfiguration httpConfiguration = new HttpConfiguration(); 
      WebApiConfig.Register(httpConfiguration); 
      app.Use<CustomExceptionMiddleware>().UseWebApi(httpConfiguration); 
      app.UseFileServer(StaticFileConfig.Build()); 
     } 
    } 
} 

ich dies auch in der web.config habe:

<add key="owin:AutomaticAppStartup" value="false" /> 

Antwort

0

Einsatz zwei Projekte:

  1. ein Webprojekt für die api-Controller erstellen. Dieses Projekt kann in iis
  2. erstellen Sie eine andere Konsole-Projekt als Selbst Host-Host, dieses Projekt muss das Web-Projekt verweisen, jetzt können Sie die WebApiConfig/Controllers zu diesem Projekt teilen

ps: OwinStartup Attribut angeben, die Einstiegspunkt für Iis, aber ich benutze die traditionelle global.asax, um die API-Konfiguration zu initialisieren. so dass die Startup Klasse kann auch die Konfiguration von WebApiConfig

teilen