Embed ASP. NET in the WinForm program

Source: Internet
Author: User
Tags webhost
Embed ASP. NET in the WinForm program

The current popular trend is desktop Web and Web desktop. The ultimate goal is to have me and me. For example, MSN Explorer is a good demonstration, so that users cannot tell when the local network is used. Such programs often require support from a background server such as IIS, which is too heavy for most desktop applications. Here, we provide a lightweight solution to embed ASP. NET into a common WinForm desktop program.
For security and other reasons, you must create a new AppDomain before using the ASP. NET engine. The simple method is to directly use the ApplicationHost. CreateApplicationHost function to create an ASP. NET engine host instance for the specified virtual directory and physical path, as shown in

// Shocould 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;
}

However, this program has a BT requirement. It will try to load the assembly of the host type (WebHost) in the bin subdirectory of the specified directory, that is to say, you must copy the program in the bin sub-directory, which is very uncomfortable. The solution is to manually complete the entire creation process as follows:

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;

}

Here, a bunch of domain. SetData parameters are passed to the ASP. NET engine. Create a new host type instance in that appdomain. This avoids the embarrassment of multiple codes. The use of ASP. NET is relatively simple. In the host class, the HttpRuntime. ProcessRequest function is used to process specific requests. Simply put, you can directly use SimpleWorkerRequest to wrap the request and generate a page to a specified TextWriter, as shown in

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

This default request packaging is simple to use, but it is not compatible with Chinese. If you have no time in two days, write a stronger one by yourself.

The use of the final class is relatively simple. Create a singleton mode attribute in the WinForm program.

Static private WebHost. WebHost _ host = null;

Public WebHost. WebHost Host
{
Get
{
If (_ host = null)
{
_ Host = WebHost. WebHost. Create ();
}
Return _ host;
}
}

Request the specified asp.net page, as shown in figure

HTML = Host. RequestPage (_ page );

You can complete the conversion from dynamic asp.net scripts to static html. Embedded in the WinForm program, you can also implement bidirectional communication between the two through the Host type to achieve mutual control. Continue later.

References:
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.