实现一个可host asp.net程序的小型IIS(Cassinidev介绍)
2011-09-22 13:13
495 查看
引子
源于给客户做的一个系统,基于传统的c/s架构(wpf+wcf),后来因为客户需要,就写了一些基于Web的查询小工具,开发完了要部署的时候才发现,服务器上并没有装IIS。服务器是win2003server操作系统,硬盘上没保留系统安装文件的备份,要装IIS就得跑一趟机房,为做一个懒惰的程序员,就开始想方法找歪路啦。
Google之,得到以下信息:
Cassini——VS自带的ASP.NETDevelopmentServer的前身(
示例
下载:下载回来的压缩包里有很多的exe及dll文件,下面是每个文件的作用描述:
直接使用的:
CassiniDev.exe:对应.netframework3.5,asp.net2.0,windowformgui
CassiniDev4.exe:对应.netframework4,asp.net4.0,windowformgui
CassiniDev-console.exe:对应.netframework3.5,asp.net2.0,控制台程序
CassiniDev4-console.exe:对应.netframework4,asp.net4.0,控制台程序
供二次开发的:
CassiniDev-lib.dll:对应.netframework3.5,asp.net2.0
CassiniDev4-lib.dll:对应.netframework4,asp.net4.0
供VS调试用的(即用来替换掉VS自带的ASP.Netdevelopmentserver):
WebDev.WebServer.exe
WebDev.WebServer20.exe
WebDev.WebServer40.exe
[code]usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows;
usingSystem.Windows.Controls;
usingSystem.Windows.Data;
usingSystem.Windows.Documents;
usingSystem.Windows.Input;
usingSystem.Windows.Media;
usingSystem.Windows.Media.Imaging;
usingSystem.Windows.Navigation;
usingSystem.Windows.Shapes;
usingCassiniDev;
namespaceJKAspNetHost
{
///<summary>
///MainWindow.xaml的交互逻辑
///</summary>
publicpartialclassMainWindow:Window
{
privatereadonlyServerserver;
publicMainWindow()
{
InitializeComponent();
server=newServer(8038,"/",System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"WebApp"),
System.Net.IPAddress.Parse("0.0.0.0"),"",0,false,false);
server.Start();
textLog.AppendText("服务器运行中...\r\n");
textLog.AppendText(server.PhysicalPath+"\r\n");
textLog.AppendText(server.RootUrl+"\r\n");
server.RequestComplete+=newEventHandler<RequestEventArgs>(server_RequestComplete);
}
voidserver_RequestComplete(objectsender,RequestEventArgse)
{
//可能由ui线程以外的线程调用,所以需要Dispatcher.Invoke
Dispatcher.Invoke(newAction(()=>{
textLog.AppendText("Request:"+e.RequestLog.Url+"\r\n");
if(textLog.Text.Length>10000)
{
textLog.Text=textLog.Text.Substring(textLog.Text.Length-1000,1000);
}
textLog.ScrollToEnd();
}));
}
privatevoidWindow_Closing(objectsender,System.ComponentModel.CancelEventArgse)
{
try
{
server.ShutDown();
}
catch{}
}
}
}[/code]
其他用途
1,如果您需要做一些随光盘附带的演示程序或介绍程序,却又没有桌面应用开发经验,那么可以利用cassinidev+webbrowser的来实现,同一个exe中,既是服务端又是前端表示层,无须安装,开箱即用。
2,单机应用或小型局域网应用,利用强大的html作表示层,用户体验不比传统的MFC,windowFrom差,开发效率,界面美观。
3,。。。