有谁用过System.Web.Hosting,使aspx,asmx页面脱离IIS来运行,高手来,顶者有分

leonbingo 2006-07-31 10:49:34
现在有个项目要求做一个独立的服务器,也就是让页面脱离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
...全文
459 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
Nov04 2006-08-14
  • 打赏
  • 举报
回复
帮你顶
terry_12 2006-08-13
  • 打赏
  • 举报
回复
mark
leonbingo 2006-08-13
  • 打赏
  • 举报
回复
谁能重写一个HttpWorkRequest对象?
C5662601 2006-08-07
  • 打赏
  • 举报
回复
学习
leonbingo 2006-08-07
  • 打赏
  • 举报
回复
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();
}
}
}
leonbingo 2006-08-07
  • 打赏
  • 举报
回复
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);
}


Radar2006 2006-08-07
  • 打赏
  • 举报
回复
up
华尔兹 2006-08-07
  • 打赏
  • 举报
回复
帮顶顺便MARK
zlkingdom 2006-08-07
  • 打赏
  • 举报
回复
学习了~~我还没试过不用IIS运行网站
leonbingo 2006-08-07
  • 打赏
  • 举报
回复
<?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>
Radar2006 2006-08-01
  • 打赏
  • 举报
回复
up
ZJguhong 2006-08-01
  • 打赏
  • 举报
回复
up
zhengjob 2006-08-01
  • 打赏
  • 举报
回复
mark
leonbingo 2006-08-01
  • 打赏
  • 举报
回复
没有人了么,faint,呵呵
Zine_Alone 2006-08-01
  • 打赏
  • 举报
回复
帮顶!
leonbingo 2006-07-31
  • 打赏
  • 举报
回复
先谢谢,希望还能有人多给点例子
fcuandy 2006-07-31
  • 打赏
  • 举报
回复
mark
siugwan 2006-07-31
  • 打赏
  • 举报
回复
mark
孟子E章 2006-07-31
  • 打赏
  • 举报
回复
完整例子和编译方法
http://radio.weblogs.com/0105476/stories/2002/10/24/executingAsmxFilesWithoutAWebServer.html
http://radio.weblogs.com/0105476/stories/2002/07/12/executingAspxPagesWithoutAWebServer.html

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