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);
}
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
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ế. –
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