B/S C/S在WinForm程序中嵌入ASP.NET WebForm应用程序
2009-03-11 14:03
375 查看
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
http://www.west-wind.com/presentations/aspnetruntime/aspnetruntime.asp
2.Executing ASMX files without a web server
http://radio.weblogs.com/0105476/stories/2002/10/24/executingAsmxFilesWithoutAWebServer.html
3.ASP. NET Client-side Hosting with Cassini
http://msdn.microsoft.com/msdnmag/issues/03/01/CuttingEdge/
4.Using ASP.NET Runtime in Desktop Applications
http://www.codeguru.com/cs_internet/UsingAspRuntime.html
=================================================================
转至:http://www.blogcn.com/User8/flier_lu/blog/1125444.html
现
在的流行趋势是桌面程序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
http://www.west-wind.com/presentations/aspnetruntime/aspnetruntime.asp
2.Executing ASMX files without a web server
http://radio.weblogs.com/0105476/stories/2002/10/24/executingAsmxFilesWithoutAWebServer.html
3.ASP. NET Client-side Hosting with Cassini
http://msdn.microsoft.com/msdnmag/issues/03/01/CuttingEdge/
4.Using ASP.NET Runtime in Desktop Applications
http://www.codeguru.com/cs_internet/UsingAspRuntime.html
=================================================================
转至:http://www.blogcn.com/User8/flier_lu/blog/1125444.html
相关文章推荐
- 在WinForm程序中嵌入ASP.NET
- 在WinForm程序中嵌入ASP.NET[转]
- 在WinForm程序中嵌入ASP.NET
- 在WinForm程序中嵌入ASP.NET
- 将你的Asp.NET应用程序嵌入到SharePoint 读后感
- ASP.NET也像WinForm程序一样运行
- asp.net WebForm程序删除.designer.cs文件之后的故事
- asp.net WebForm程序删除.designer.cs文件,重新设计并绑定服务端控件时间
- 利用Bootstrap+Avalonjs+EntityFramework 开发ASP.NET WebForm应用程序(上)
- 将Asp.NET应用程序嵌入到SharePoint
- [C#]使用 C# 代码实现拓扑排序 dotNet Core WEB程序使用 Nginx反向代理 C#里面获得应用程序的当前路径 关于Nginx设置端口号,在Asp.net 获取不到的,解决办法 .Net程序员 初学Ubuntu ,配置Nignix 夜深了,写了个JQuery的省市区三级级联效果
- 将你的Asp.NET应用程序嵌入到SharePoint
- 有简易通用权限管理后台的快速C# ASP.NET开发的例子应用程序卖给了曾长期开发JAVA银行程序的大哥
- 【Asp.net入门05】第一个ASP.NET 应用程序-测试Asp.net程序
- ASP.NET程序也能像WinForm程序一样运行
- Visual Studio C#的winform/webform/asp.net控件命名规范
- ASP.NET也像WinForm程序一样运行的实现方法
- 将你的Asp.NET应用程序嵌入到SharePoint
- C#、ASP.NET获取当前应用程序的绝对路径,获取程序工作路径
- 在程序中支持多语言环境,支持asp.net和winform。