首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ASP.NET MVC应用程序的自托管

ASP.NET MVC应用程序的自托管
EN

Stack Overflow用户
提问于 2014-12-01 19:02:02
回答 1查看 12.8K关注 0票数 19

我有一个完整工作的ASP.NET MVC应用程序(由5个程序集组成,.NET 4.5.1,ASP.NET MVC5.2.2),它在Visual Studio (使用IISExpress)中运行良好。

我现在希望有一个控制台应用程序,它接受MVC应用程序并托管它(自托管)。

我尝试使用Microsoft.Owin.Host.HttpListenerNancy.Owin,但是虽然我有404个页面,但我的配置缺乏到我的MVC-app的映射。

我得到了

代码语言:javascript
复制
public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseNancy();
    }
}

代码语言:javascript
复制
    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();
        }
    }

但显然,在运行的MVC应用程序中缺少使用MyMvcApplication的配置。如何做到这一点?或者如何进行自我托管?

我在网上找到的答案指的是更老的版本,我希望今天能有一个更简单的方法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-04 04:25:14

由于MVC vNext还不可用,而我的应用程序使用的是MVC5,因此我必须将MVC应用程序完全迁移到Nancy或类似的版本。MVC5过于依赖IIS。

为了解决这个问题,我决定采用中间解决方案,因为性能不是问题:

我的控制台应用程序创建一个IIS配置文件,并启动一个IIS express:

代码语言:javascript
复制
        // 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();

如果有人愿意,这是"stop“片段的一部分:

代码语言:javascript
复制
        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();

要以普通方式停止iis进程,应向其发送WM_QUIT。这可能会对此有所帮助:

代码语言:javascript
复制
    /// <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);
    }
票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27226954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档