2014-12-01 20 views
18

Tôi có một ứng dụng ASP.NET MVC đầy đủ (bao gồm 5 hội đồng, .NET 4.5.1, ASP.NET MVC 5.2.2) chạy tốt trong Visual Studio (trong đó sử dụng IISExpress).Tự lưu trữ ứng dụng ASP.NET MVC

Bây giờ tôi muốn có một ứng dụng giao diện điều khiển có ứng dụng MVC và lưu trữ nó (tự lưu trữ).

Tôi đã thử với Microsoft.Owin.Host.HttpListenerNancy.Owin nhưng trong khi tôi nhận được 404 trang, cấu hình của tôi thiếu bản đồ cho ứng dụng MVC của tôi.

tôi đã

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

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

Nhưng rõ ràng là cấu hình để sử dụng MyMvcApplication từ các ứng dụng MVC chạy là mất tích. Làm thế nào để làm điều đó? Hoặc làm thế nào để tự lưu trữ nó khác?

Câu trả lời tôi tìm thấy trên web đề cập đến các phiên bản cũ hơn và tôi hy vọng sẽ có một cách dễ dàng hơn hôm nay.

+2

Theo như tôi đã nghiên cứu bây giờ điều này sẽ không thể thực hiện trước khi ASP.NET 5 (vNext; MVC 6) và lưu trữ ứng dụng của tôi với nancy sẽ yêu cầu để di chuyển từ ASP.NET MVC đến Nancy (cũng có thể sử dụng công cụ tạo mẫu dao cạo). Chính xác? – ZoolWay

+2

Như trên, bạn không thể "tự lưu trữ" MVC, và Nancy là một framework phát triển web hoàn toàn khác với MVC - nó không phải là một cách thay thế để lưu trữ MVC và bạn không thể kết hợp chúng lại với nhau như thế. –

+0

Câu hỏi đặt ra là nếu có một cái gì đó khác để tự lưu trữ đó hoặc nếu MVC5 chỉ cần yêu cầu lưu trữ IIS mà không có bất kỳ giải pháp nào. – ZoolWay

Trả lời

12

Vì ASP.NET vNext chưa có sẵn và ứng dụng của tôi sử dụng MVC5, tôi sẽ phải di chuyển ứng dụng MVC hoàn toàn sang Nancy hoặc một cái gì đó tương tự. MVC5 quá phụ thuộc vào IIS.

Để giải quyết đồng thời này, tôi quyết định chọn một giải pháp trung gian như hiệu suất không phải là một vấn đề:

console Ứng dụng của tôi tạo ra một tập tin IIS cấu hình và khởi chạy một nhanh IIS:

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

Nếu ai đó muốn , đây là một phần của đoạn "dừng":

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

Để dừng quá trình bình thường, bạn nên gửi WM_QUIT cho nó. Điều này có thể hữu ích cho việc này:

/// <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

Vâng, tại sao không cũng chuyển hướng stdinput và gửi 'Q' để ngăn chặn iis gracefully. Hơn bạn có thể chờ HasExited và vẫn dừng quá trình nếu thất bại. –

Các vấn đề liên quan