Thursday, November 16, 2006

Consuming a webservice from behind a proxy firewall

I was trying to access a webservice from my office which is behind a proxy firewall, I was annoyed by seeing the following error message

"- The request failed with HTTP status 407: Proxy Authentication Required"

I knew that it is something to do with the firewall proxy. So I googled my doubt and the error text but couldn't get a descent solution. Then I tried with the options in the command WSDL that we use to generate a proxy class (not to be confused with firewall proxy) for our webservice.

I found the following options.


But I didn't know how to use these options (I should admit that I am not good at DOS). Then I googled these options and got some answers which I am going to share with you now.

/proxy: is the url of the proxy server with the port number. For E.g., if your proxy server address is and the port is 8001 then your proxy url is

/proxyusername: is the username of your proxy server and /proxypassword: is the password of your proxy server.

/proxydomain: is the domain name of your server.

So the WSDL command should be something like this.

c:\WSDL /proxy:
/proxypassword:pwd /proxydomain:lotus

The above command will download the proxy class file for the webservice.

But this is not the end of the show. We need to consume the service. The first thing we need to do is compile the class to a dll.

c:\csc.exe \t:library service1.cs

Then create a windows application project in Visual Studio and add the dll as a reference. Now you can use the class that is available in the dll and create an instance of it. But calling the web method from the webservice will again display the same error.

To resolve this you have to create a WebProxy. This is again the authenication information for firewall proxy.

To instantiate the WebProxy class you need to include the namespace System.Net in the "using" part.

using System.Net;

Now you can instantiate WebProxy class as follows,

// Instantiate the WebProxy class
WebProxy myProxy = new WebProxy(,true);
myProxy.Credentials = new NetworkCredential("uname", "pwd", "lotus");

Now, the proxy should be assigned to the Service class and the function can be called.

// Instantiate the WebService class
Service1 sr=new Service1();
sr.Proxy =myProxy; // Set the proxy to the class
String str=sr.HelloWorld(); // Calling the web method

This will call the method that is available in the webservice by authenticating the proxy server's credentials.

Happy Coding :)

Monday, November 06, 2006

Dynamic Code Compilation

Welcome to dynamic code compilation. This article throws light on how a code can be built dynamically, compiled and run. The scenario of dynamic code compilation may arise when you want an application to upgrade (or rewrite) another application.
Lets jump in to coding. The namespaces that we are going to use in this project are
  • System.CodeDom
  • System.CodeDom.Compiler
  • Microsoft.CSharp
The System.CodeDom namespace contains classes that can be used to represent the elements and structure of a source code document. The classes in this namespace can be used to model the structure of a source code document that can be output as source code in a supported language using the functionality provided by the System.CodeDom.Compiler namespace.

The Microsoft.CSharp namespace contains classes that support compilation and code generation using the C# language.

At first we need to instantiate the class CSharpCodeProvider. Then create a complier using CreateCompiler() function of CSharpCodeProvider class and assign to the interface ICodeCompiler.

CSharpCodeProvider codeProvider = new CSharpCodeProvider();
ICodeCompiler icc = codeProvider.CreateCompiler();

Then set the parameters for the compiler using CompilerParameters class. Set parameters like GenerateExecutable, OutputAssembly

System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
parameters.GenerateExecutable = true;
parameters.OutputAssembly = "Out.exe";

Now declare a String variable and write the source code for the new application/program. Now instantiate CompilerResults class with compiler parameters and the source code as below.

String sourcecode;
sourcecode="using System;namespace SampleApp{class Class1{[STAThread] static void Main(string[] args) { Console.WriteLine(\"I am born to live!\");Console.Read(); } }}";
CompilerResults results = icc.CompileAssemblyFromSource(parameters,sourcecode);

Now the code is compiled. If you want to run the compiled EXE, start the process by


Thats all folks!....
Happy coding