有谁用过System.Web.Hosting,使aspx,asmx页面脱离IIS来运行,高手来,顶者有分
现在有个项目要求做一个独立的服务器,也就是让页面脱离IIS独立运行,在MSDN上面有相应的例程,可是居然编译不过去,郁闷ing,请有相关经验的大侠来指点一下,分不够可以再家,例程里面用的是HttpListener,如果哪位有利用TcpListener来做监听的更好,分不够的话我开贴再加
MSDN上的例子:http://www.microsoft.com/china/MSDN/library/WebServices/WebServices/ServiceStation.mspx?mfr=true
几个示例代码:
http://msdn.microsoft.com/msdnmag/issues/04/12/ServiceStation/default.aspx?fig=true#fig2
问题点数:100、回复次数:19Top
1 楼net_lover(【孟子E章】)回复于 2006-07-31 22:56:41 得分 60
完整例子和编译方法
http://radio.weblogs.com/0105476/stories/2002/10/24/executingAsmxFilesWithoutAWebServer.html
http://radio.weblogs.com/0105476/stories/2002/07/12/executingAspxPagesWithoutAWebServer.htmlTop
2 楼siugwan(开怀)回复于 2006-07-31 23:14:08 得分 4
markTop
3 楼fcuandy(了此残生.)回复于 2006-07-31 23:20:20 得分 4
markTop
4 楼leonbingo(libin)回复于 2006-07-31 23:54:51 得分 0
先谢谢,希望还能有人多给点例子Top
5 楼Zine_Alone(☆小飞☆)回复于 2006-08-01 02:31:06 得分 4
帮顶!Top
6 楼leonbingo(libin)回复于 2006-08-01 08:53:36 得分 0
没有人了么,faint,呵呵Top
7 楼zhengjob()回复于 2006-08-01 08:57:52 得分 3
markTop
8 楼ZJguhong()回复于 2006-08-01 09:02:55 得分 3
upTop
9 楼Radar2006(中华英雄)回复于 2006-08-01 09:36:13 得分 3
upTop
10 楼leonbingo(libin)回复于 2006-08-07 00:31:46 得分 0
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Hosting;
using System.Xml;
namespace MyHttpServer
{
public class HttpServer:MarshalByRefObject
{
public enum HostInfo { VitualDirectory, Port }
public static TcpListener tcpListener;
public HttpServer()
{
try
{
tcpListener = new TcpListener(int.Parse(GetHostInfo(HostInfo.Port)));
tcpListener.Start();
Thread thread = new Thread(new ThreadStart(StartListen));
thread.Start();
}
catch
{
}
}
public string GetHostInfo(HostInfo Info)
{
string rValue = "";
string xPath = "";
if (Info.Equals( HostInfo.VitualDirectory))
{
xPath = "HostSetting/VirtualDir";
}
else if (Info.Equals( HostInfo.Port))
{
xPath = "HostSetting/Port";
}
else
{
return "";
}
try
{
XmlDocument xd = new XmlDocument();
xd.Load(AppDomain.CurrentDomain.BaseDirectory + "HostSetting.xml");
XmlNode xn = xd.SelectSingleNode(xPath);
rValue = xn.InnerText;
return rValue;
}
catch
{
throw;
}
}
public string GetDefaultPage(string localDirectory)
{
try
{
XmlDocument xd = new XmlDocument();
xd.Load(AppDomain.CurrentDomain.BaseDirectory + "HostSetting.xml");
string xPagePath = "HostSetting/DefaultPage";
if (!localDirectory.EndsWith("\\"))
{
localDirectory += "\\";
}
foreach (XmlNode xn in xd.SelectSingleNode(xPagePath))
{
if (File.Exists(localDirectory + xn.InnerText))
{
return "\\"+xn.InnerText;
}
}
return "";
}
catch
{
throw;
}
}
public string GetMimeType(string requestFile)
{
string mimeType = "";
string fileExt = GetFileExt(requestFile);
try
{
string xPath = "HostSetting/Mime/Values/Type[../Ext='" + fileExt + "']";
XmlDocument xd = new XmlDocument();
xd.Load(AppDomain.CurrentDomain.BaseDirectory + "HostSetting.xml");
XmlNode mimeNode = xd.SelectSingleNode(xPath);
if (mimeNode != null)
{
mimeType = mimeNode.InnerText;
return mimeType;
}
else
{
return "";
}
}
catch
{
throw;
}
}
private string GetFileExt(string requestFile)
{
string fileExt = "";
int startPos = requestFile.IndexOf(".");
int endPos = requestFile.IndexOf("?");
if (endPos != -1)
{
fileExt = requestFile.Substring(startPos, endPos - startPos);
}
else
{
fileExt = requestFile.Substring(startPos);
}
fileExt = fileExt.ToLower();
return fileExt;
}
public void WriteHttpHeader(string httpVersion, string mimeHeader, int totalBytes, string statusCode, ref Socket mySocket)
{
string buffer = "";
if (mimeHeader.Length == 0)
{
mimeHeader = "text/html";
}
buffer += httpVersion + statusCode + "\r\n";
buffer += "Server:MyServer\r\n";
buffer += "Content-Type:" + mimeHeader + "\r\n";
buffer += "Acccept-Ranges:bytes\r\n";
buffer += "Content-Length:" + totalBytes + "\r\n\r\n";
SendToBrowser(Encoding.ASCII.GetBytes(buffer),ref mySocket);
}
public void SendToBrowser(byte[] sendDate, ref Socket mySocket)
{
try
{
if (mySocket.Connected)
{
if (mySocket.Send(sendDate) == -1)
{
}
}
else
{
}
}
catch
{
throw;
}
}
public void SendToBrowser(string sendDate, ref Socket mySocket)
{
SendToBrowser(Encoding.ASCII.GetBytes(sendDate), ref mySocket);
}
Top
11 楼leonbingo(libin)回复于 2006-08-07 00:32:27 得分 0
public void StartListen()
{
while (true)
{
int startPos = 0;
string request = "";
string dirName = "";
string requestFile = "";
string errorMessage = "";
string localDir = "";
string rootDir = GetHostInfo(HostInfo.VitualDirectory);
Socket socket = tcpListener.AcceptSocket();
if (socket.Connected)
{
byte[] receive = new byte[1024];
socket.Receive(receive);
string buffer=Encoding.ASCII.GetString(receive);
startPos = buffer.IndexOf("HTTP", 1);
string httpVersion = buffer.Substring(startPos, 8);
request = buffer.Substring(0, startPos - 1).Replace("\\", "/");
if (request.IndexOf(".") < 0 && !request.EndsWith("/"))
{
request += "/";
}
startPos = request.LastIndexOf("/") + 1;
requestFile = request.Substring(startPos);
dirName=request.Substring(request.IndexOf("/"),request.LastIndexOf("/")-3);
if (dirName == "/")
{
localDir += rootDir;
}
else
{
dirName = dirName.Replace("/", "\\");
localDir += rootDir + dirName;
}
//Find default page
if (requestFile.Length == 0)
{
requestFile = GetDefaultPage(localDir);
//Can not find the default page
if (requestFile == "")
{
NotFindPage(ref socket, httpVersion);
}
else//Find the default page
{
HandleRequest(dirName + requestFile, httpVersion, ref socket);
}
}
else//Find the request file
{
HandleRequest(dirName+requestFile, httpVersion, ref socket);
}
}
socket.Close();
}
}
private void NotFindPage(ref Socket socket, string httpVersion)
{
string errorMessage = "<iframe src=\"res://C:\\WINDOWS\\system32\\shdoclc.dll/dnserror.htm\" width=\"100%\" height=\"100%\">";
WriteHttpHeader(httpVersion, "text/html", errorMessage.Length, "404 Not Found", ref socket);
SendToBrowser(errorMessage, ref socket);
}
private void HandleRequest(string fileName,string httpVersion, ref Socket socket)
{
string virtualDir = GetHostInfo(HostInfo.VitualDirectory);
string fileExt = GetFileExt(fileName);
string mimiType = GetMimeType(fileName);
byte[] content=null;
if (fileExt == ".aspx" || fileExt == ".asmx")
{
MyHost myHost = (MyHost)ApplicationHost.CreateApplicationHost(typeof(MyHost), "/", virtualDir);
//MyHost myHost = new MyHost();
content = Encoding.ASCII.GetBytes(myHost.HandleRequest(virtualDir + fileName));
}
else
{
if (File.Exists(virtualDir + fileName))
{
FileStream fs = new FileStream(virtualDir + fileName, FileMode.Open);
content = new byte[fs.Length];
fs.Read(content, 0, content.Length);
fs.Close();
}
else
{
NotFindPage(ref socket, httpVersion);
}
}
WriteHttpHeader(httpVersion, mimiType, content.Length, "200 OK", ref socket);
SendToBrowser(content, ref socket);
}
~HttpServer()
{
tcpListener.Stop();
}
}
public class MyHost : MarshalByRefObject
{
public string HandleRequest(string fileName)
{
StringWriter wr = new StringWriter();
HttpWorkerRequest worker = new SimpleWorkerRequest(fileName, "", wr);
HttpRuntime.ProcessRequest(worker);
return wr.ToString();
}
}
}
Top
12 楼C5662601(你学的越多 你忘的越多 你学的越少 你忘的越少)回复于 2006-08-07 00:41:26 得分 3
学习Top
13 楼leonbingo(libin)回复于 2006-08-07 01:17:26 得分 0
<?xml version="1.0" encoding="utf-8" ?>
<HostSetting>
<VirtualDir>c:\temp</VirtualDir>
<Port>8001</Port>
<DefaultPage>
<File>default.htm</File>
<File>default.html</File>
<File>default.aspx</File>
<File>default.asmx</File>
</DefaultPage>
<Mime>
<Values>
<Ext>.htm</Ext>
<Type>text/html</Type>
</Values>
<Values>
<Ext>.html</Ext>
<Type>text/html</Type>
</Values>
<Values>
<Ext>.aspx</Ext>
<Type>text/html</Type>
</Values>
<Values>
<Ext>.asmx</Ext>
<Type>text/html</Type>
</Values>
<Values>
<Ext>.gif</Ext>
<Type>image/gif</Type>
</Values>
<Values>
<Ext>.jpg</Ext>
<Type>image/jpg</Type>
</Values>
</Mime>
</HostSetting>Top
14 楼zlkingdom(风之悲伤)回复于 2006-08-07 09:23:33 得分 3
学习了~~我还没试过不用IIS运行网站Top
15 楼ji_yi_m(無盡の華爾茲)回复于 2006-08-07 10:43:56 得分 3
帮顶顺便MARKTop
16 楼Radar2006(中华英雄)回复于 2006-08-07 11:29:38 得分 3
upTop
17 楼leonbingo(libin)回复于 2006-08-13 21:39:01 得分 0
谁能重写一个HttpWorkRequest对象?Top
18 楼terry_12(大撒发射点)回复于 2006-08-13 21:49:28 得分 3
markTop
19 楼Nov04(当欲望得到满足,灵魂才重新拾起前进的方向)回复于 2006-08-14 00:24:27 得分 4
帮你顶Top





