Compiling C# program with Request object

View: New views
4 Messages — Rating Filter:   Alert me  

Compiling C# program with Request object

by theallan :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello all,

I want to write a little CGI program that I can run under IIS, and I'm some what stumped - here is what I have:

public class Test
{
        static void Main()
        {
                Console.Write( "Content-type: text/html\n\n" );
                System.Console.WriteLine( System.Web.HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"] );
        }
}

Which is all well can good - it compiles with "gmcs -out:text.exe test.cs -r:System.Web". But then I transfer the .exe file to my Windows machine and try to run it as a CGI program I get:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.

So the question is, how can I compile a C# application with Mono using the 'Request' object and have it available under a IIS CGI environment. Or should I be accessing the CGI variables in some different manner?

Thanks
Allan

Re: Compiling C# program with Request object

by Robert Jordan :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

theallan wrote:

> Hello all,
>
> I want to write a little CGI program that I can run under IIS, and I'm some
> what stumped - here is what I have:
>
> public class Test
> {
>         static void Main()
>         {
> Console.Write( "Content-type: text/html\n\n" );
>                 System.Console.WriteLine(
> System.Web.HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"] );
>         }
> }
>
> Which is all well can good - it compiles with "gmcs -out:text.exe test.cs
> -r:System.Web". But then I transfer the .exe file to my Windows machine and
> try to run it as a CGI program I get:
>
> Unhandled Exception: System.NullReferenceException: Object reference not set
> to an instance of an object.

Well, a HttpContext is only available to ASP.NET applications.

> So the question is, how can I compile a C# application with Mono using the
> 'Request' object and have it available under a IIS CGI environment. Or
> should I be accessing the CGI variables in some different manner?

Get them from the environment (see the CGI specs) or resort to ASP.NET.

Robert

_______________________________________________
Mono-list maillist  -  Mono-list@...
http://lists.ximian.com/mailman/listinfo/mono-list

Re: Compiling C# program with Request object

by theallan :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Robert,

Thanks for the reply!

> Well, a HttpContext is only available to ASP.NET applications.

Okay - I see. Don't particularly want to make it an ASP.NET application.

> Get them from the environment (see the CGI specs) or resort to ASP.NET.

I had been trying to use: System.Environment.GetEnvironmentVariable, which works perfectly on the CLI, but when I run it in the IIS CGI environment I get:

Unhandled Exception: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed

So I guess I need to grant it permissions to get the environment variable, but I don't have a clue how to do this.

Thanks
Allan

Re: Compiling C# program with Request object

by Damien DALY-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I had been trying to use: System.Environment.GetEnvironmentVariable, which
works perfectly on the CLI, but when I run it in the IIS CGI environment I
get:

Unhandled Exception: System.Security.SecurityException: Request for the
permission of type 'System.Security.Permissions.EnvironmentPermission,
mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
failed

So I guess I need to grant it permissions to get the environment variable,
but I don't have a clue how to do this.

Hi,

This is about the user the CGI runs...
In most cases, IIS user is IUSR_machine, a kind of equivalent to unix user "none".

Due to security restrictions of the IUSR_machine user, it can't acces to its environement variables. So you have to search in IIS how you can change the user it uses for CGI access...

Good luck

Damien

_______________________________________________
Mono-list maillist  -  Mono-list@...
http://lists.ximian.com/mailman/listinfo/mono-list