Asp. NET a simple webserver, simulating IIS with a console program to hold up Web services

Source: Internet
Author: User

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>

     processrequesthandler ( page,  query, textwriter writer) ;      webserver : marshalbyrefobject, iregisteredobject     {          processrequest ( page,   query, textwriter writer)         {             SimpleWorkerRequest worker =   SimpleWorkerRequest (Page, query, writer);             httpruntime.processrequest (worker);        }           stop ( immediate)          {             Hostingenvironment.unregisterobject ();         }    }       simplehttplistener    {         processrequesthandler processrequest;           Setprocessrequest (Processrequesthandler handler)         {             . processrequest = handler;        }           physicroot = . Empty;         public void run ()          {            if   (! httplistener.issupported)                  throw new invalidoPerationexception ("Use  HttpListener  must be  Windows XP SP2  or  server 2003   ABOVE system! ");            string[] prefixes =  new string[] {  "http://localhost:49152/"  };             httplistener listener = new httplistener ();             foreach  (string item in  Prefixes)             {                 listener. Prefixes.add (item);            }             listener. Start ();             console.writeline ("Start listening ...");            while  (True)              {                 httplistenercontext context = listener. GetContext ();                 Httplistenerrequest request = context. request;                 Console.WriteLine ("{0} {1} http/1.1",  request. Httpmethod, request. RAWURL);                 Httplistenerresponse response = context. Response;                using   (streamwriter writer = new sTreamwriter (response. OutputStream))                  {                     stringwriter sw = new stringwriter ();                     string content =  string. empty;                     string filename = request. Url.LocalPath.ToLower ();                     regex reg = new regex ("(. aspx|. Ashx|. svc|. ASMX);                     match m = reg. MaTCH (filename);                     if  (m.success)                      {                         string path  = request. url.absolutepath;                         string query = request. Url.Query.Replace ("?",  string. Empty);                         string prefx = m.groups[0]. value;                         if  (!filename. EndsWith (PREFX))                          {                              int index = path. IndexOf (PREFX);                             index = index +  prefx. length;                             query = path. Substring (Index);                         &nbsP;   path = path. Substring (0, index);                         }                         path =  path.getfilename (Path);                         this. ProcessRequest (PATH, QUERY, SW);                         CONTENT = SW. ToString ();                         SW. Close ();              &nBsp;          response. Contentlength64 = encoding.utf8.getbytecount (content);                         response . contenttype =  "Text/html; charset=utf-8";                         writer. Write (content);                     }                     else                     {                     &nBsp;   string filepath = path.combine (physicroot, request. Url.AbsolutePath.Substring (1));                         lock  (This)                           {                             byte[] buffer = new  byte[4096];                             if  (File.exists (FilePath))                              {                                 using   (Streamreader sr = new streamreader (FilePath))                                   {                                      response. CONTENTLENGTH64 = SR. basestream.length;                                      int readLength = Sr. Basestream.read (Buffer, 0, buffer. Length);                                      while  (readlength > 0)                                       {                                          writer. Basestream.write (buffer, 0, readlength);                                          READLENGTH = SR. Basestream.read (Buffer, 0, buffer. Length);                                      }                                 }                              }                             else                             {                                  string msg =  "Not found:"  + filePath;                                  byte[] data =  encoding.utf8.getbytes (msg);                                  writer. Basestream.write (Data, 0, data. Length);                             }                         }                     }                     console.writeline ("\r\nprocess ok.\r\n");                 }                 if  (console.keyavailable)                       break;            }             listener. Stop ();         }   &nbSp;       start ()         {             PhysicRoot = ;             WebServer server =  Createworkerappdomainwithhost ((WebServer),  , physicroot)   WebServer;                          simplehttplistener listener =  simplehttplistener ();             listener. Setprocessrequest (server. ProcessRequest);             listener. Run ();        }           createworkerappdomainwithhost (TYPE HOSTTYPE,  VIRTUALPATH,&NBsp; physicalpath)         {              uniqueappstring = . Concat (Virtualpath, physicalpath). Tolowerinvariant ();              appid =   (Uniqueappstring.gethashcode ()). ToString (,  cultureinfo.invariantculture);              appmanager = applicationmanager.getapplicationmanager ();              buildManagerHostType =  (HttpRuntime). Assembly.GetType ();              Buildmanagerhost = appmanager. CreateObject (appid, buildmanagerhosttype, virtualpath, physicalpath, );             buildmanagerhosttype. InvokeMember (, bindingflags.instance | bindingflags.invokemethod |  Bindingflags.nonpublic, , buildmanagerhost,  [] { hosttype.assembly.fullname,  hosttype.assembly.location });              appmanager. CreateObject (appid, hosttype, virtualpath, physicalpath, );         }    }

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>


Asp. NET a simple webserver, simulating IIS with a console program to hold up Web services

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.