zoukankan      html  css  js  c++  java
  • 实现一个可host asp.net程序的小型IIS(Cassinidev介绍)

    http://www.cnblogs.com/sweethome/archive/2011/09/22/2184988.html

    引子

        源于给客户做的一个系统, 基于传统的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 { }
            }
        }
    }

    其他用途

    1,如果您需要做一些随光盘附带的演示程序或介绍程序, 却又没有桌面应用开发经验, 那么可以利用cassinidev+webbrowser的来实现,  同一个exe中, 既是服务端又是前端表示层, 无须安装, 开箱即用。

    2,单机应用或小型局域网应用, 利用强大的html作表示层,用户体验不比传统的MFC, windowFrom差, 开发效率, 界面美观。

    3, 。。。

    分类: web

     http://cassinidev.codeplex.com/

  • 相关阅读:
    【模板】Sparse-Table
    UVa 11235 Frequent values
    【模板】树状数组
    UVa 1428 Ping pong
    数学技巧
    UVa 11300 Spreading the Wealth
    UVa 11729 Commando War
    UVa 11292 Dragon of Loowater
    POJ 3627 Bookshelf
    POJ 1056 IMMEDIATE DECODABILITY
  • 原文地址:https://www.cnblogs.com/chulia20002001/p/2459164.html
Copyright © 2011-2022 走看看