您的位置:首页 > 编程语言 > ASP

实现一个可host asp.net程序的小型IIS(Cassinidev介绍)

2011-09-22 13:13 495 查看

引子

源于给客户做的一个系统,基于传统的c/s架构(wpf+wcf),后来因为客户需要,就写了一些基于Web的查询小工具,开发完了要部署的时候才发现,服务器上并没有装IIS。


服务器是win2003server操作系统,硬盘上没保留系统安装文件的备份,要装IIS就得跑一趟机房,为做一个懒惰的程序员,就开始想方法找歪路啦。

Google之,得到以下信息:

Cassini——VS自带的ASP.NETDevelopmentServer的前身(http://blogs.msdn.com/b/dmitryr/),可惜这玩意只支持本机连接,而且最高只支持到.netframework3.5,pass掉。IISExpress——微软打算用来替换掉ASP.NETDevelopmentServer的工具,不仅支持远程连接,.netframework4更是不在话下,比VS自带的server功能强大许多,相信很多vsweb开发者已经用iisexpress来替换掉vs自带的那个asp.netdevelopmentserver,可是对我这个web新手来说,这东西还是重量级了些,配置又好麻烦,懒得研究,pass掉。Cassinidev——就是本文的主角啦,这是一个开源项目(http://cassinidev.codeplex.com/),支持远程连接,支持.net3.5和4.0,可以源码级重用或程序集重用,绝对轻量级,部署方便。

示例

下载:

CassiniDev3.5.1.8-4.1.0.8release(官方下载)

下载回来的压缩包里有很多的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

使用:

一般情况下,直接使用上面的exe便可实现asp.net的寄宿了,但是相应的,可供我们控制的就比较少,不自由,例如实际应用中可能需要把我们的宿主写成windowsService的形式,直接用上面的exe就没法实现了,这种情况下可以利用cassinidev-lib进行二次开发,甚至是直接到官网上下载源代码来生成自己需要的程序,下面是一段简单的例子,利用cassinidve4-lib.dll实现一个asp.net的host

在vs2010中新建一wpf应用程序,在主窗口中加入一TextBox做显示信息用,修改MainWindow代码如下:

[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,。。。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐
章节导航