2014-12-01 20 views
18

Visual Studio'da düzgün çalışan tam bir ASP.NET MVC uygulamasına (5 derleme, .NET 4.5.1, ASP.NET MVC 5.2.2'den oluşur) sahibim IISExpress kullanan).ASP.NET MVC uygulamasının kendiliğinden barındırıcısı

Artık MVC uygulamasını alan ve bunu barındıran bir konsol uygulamasına sahip olmak istiyorum (kendi kendini barındıran).

Microsoft.Owin.Host.HttpListener ve Nancy.Owin ile denedim, ancak 404 sayfa alırken, yapılandırmam MVC uygulamasına eşleşmiyor.

Ben
static void Main(string[] args) 
    { 
     StartOptions so = new StartOptions("http://localhost:9000/"); 
     using (WebApp.Start<Startup>(so)) 
     { 
      Console.WriteLine("Press Enter to Exit"); 
      Console.ReadLine(); 
     } 
    } 

public class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     app.UseNancy(); 
    } 
} 

ve

var Ama açıkçası yapılandırma eksik çalışan MVC uygulamadan MyMvcApplication kullanmak. Bu nasıl yapılır? Ya da aksi halde kendi kendini nasıl barındırırsınız?

Web'de bulduğum cevaplar eski sürümlere atıfta bulunuyor ve bugün daha kolay bir yol almayı umuyordum.

+2

Bildiğim kadarıyla artık bu mümkün olmayacaktır araştırmış olarak ASP.NET 5 (vNext; MVC 6) öncesi ve nancy ile benim uygulamayı barındıran ASP.NET taşınma gerektirecektir MVC'den Nancy'ye (ayrıca Razor şablon motorunu da kullanabilir). Doğru? – ZoolWay

+2

Yukarıdaki gibi, "kendi kendini barındıran" MVC ve Nancy, MVC için tamamen farklı bir web geliştirme çerçevesidir - bu MVC'yi barındırmanın alternatif bir yolu değildir ve bunları bir araya getiremezsiniz. –

+0

Soru, daha sonra kendiliğinden konaklamak için başka bir şey olup olmadığı veya MVC5'in herhangi bir geçici çözüm olmadan IIS'nin barındırma gerektirip gerektirmediğidir. – ZoolWay

cevap

12

ASP.NET vNext henüz mevcut değil ve benim uygulama MVC5 kullanıyor MVC uygulamasını tamamen Nancy veya benzer bir şekilde geçirmeliyim. MVC5, IIS'ye çok bağlı.

performans bir sorun değil gibi ben bir ara çözüm üzerinde karar bu arada çözmek için:

Benim konsol uygulaması bir IIS yapılandırma dosyası oluşturur ve bir IIS ekspres başlattı:

 // start IIS 
     bool systray = Debugger.IsAttached; 
     ProcessStartInfo psi = new ProcessStartInfo(iisExecutable, String.Format("/config:\"{0}\" /site:Ecm2.Web /trace:info /systray:{1}", configFile, systray)); 
     psi.UseShellExecute = false; 
     psi.RedirectStandardInput = false; 
     psi.RedirectStandardOutput = true; 
     psi.RedirectStandardError = true; 
     psi.CreateNoWindow = true; 

     if (this.iisProcess != null) throw new NotSupportedException("Multiple starts not supported"); 
     this.iisProcess = new Process(); 
     this.iisProcess.StartInfo = psi; 
     this.iisProcess.ErrorDataReceived += OnErrorDataReceived; 
     this.iisProcess.OutputDataReceived += OnOutputDataReceived; 
     this.iisProcess.Start(); 
     this.iisProcess.BeginErrorReadLine(); 
     this.iisProcess.BeginOutputReadLine(); 

birisi istiyorsanız

 if (this.iisProcess == null) throw new Exception("Does not look like there was something started yet!"); 

     if (this.iisProcess.HasExited) 
     { 
      log.WarnFormat("IIS has already exited with code '{0}'", this.iisProcess.ExitCode); 
      this.iisProcess.Close(); 
      return; 
     } 

     log.InfoFormat("Stopping IIS instance #{0}", this.instanceId); 
     ProcessCommunication.SendStopMessageToProcess(this.iisProcess.Id); 
     bool exited = this.iisProcess.WaitForExit(30000); 
     if (!exited) 
     { 
      log.WarnFormat("Failed to stop IIS instance #{0} (PID {1}), killing it now", this.instanceId, this.iisProcess.Id); 
      this.iisProcess.Kill(); 
     } 

     this.iisProcess.Close(); 

buna WM_QUIT göndermesi gerektiğini sıradan iis işlemini durdurmak için: Bu "dur" parçasının bir parçasıdır. Bu, bu yardımcı olabilir:

/// <summary> 
    /// Sends a WM_QUIT message to another process. 
    /// </summary> 
    /// <param name="pid">PID of the other process</param> 
    public static void SendStopMessageToProcess(int pid) 
    { 
     log.DebugFormat("Sending stop message to PID #{0}", pid); 
     try 
     { 
      for (IntPtr ptr = NativeMethods.GetTopWindow(IntPtr.Zero); ptr != IntPtr.Zero; ptr = NativeMethods.GetWindow(ptr, 2)) 
      { 
       uint num; 
       NativeMethods.GetWindowThreadProcessId(ptr, out num); 
       if (pid == num) 
       { 
        HandleRef hWnd = new HandleRef(null, ptr); 
        NativeMethods.PostMessage(hWnd, 0x12, IntPtr.Zero, IntPtr.Zero); 
        return; 
       } 
      } 
     } 
     catch (ArgumentException ex) 
     { 
      log.Error(String.Format("Failed to send WM_QUIT to PID #{0}", pid), ex); 
     } 
    } 

    /// <summary> 
    /// Provides the native methods to post messages to other windows processes. 
    /// </summary> 
    internal class NativeMethods 
    { 
     // Methods 
     [DllImport("user32.dll", SetLastError = true)] 
     internal static extern IntPtr GetTopWindow(IntPtr hWnd); 
     [DllImport("user32.dll", SetLastError = true)] 
     internal static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd); 
     [DllImport("user32.dll", SetLastError = true)] 
     internal static extern uint GetWindowThreadProcessId(IntPtr hwnd, out uint lpdwProcessId); 
     [DllImport("user32.dll", SetLastError = true)] 
     internal static extern bool PostMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam); 
    } 
+0

Peki, neden aynı zamanda stdinput yönlendirmek ve zarifçe iis durdurmak için 'Q' göndermek. Daha fazla HasExited için bekleyebilir ve bu failes eğer hala süreci durdurmak olabilir. –