• CassiniDev源码学习


    最近一个项目是将web版的程序,改为单机版。话说这个web版号称当年十几个人用了至少3个月的时间开发,后来三年还不断有修改,而现在要在1个月内由一个人完成,这简直是不可能完成的任务!直觉告诉我,重写肯定不是办法,还好有朋友用过Cassini http://cassinidev.codeplex.com (可替代IIS的单机Web Form解决方案)立即投入测试,可用;有源码,不担心出太大问题。

    现在项目结束了,看了一遍CassiniDev,它的基本思路是:

    1.新建socket,绑定并监听本机上一个可用的端口。

    2.对新建连接(新的请求)创建新的socket。

         public void Start()
            {
                _socket = CreateSocketBindAndListen(AddressFamily.InterNetwork, _ipAddress, _port);
    
                //start the timer
                //DecrementRequestCount();
    
                ThreadPool.QueueUserWorkItem(delegate
                    {
                        while (!_shutdownInProgress)
                        {
                            try
                            {
                                Socket acceptedSocket = _socket.Accept();
    
                                ThreadPool.QueueUserWorkItem(delegate
                                    {
                                        if (!_shutdownInProgress)
                                        {
                                            Connection conn = new Connection(this, acceptedSocket);
    
                                            if (conn.WaitForRequestBytes() == 0)
                                            {
                                                conn.WriteErrorAndClose(400);
                                                return;
                                            }
    
                                            Host host = GetHost();
    
                                            if (host == null)
                                            {
                                                conn.WriteErrorAndClose(500);
                                                return;
                                            }
    
                                            //IncrementRequestCount();
                                            host.ProcessRequest(conn);
                                        }
                                    });
                            }
                            catch
                            {
                                Thread.Sleep(100);
                            }
                        }
                    });
            }

    3.通过一个微软的内部类“System.Web.Compilation.BuildManagerHost”(未对外公布,MSDN查不到),生成Host实例。

         /// <remarks>
            /// This is Dmitry's hack to enable running outside of GAC.
            /// There are some errors being thrown when running in proc
            /// </remarks>
            private object CreateWorkerAppDomainWithHost(string virtualPath, string physicalPath, Type hostType,int port)
            {
    
    
                // create BuildManagerHost in the worker app domain
                //ApplicationManager appManager = ApplicationManager.GetApplicationManager();
                Type buildManagerHostType = typeof(HttpRuntime).Assembly.GetType("System.Web.Compilation.BuildManagerHost");
                IRegisteredObject buildManagerHost = ApplicationManager.CreateObject(_appId, buildManagerHostType, virtualPath,
                                                                              physicalPath, false);
    
                // call BuildManagerHost.RegisterAssembly to make Host type loadable in the worker app domain
                buildManagerHostType.InvokeMember("RegisterAssembly",
                                                  BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.NonPublic,
                                                  null,
                                                  buildManagerHost,
                                                  new object[] { hostType.Assembly.FullName, hostType.Assembly.Location });
    
                // create Host in the worker app domain
                // FIXME: getting FileLoadException Could not load file or assembly 'WebDev.WebServer20, Version=4.0.1.6, Culture=neutral, PublicKeyToken=f7f6e0b4240c7c27' or one of its dependencies. Failed to grant permission to execute. (Exception from HRESULT: 0x80131418)
                // when running dnoa 3.4 samples - webdev is registering trust somewhere that we are not
                return ApplicationManager.CreateObject(_appId, hostType, virtualPath, physicalPath, false);
            }

    4.继承“SimpleWorkerRequest”类(它是System.Web.HttpWorkerRequest 抽象类的简单实现,该抽象类可用于在 Internet 信息服务 (IIS) 应用程序之外承载ASP.NET 应用程序),写一个自己的Request类。

    5.由HttpRuntime驱动所有 ASP.NET Web 处理执行。

    HttpRuntime.ProcessRequest(this);

    这个开源组件确实不错,好用,稳定,在winxp, vista, win7, win8都可用。但有一个性能上的不足,在创建微软内部类“System.Web.Compilation.BuildManagerHost”实例时,耗时要5-6秒的时间,机器差一点的话,要9-20秒,目前暂无改进。

  • 相关阅读:
    《JAVA程序设计》第10周学习总结
    《JAVA程序设计》第9周学习总结
    《Java程序设计》第8周学习总结
    《Java程序设计》第7周学习总结
    《Java程序设计》第6周学习总结
    《Java程序设计》第5周学习总结
    《Java程序设计》第4周学习总结
    《Java程序设计》第3周学习总结
    《Java程序设计》第2周学习总结
    201521123044 《Java程序设计》第12周学习总结
  • 原文地址:https://www.cnblogs.com/MikeYao/p/3529776.html
Copyright © 2020-2023  润新知