[Mono-list] Executing ASP.net page out side IIS (in apache)

Jason Diamond jason@injektilo.org
Tue, 26 Mar 2002 08:18:59 -0800


This is a multi-part message in MIME format.

------=_NextPart_000_0092_01C1D49E.E1B596A0
Content-Type: text/plain;
	charset="Windows-1252"
Content-Transfer-Encoding: 7bit

Here's what I use to execute ASP.NET pages outside of IIS. The "fix" was to
copy the assembly containing the host to a bin subdirectory.

Jason

----- Original Message -----
From: <rmahato@dacafe.com>
To: <mono-list@ximian.com>
Cc: <gvaish@iitk.ac.in>
Sent: Tuesday, March 26, 2002 7:40 AM
Subject: [Mono-list] Executing ASP.net page out side IIS (in apache)


> hi
> i am  working on developing a ISAPI application for  executing a asp.net
> page  in a apache application
>
> i would like to contibute in the areas for developing asp.net runtime..
> any idea where i  can start on.
>
> Raj
>
> ==========
>
> regarding what i am doing :
>
> 1) and ISAP dll  for ruting .aspx/asmx request  in apache
>
> 2) process this request in .net framework
>
> using system.web.hosting.applicationshost
>
> httpworkerrequest.
>
>
> i am able to execute the code form am exe
> but whe i call the dotnet component form  an ISAPI applicatio it throws
> and exception
>
>
> The type initializer for "System.Web.HttpRuntime" threw an exception.
>
> =================
>
> .net componet code
> ==================
>
>
> using System;
> using System.Web;
> using System.IO ;
> using System.Web.Hosting;
> using  System.Runtime.InteropServices ;
>
>
> namespace ASPNET
> {
> /// <summary>
> /// Summary description for Class1.
> /// </summary>
> ///
> public class MyExeHost : MarshalByRefObject
> {
> public void ProcessRequest(string page,ref string Output)
> {
> try
> {
> MemoryStream ms= new MemoryStream();
> StreamWriter sw = new StreamWriter(ms);
> //BinaryWriter bw = new BinaryWriter(ms);
> //HttpContext hc= HttpContext.Current;
> sw.AutoFlush = true;
> HttpRuntime.ProcessRequest(new SimpleWorkerRequest(page ,null,sw));
> StreamReader sr= new StreamReader(ms);
> ms.Position =0;
> Output = sr.ReadToEnd();
> }
> catch(Exception ex)
> {
> Output = ex.Message+page;
> return;
> }
> }
>
> }
>
> public interface IMyHost
> {
> void  ExecutePage(string page,string AppName, string AppDir, ref
> string Output);
> }
>
> [ClassInterface(ClassInterfaceType.None )]
> public class MyHost :IMyHost
> {
> public void  ExecutePage(string page,string AppName, string AppDir,
> ref string Output)
> {
> try
> {
>
> MyExeHost  host =
> (MyExeHost)ApplicationHost.CreateApplicationHost(typeof(MyExeHost),
> AppName,AppDir);
> host.ProcessRequest(page, ref Output);
> }
> catch(Exception ex)
> {
> Output = ex.Message +page+" Execute page "+AppName+AppDir;
> }
>
> }
> }
>
> }
> ====================
>
> ISAPI code
>
> ======================
>
> // validate.cpp : Defines the entry point for the DLL application.
> //
> #include <windows.h>
> //#include "compnet.h"
> #include "httpext.h"
> #include "stdio.h"
> #import "aspnetcomp.tlb"
>
>
> void WriteContext(EXTENSION_CONTROL_BLOCK *pECB, char *pszFormat, ...);
> int ExecPage(BSTR pg ,BSTR ap ,BSTR dr ,BSTR *out) ;
>
> //DLL Entry point
> BOOL APIENTRY DllMain(HANDLE hModule, DWORD  ul_reason_for_call, LPVOID
> lpReserved)
> {
> return TRUE;
> }
>
>
>
> //Ist Function called by IIS
> BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer)
> {
> pVer->dwExtensionVersion = HSE_VERSION;
> strncpy(pVer->lpszExtensionDesc, "Validate ISAPI Extension",
> HSE_MAX_EXT_DLL_NAME_LEN);
> return TRUE;
> }
>
> //start
> void StartContext(EXTENSION_CONTROL_BLOCK *pECB)
> {
> WriteContext(pECB, "<html>\r\n<body>\r\n");
> }
>
> //end
> void EndContext(EXTENSION_CONTROL_BLOCK *pECB)
> {
> WriteContext(pECB, "</body>\r\n</html>");
> }
>
> //for Writing Data back to client
> void WriteContext(EXTENSION_CONTROL_BLOCK *pECB, char *pszFormat, ...)
> {
> char szBuffer[1024];
> va_list arg_ptr;
> va_start(arg_ptr, pszFormat);
> vsprintf(szBuffer, pszFormat, arg_ptr);
> va_end(arg_ptr);
> DWORD dwSize = strlen(szBuffer);
> pECB->WriteClient(pECB->ConnID, szBuffer, &dwSize, 0);
> }
>
>
> //Function called for processing the client Request
> DWORD WINAPI HttpExtensionProc(EXTENSION_CONTROL_BLOCK *pECB)
> {
> StartContext(pECB);
> WriteContext(pECB, "<p><b><font face='Verdana' color='#008000'>This
> Message is recieved form ISAPI application!</font></b></p>");
> WriteContext(pECB, pECB->lpszQueryString);
> WriteContext(pECB, "<HR>");
> LPSTR  tt= "test.aspx" ;
> BSTR  pp= (wchar_t *)malloc( sizeof( wchar_t ));
> int ss= mbstowcs( pp, tt, strlen(tt)+1);
> BSTR pg = ::SysAllocString(pp);
> BSTR out =::SysAllocString( L"Invalid output" );
> WriteContext(pECB, "<HR>");
> // int rc = ExecPage (L"test.aspx",L"/myapp",L"E:\\raj\\mypages",
> &out);
> unsigned    int sz = ::SysStringLen (out) ;
> char *result = new char [2*sz+1]  ;
> wcstombs(result,out, sz+1);
> WriteContext(pECB,result);
> WriteContext(pECB, "<HR>");
> EndContext(pECB);
> return HSE_STATUS_SUCCESS;
> }
>
> //
>
> BOOL WINAPI TerminateExtension(DWORD dwFlags)
> {
>
> //dwFlags=HSE_TERM_MUST_UNLOAD ;
>
> return TRUE;
>
> }
>
>
> ///////////////////////////////
>
>
>
>
> int ExecPage(BSTR pg ,BSTR ap ,BSTR dr ,BSTR *out)
> {
> using namespace ASPNETComp;
>
> HRESULT hr;
> int Rc =-1;
>
> IMyCompHost *hstPtr;
>
> CoInitialize(NULL);
>
> hr = CoCreateInstance ( __uuidof(MyComHost), // CLSID of coclass
> NULL,                    // not used -
> aggregation
> CLSCTX_INPROC_SERVER,    // type of server
> __uuidof (IMyCompHost),         // IID of
> interface
> (void**) &hstPtr);        // Pointer to our
> interface pointer
>
> if ( SUCCEEDED ( hr ) )
> {
> // Call methods using pISL here.
> //printf(" \nSuccess in Creating the COM Class");
> // Long see variant table for codes
> BSTR page= ::SysAllocString (pg);
> BSTR app  =::SysAllocString(ap);
> BSTR dir = ::SysAllocString(dr);
>    //BSTR output = :: SysAllocString( out );
>     hr=hstPtr->ExecutePage(page,app,dir,out);
>     if ( SUCCEEDED ( hr ) )
> {
>
> //printf("Success in calling the method");
> unsigned    int sz = ::SysStringLen (*out) ;
> char *result = new char [sz]  ;
> wcstombs(result,*out, sz);
> // printf("\nResult :\n\n%s",result);
> Rc=0;
>
> }
> else
> {
> //printf("Failed in calling the method");
> Rc=-1;
> }
>
> hstPtr->Release();
> }
> else
> {
> // Couldn't create the COM object.  hr holds the error code.
> //printf(" \nError in Creating The COM Object:");
>
> Rc=-1;
>
> }
>
> //Unload comobject
> CoUninitialize();
> return Rc;
> }
>
>
> /*
> int ExecPage(BSTR pg ,BSTR ap ,BSTR dr ,BSTR *out)
> {
> using namespace ASPNET;
>
> HRESULT hr;
> int Rc =-1;
>
> IMyHost *hstPtr;
>
> CoInitialize(NULL);
>
> hr = CoCreateInstance ( __uuidof(MyHost), // CLSID of coclass
> NULL,                    // not used -
> aggregation
> CLSCTX_INPROC_SERVER,    // type of server
> __uuidof (IMyHost),         // IID of
> interface
> (void**) &hstPtr);        // Pointer to our
> interface pointer
>
> if ( SUCCEEDED ( hr ) )
> {
> // Call methods using pISL here.
> //printf(" \nSuccess in Creating the COM Class");
> // Long see variant table for codes
> BSTR page= ::SysAllocString (pg);
> BSTR app  =::SysAllocString(ap);
> BSTR dir = ::SysAllocString(dr);
>    //BSTR output = :: SysAllocString( out );
>     hr=hstPtr->ExecutePage(page,app,dir,out);
>     if ( SUCCEEDED ( hr ) )
> {
>
> //printf("Success in calling the method");
> unsigned    int sz = ::SysStringLen (*out) ;
> char *result = new char [sz]  ;
> wcstombs(result,*out, sz);
> // printf("\nResult :\n\n%s",result);
> Rc=0;
>
> }
> else
> {
> //printf("Failed in calling the method");
> Rc=-1;
> }
>
> hstPtr->Release();
> }
> else
> {
> // Couldn't create the COM object.  hr holds the error code.
> //printf(" \nError in Creating The COM Object:");
>
> Rc=-1;
>
> }
>
> //Unload comobject
> CoUninitialize();
> return Rc;
> }*/
>
>
>
> //////////comp header
>
> // Created by Microsoft (R) C/C++ Compiler Version 12.00.8168.0
> (fbe69483).
> //
> // \\inatpdqml21s\genhfrom tlb\compnet\debug\aspnet.tlh
> //
> // C++ source equivalent of Win32 type library aspnet.tlb
> // compiler-generated file created 03/25/02 at 10:38:40 - DO NOT EDIT!
>
> #pragma once
> #pragma pack(push, 8)
>
> #include <comdef.h>
>
> namespace ASPNET {
>
> //
> // Forward references and typedefs
> //
>
> struct /* coclass */ MyExeHost;
> struct __declspec(uuid("e24d8042-d758-3c0c-9a8c-bbcd6f2b6ba1"))
> /* dual interface */ IMyHost;
> struct /* coclass */ MyHost;
> struct __declspec(uuid("d7f66457-3a60-3ccb-9f50-12267f756f07"))
> /* dual interface */ _MyExeHost;
>
> //
> // Smart pointer typedef declarations
> //
>
> _COM_SMARTPTR_TYPEDEF(IMyHost, __uuidof(IMyHost));
> _COM_SMARTPTR_TYPEDEF(_MyExeHost, __uuidof(_MyExeHost));
>
> //
> // Type library items
> //
>
> struct __declspec(uuid("c42ca746-c369-378c-98d6-d800eeb88d10"))
> MyExeHost;
> // [ default ] interface _MyExeHost
> // interface _Object
>
> struct __declspec(uuid("e24d8042-d758-3c0c-9a8c-bbcd6f2b6ba1"))
> IMyHost : IDispatch
> {
> //
> // Wrapper methods for error-handling
> //
>
> HRESULT ExecutePage (
> _bstr_t page,
> _bstr_t AppName,
> _bstr_t AppDir,
> BSTR * Output );
>
> //
> // Raw methods provided by interface
> //
>
> virtual HRESULT __stdcall raw_ExecutePage (
> BSTR page,
> BSTR AppName,
> BSTR AppDir,
> BSTR * Output ) = 0;
> };
>
> struct __declspec(uuid("5543e24c-9d66-39b0-8507-73a5357e77ba"))
> MyHost;
> // interface _Object
> // [ default ] interface IMyHost
>
> struct __declspec(uuid("d7f66457-3a60-3ccb-9f50-12267f756f07"))
> _MyExeHost : IDispatch
> {};
>
> //
> // Wrapper method implementations
> //
>
> //#include "\\inatpdqml21s\genhfrom tlb\compnet\debug\aspnet.tli"
>
>
> inline HRESULT IMyHost::ExecutePage ( _bstr_t page, _bstr_t AppName,
> _bstr_t AppDir, BSTR * Output ) {
> HRESULT _hr = raw_ExecutePage(page, AppName, AppDir, Output);
> if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
> return _hr;
> }
>
>
> } // namespace ASPNET
>
> #pragma pack(pop)
> //////////////////
>
>
>
>
>
> ///////
>
> Any help in this regard  ll be great
>
>
>
> Raj
> _____________________________________________________________
>
>
>
> _______________________________________________
> Mono-list maillist  -  Mono-list@ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>

