`
lh8489
  • 浏览: 51462 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

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

阅读更多

引子

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

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

Google之, 得到以下信息:

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

示例

    下载:

CassiniDev 3.5.1.8-4.1.0.8 release (官方下载)

下载回来的压缩包里有很多的exe及dll文件, 下面是每个文件的作用描述:

直接使用的:

CassiniDev.exe:对应.net framework 3.5, asp.net 2.0, windowform gui

CassiniDev4.exe: 对应.net framework 4, asp.net 4.0, windowform gui

CassiniDev-console.exe:对应.net framework 3.5, asp.net 2.0, 控制台程序

CassiniDev4-console.exe:对应.net framework 4, asp.net 4.0, 控制台程序

供二次开发的:

CassiniDev-lib.dll:对应.net framework 3.5, asp.net 2.0

CassiniDev4-lib.dll:对应.net framework 4, asp.net 4.0

供VS调试用的(即用来替换掉VS自带的ASP.Net development server):

WebDev.WebServer.exe

WebDev.WebServer20.exe

WebDev.WebServer40.exe

    使用:

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

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

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

using CassiniDev;

 

namespace JKAspNetHost

{

    /// <summary>

    /// MainWindow.xaml 的交互逻辑

    /// </summary>

    public partial class MainWindow : Window

    {

        private readonly Server server;

 

        public MainWindow()

        {

            InitializeComponent();

 

            server = new Server(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 += new EventHandler<RequestEventArgs>(server_RequestComplete);

        }

 

        void server_RequestComplete(object sender, RequestEventArgs e)

        {

            //可能由ui线程以外的线程调用, 所以需要Dispatcher.Invoke

            Dispatcher.Invoke(new Action(()=>{

                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();

            }));

        }

 

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)

        {

            try

            {

                server.ShutDown();

            }

            catch { }

        }

    }

}
 
分享到:
评论

相关推荐

    微软开源的SignalR .Net客户端实现ASP.NET SignalR.zip

    ASP.NET SignalR 是为 ASP.NET 开发人员提供的一个库,可以简化开发人员将实时 Web 功能添加到应用程序的过程。实时 Web 功能是指这样一种功能:当所连接的客户端变得可用时服务器代码可以立即向其推送内容,而不是...

    实现电子商务网站的.NET 应用程序 C#实现

    使用基于服务的体系结构实现电子商务网站的参考 .NET 应用程序。 (仅限 Windows)安装 Visual Studio。Visual Studio 包含你希望拥有的对 .NET Aspire 的工具支持。Visual Studio 2022 版本 17.9 预览版。 在安装...

    ASP.NET程序中常用的代码

    我们在用asp.net开发web程序的时候,经常会碰到获取url的时候,现在汇总如下便于下次查找: 假设现在有一个url地址:http://www.niurena.com/website/default.aspx, 结果如下: Request.Url.AbsoluteUri: ...

    【ASP.NET编程知识】ASP.NET WebAPi(selfhost)实现文件同步或异步上传.docx

    【ASP.NET编程知识】ASP.NET WebAPi(selfhost)实现文件同步或异步上传.docx

    Pro ASP.NET Core MVC, 6th Edition, 2016

    ASP.NET Core MVC provides a "host agnostic" framework and a high-productivity programming model that promotes cleaner code architecture, test-driven development, and powerful extensibility. ...

    Beginning ASP.NET 2.0 with C# (英文版)

    ASP.NET 2.0 is an amazing technology that allows you to develop web sites and applications with very little hassle, and its power and depth enable it to host even the most complex applications ...

    Pro ASP.NET Core MVC 2 7th Edition

    ASP.NET Core MVC provides a "host agnostic" framework and a high-productivity programming model that promotes cleaner code architecture, test-driven development, and powerful extensibility. ...

    用IIS建立的.net网站通过IP地址不能访问解决方法

    用iis建立一个.net的网站,在iis中绑定了服务器公网ip的80端口和127.0.0.1的80端口,结果用ip地址不能直接访问

    WopiHost:WOPI协议的ASP.NET Core MVC实现。 启用与WOPI客户端(例如Office Online Server)的集成

    WopiHost 包裹版本资料下载WopiHost.Abstractions WopiHost.Core WopiHost.Discovery WopiHost.FileSystemProvider WopiHost.Url 介绍该项目是WOPI主机的示例实现。 基本上,它允许开发人员通过实现一堆接口将自定义...

    Apress.Pro.ASP.NET.Core.MVC.2.7th.Edition

    ASP.NET Core MVC provides a "host agnostic" framework and a high-productivity programming model that promotes cleaner code architecture, test-driven development, and powerful extensibility. ...

    asp.net core host 启动过程.md

    asp.net core host 启动过程.md

    asp.net 网易微博 OAuth 2.0 实例源码

    asp.net 网易微博 OAuth 2.0 实例源码 1.需要在http://open.t.163.com/ 申请一个帐号 2.获取Appkey,AppSecret 3.需要配置 oauth.config 填入相应的Appkey,AppSecret,原来上面的是我自己乱填的,所以测试需要填你...

    欧姆龙PLC C#通讯程序源码(HOST LINK 实现)

    可实现功能: 1、串口通信、通信测试、状态读取、数据的读写等基本操作 2、PLC工作模式设定 3、读写DM数据区 4、置位和复位IR区,读相应位的状态 5、校验实现:HOST LINK校验方式 三、注意事项 1、开发环境为...

    asp.net mvc

    A new property is available to T4 files from the ASP.NET MVC T4 host that specifies the version of the .NET Framework that is used by the application. This allows T4 templates to generate code and ...

    FreeHost SQL2000网页管理器asp.net版

    为了方便用户在网页上管理SQL2000,请用平台开通一个支持ASP.net的虚拟主机,将所有的文件复制到虚拟主机中,就可以使用。 请注意,所有文件必须放在网站的根目录下,不能放在子目录中。

    c#、.NET6实现websocket协议服务端demo,完整清晰的解析和打包示例

    c#、.NET6实现websocket协议demo,完整清晰的解析和打包示例 包含请求头解析、数据帧解析、数据帧掩码处理、数据帧打包

    Pro ASP.NET SignalR(Apress,2014)

    ASP.NET SignalR is the new solution to real-time communication between servers and clients in .NET. Use it to push new data to a web page or mobile device as soon as it becomes available, whether it's...

    dotnet-hosting-3.1.30-win.exe 运行.net core3.1程序必备环境

    ASP.NET Core 托管捆绑包包括 .NET Core 运行时和 ASP.NET Core 运行时。如果在装有 IIS 的计算机上安装,它还会添加 ASP.NET Core IIS 模块.

    用Owin Host实现脱离IIS跑Web API单元测试

    在开发一个ASP.NET Web API项目写单元测试时,实在无法忍受之前的笨方法,决定改过自新。  之前Web API的单元测试需要进行以下的操作:  初始配置:  1)在IIS中创建一个站点指定Web API项目  2)在hosts...

    ASP.NET 单点登录

    VS2010编写的单点登陆源码,建议修改HOST文件部署成多个站点域名测试,文件中包括一个服务器和3个子站点, 单点登陆参考是的是京东的做法,在各自客户端通过脚本然后写入其他站点的cookie,而没有使用POST多站点的...

Global site tag (gtag.js) - Google Analytics