B/S C/S在WinForm程式中嵌入ASP.NET WebForm應用程式
現 在的流行趨勢是桌面程式Web化,Web程式桌面化,呵呵。最終目标就是你中有我,我中有你。例如MSN Explorer就是一個很好的展示,讓使用者在 使用的時候分不清什麼時候是在本地什麼時候是在網絡。而這類程式往往需要有一個背景伺服器如IIS的支援,這對大多數桌面應用來說too heavy了。 本着簡單就是美的設計思想,這裡給出一個輕量級的解決方法,把ASP.NET嵌入到普通WinForm桌面程式中去。
因為安全以及其它一些方面的原因,在使用ASP.NET引擎之前,必須建立一個新的AppDomain。簡單的方法是直接使用ApplicationHost.CreateApplicationHost函數為指定的虛拟目錄和實體路徑建立ASP.NET引擎宿主的執行個體,
如 :
// should create a subdirectory ./bin and copy the assembly to it
static public WebHost Create( string name, string path)
{
if ( ! name.StartsWith( new string (Path.AltDirectorySeparatorChar, 1 )))
{
name = Path.AltDirectorySeparatorChar + name;
}
WebHost host = (WebHost)ApplicationHost.CreateApplicationHost(
typeof (WebHost), name, path);
host.setVirtualDirectory(name);
host.setBaseDirectory(path);
return host;
}
但這樣建立的程式有個BT的要求,他會在指定目錄的bin子目錄中去嘗試載入宿主類型(WebHost)的assembly,也就是說你必須把程式在bin子目錄下複制一份,非常不爽。解決方法是自己手工完成整個建立過程,如下:
static public WebHost Create( string virtualDir, string physicalDir)
{
if ( ! virtualDir.StartsWith( new string (Path.AltDirectorySeparatorChar, 1 )))
{
virtualDir = Path.AltDirectorySeparatorChar + virtualDir;
}
if ( ! physicalDir.EndsWith( new string (Path.DirectorySeparatorChar, 1 )))
{
physicalDir += Path.DirectorySeparatorChar;
}
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationName = " APP_ " + Guid.NewGuid().ToString();
setup.ConfigurationFile = " web.config " ;
AppDomain domain = AppDomain.CreateDomain( " ASPHOST_ " + Guid.NewGuid().ToString(), null , setup);
domain.SetData( " .appDomain " , " * " );
domain.SetData( " .appPath " , physicalDir);
domain.SetData( " .appVPath " , virtualDir);
domain.SetData( " .domainId " , domain.FriendlyName);
domain.SetData( " .hostingVirtualPath " , virtualDir);
domain.SetData( " .hostingInstallDir " , HttpRuntime.AspInstallDirectory);
WebHost host = (WebHost)domain.CreateInstanceAndUnwrap(
typeof (WebHost).Module.Assembly.FullName, typeof (WebHost).FullName);
host.setApplicationDomain(domain);
host.setVirtualDirectory(virtualDir);
host.setBaseDirectory(physicalDir);
return host;
}
這兒的一堆domain.SetData是傳遞參數給ASP.NET引擎。然後在那個appdomain中建立新的宿主類型的執行個體。這樣就避免多份代碼的尴尬。而使用ASP.NET就比較簡單了,在宿主類中使用HttpRuntime.ProcessRequest函數處理特定請求。簡單一點的話,可以直接用SimpleWorkerRequest包裝請求,生成頁面到一個指定的TextWriter中,如
private void DoRequest( string page, string query, TextWriter writer)
{
HttpRuntime.ProcessRequest( new SimpleWorkerRequest(page, query, writer));
}
public void RequestPage( string page, string query, Stream stream)
{
DoRequest(page, query, new StreamWriter(stream));
}
public void RequestPage( string page, Stream stream)
{
RequestPage(page, null , stream);
}
public string RequestPage( string page, string query)
{
using (StringWriter writer = new StringWriter())
{
DoRequest(page, query, writer);
return writer.ToString();
}
}
public string RequestPage( string page)
{
return RequestPage(page, string .Empty);
}
這個預設的請求包裝使用是簡單,但對中文的相容性不太好,過兩天有空再自己寫個強一點的吧,呵呵
最終類的使用就比較簡單了,在WinForm程式中建立一個singleton模式的屬性
static private WebHost.WebHost _host = null ;
public WebHost.WebHost Host
{
get
{
if (_host == null )
{
_host = WebHost.WebHost.Create();
}
return _host;
}
}
然後請求指定的asp.net頁面,如
HTML = Host.RequestPage(_page);
即可完成從動态的asp.net腳本到靜态html的轉換。嵌入WinForm程式中,還可以通過Host類型完成兩者之間的雙向通訊,實作互相控制。下次有空繼續,呵呵
參考資料:
1.Using the ASP.Net Runtime for extending desktop applications with dynamic HTML Scripts
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5CbyVXYvw1cldWYtl2Lc12bj5ibjd2bsJmL3d3dvw1LcpDc0RHaiojIsJye.gif)
http://www.west-wind.com/presentations/aspnetruntime/aspnetruntime.asp
2.Executing ASMX files without a web server
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5CbyVXYvw1cldWYtl2Lc12bj5ibjd2bsJmL3d3dvw1LcpDc0RHaiojIsJye.gif)
http://radio.weblogs.com/0105476/stories/2002/10/24/executingAsmxFilesWithoutAWebServer.html
3.ASP. NET Client-side Hosting with Cassini
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5CbyVXYvw1cldWYtl2Lc12bj5ibjd2bsJmL3d3dvw1LcpDc0RHaiojIsJye.gif)
http://msdn.microsoft.com/msdnmag/issues/03/01/CuttingEdge/
4.Using ASP.NET Runtime in Desktop Applications
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5CbyVXYvw1cldWYtl2Lc12bj5ibjd2bsJmL3d3dvw1LcpDc0RHaiojIsJye.gif)
http://www.codeguru.com/cs_internet/UsingAspRuntime.html
=================================================================
轉至:http://www.blogcn.com/User8/flier_lu/blog/1125444.html