------=_NextPart_000_0092_01C1D49E.E1B596A0
Content-Type: text/plain;
	name="ConsoleHost.cs"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="ConsoleHost.cs"

using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Hosting;

public class ConsoleHost : MarshalByRefObject
{
	static void Main(string[] arguments)
	{
		if (arguments.Length =3D=3D 0)
		{
			Console.Error.WriteLine("Usage: ConsoleHost page (name=3Dvalue)*");
		}
		else
		{
			string page =3D arguments[0];

			// Build a query string using the command line arguments.

			string query =3D String.Empty;

			for (int i =3D 1; i < arguments.Length; ++i)
			{
				if (query !=3D String.Empty)
				{
					query +=3D "&";
				}

				int indexOfEquals =3D arguments[i].IndexOf('=3D');

				if (indexOfEquals !=3D -1)
				{
					string name =3D arguments[i].Substring(0, indexOfEquals);
					string value =3D arguments[i].Substring(indexOfEquals + 1);
					query +=3D name + "=3D" + EscapeValue(value);
				}
			}

			// CreateApplicationHost requires this assembly be in a
			// subdirectory called bin so copy this assembly there if it
			// doesn't already exist or is out of date.

			if (! Directory.Exists("bin"))
			{
				Directory.CreateDirectory("bin");
			}

			string mainModuleFileName =3D =
Process.GetCurrentProcess().MainModule.FileName;
			string binFileName =3D "bin\\" + =
Path.GetFileName(mainModuleFileName);

			if (! File.Exists(binFileName) || =
File.GetLastWriteTime(mainModuleFileName) > =
File.GetLastWriteTime(binFileName))
			{
				File.Copy(mainModuleFileName, binFileName, true);
			}

			// Create the host and process the request.

			ConsoleHost host =3D =
(ConsoleHost)ApplicationHost.CreateApplicationHost(typeof(ConsoleHost), =
"/", Directory.GetCurrentDirectory());
			host.ProcessRequest(page, query);
		}
	}

	void ProcessRequest(string page, string query)
	{
		HttpRuntime.ProcessRequest(new SimpleWorkerRequest(page, query, =
Console.Out));
	}

	// See RFC2396 for the details on why we need to do this.

	static string EscapeValue(string value)
	{
		StringBuilder result =3D new StringBuilder();

		foreach (char c in value)
		{
			if (c =3D=3D ';' ||
				c =3D=3D '/' ||
				c =3D=3D '?' ||
				c =3D=3D ':' ||
				c =3D=3D '@' ||
				c =3D=3D '&' ||
				c =3D=3D '=3D' ||
				c =3D=3D '+' ||
				c =3D=3D '$' ||
				c =3D=3D ',' ||
				c =3D=3D '%')
			{
				result.AppendFormat("%{0:X2}", (int)c);
			}
			else if (c =3D=3D ' ')
			{
				result.Append('+');
			}
			else
			{
				result.Append(c);
			}
		}

		return result.ToString();
	}
}

------=_NextPart_000_0092_01C1D49E.E1B596A0--