Разработка распределенных приложений в Microsoft.NET Framework


Пример II1


// SevaAspHost.cs

using System;

using System.IO;

using System.NET;

using System.Web;

using System.Web.Hosting;

using System.Text.RegularExpressions;

namespace Seva.AspHost

{

public class AspHost: MarshalByRefObject

{

private HttpListener listener;

private string virtualDir;

private string hostingDir;

public string VirtualDir

{

get {return virtualDir;}

}

public string HostingDir

{

get {return hostingDir;}

}

public static AspHost CreateHost(string[] prefixes, string aspVirtualDir,

string aspHostingDir)

{

if (!HttpListener.IsSupported)

{

throw new NotSupportedException(

"Требуется Windows XP SP2/Server 2003.");

}

AspHost host = (AspHost) ApplicationHost.CreateApplicationHost(

typeof(AspHost), aspVirtualDir, aspHostingDir);

host.Init(prefixes, aspVirtualDir, aspHostingDir);

return host;

}

public void Init(string[] prefixes, string aspVirtualDir,

string aspHostingDir)

{

virtualDir = aspVirtualDir;

hostingDir = aspHostingDir;

listener = new HttpListener();

foreach (string prefix in prefixes)

listener.Prefixes.Add(prefix);

}

public void Start()

{

listener.Start();

}

public void Stop()

{

listener.Stop();

}

public void ProcessRequest()

{

HttpListenerContext context = listener.GetContext();

HttpListenerWorkerRequest workerRequest =

new HttpListenerWorkerRequest(context, this);

HttpRuntime.ProcessRequest(workerRequest);

}

}

public class HttpListenerWorkerRequest: HttpWorkerRequest

{

private HttpListenerContext context;

private AspHost host;

public HttpListenerWorkerRequest(HttpListenerContext listenerContext,

AspHost aspHost)

{

context = listenerContext;

host = aspHost;

}

public override void EndOfRequest()

{

context.Response.OutputStream.Close();

context.Response.Close();

}

public override void FlushResponse(bool finalFlush)

{

context.Response.OutputStream.Flush();

}

public override string GetHttpVerbName()

{

return context.Request.HttpMethod;

}

public override string GetHttpVersion()




Начало    Вперед



Книжный магазин