当前位置: 首页 > 编程语言 > C#

自托管ASP.NETMVC应用分享

时间:2023-04-10 18:40:29 C#

自托管ASP.NETMVC应用我有一个完整的ASP.NETMVC应用(由5个程序集组成,.NET4.5.1,ASP.NETMVC5.2.2),它在VisualStudio中运行良好(使用IISExpress)。我现在想要一个控制台应用程序,它采用MVC应用程序并托管它(自托管)。我尝试使用Microsoft.Owin.Host.HttpListener和Nancy.Owin,但是当我收到404页面时,我的配置缺少到我的MVC应用程序的映射。我有publicclassStartup{publicvoidConfiguration(IAppBuilderapp){app.UseNancy();}}和staticvoidMain(string[]args){StartOptionsso=newStartOptions("http://localhost:9000/");使用(WebApp.Start(so)){Console.WriteLine("PressEntertoExit");控制台.ReadLine();但显然MyMvcApplication使用来自正在运行的MVC应用程序的MyMvcApplication的配置。怎么做?或者自我托管怎么样?我在网上找到的答案是指旧版本,我希望今天有更简单的方法。由于ASP.NETvNext尚不可用并且我的应用程序使用MVC5,我必须将MVC应用程序完全迁移到Nancy或类似的东西。MVC5过于依赖IIS。为了解决这个问题,我决定采用中间解决方案,因为性能不是问题:我的控制台应用程序创建IIS配置文件并启动IISexpress://startIISboolsystray=Debugger.IsAttached;ProcessStartInfopsi=newProcessStartInfo(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)thrownewNotSupportedException("不支持多次启动");this.iisProcess=newProcess();这。iisProcess.StartInfo=psi;this.iisProcess.ErrorDataReceived+=OnErrorDataReceived;this.iisProcess.OutputDataReceived+=OnOutputDataReceived;this.iisProcess.Start();this.iisProcess.BeginErrorReadLine();想一想,这是“停止”片段的一部分:如果(这个.iisProcess.HasExited){log.WarnFormat("IIS已经退出,代码为'{0}'",this.iisProcess.ExitCode);this.iisProcess.Close();返回;}log.InfoFormat("正在停止IIS实例#{0}",this.instanceId);ProcessCommunication.SendStopMessageToProcess(this.iisProcess.Id);布尔退出=this.iisProcess.WaitForExit(30000);if(!exited){log.WarnFormat("无法停止IIS实例#{0}(PID{1}),现在终止它",this.instanceId,this.iisProcess.Id);this.iisProcess.Kill();}this.iisProcess.Close();要使iis进程保持通用,你应该向它发送WM_QUIT这可能对此有所帮助:以上就是《C#学习教程:ASP.NETMVC应用自托管分享》的全部内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注—/////向另一个进程发送WM_QUIT消息。//////其他进程的PIDpublicstaticvoidSendStopMessageToProcess(intpid){log.DebugFormat("SendingstopmessagetoPID#{0}",pid);try{for(IntPtrptr=NativeMethods.GetTopWindow(IntPtr.Zero);ptr!=IntPtr.Zero;ptr=NativeMethods.GetWindow(ptr,2)){uintnum;本机方法。GetWindowThreadProcessId(ptr,outnum);if(pid==num){HandleRefhWnd=newHandleRef(null,ptr);NativeMethods.PostMessage(hWnd,0x12,IntPtr.Zero,IntPtr.Zero);返回;}}}catch(ArgumentExceptionex){log.Error(String.Format("FailedtosendWM_QUITtoPID#{0}",pid),ex);}}//////提供向其他窗口进程发布消息的本机方法。///内部类NativeMethods{//方法[DllImport("user32.dll",SetLastError=true)]internalstaticexternIntPtrGetTopWindow(IntPtrhWnd);[DllImport("user32.dll",SetLastError=true)]internalstaticexternIntPtrGetWindow(IntPtrhWnd,uintuCmd);[DllImport("user32.dll",SetLastError=true)]internalstaticexternuintGetWindowThreadProcessId(IntPtrhwnd,outuintlpdwProcessId);[DllImport("user32.dll",SetLastError=true)]internalstaticexternboolPostMessage(HandleRefhWnd,uintMsg,IntPtrwParam,IntPtrlParam);}本文收集自网络,不代表立场,如涉及侵权,请点击右侧联系管理员删除,如需转载请注明出处: