Create, compile & generate web-services on the fly.

April 23, 2007

This article discusses how web services are generated on the fly. The code is generated and compiled at runtime. The code file  (i.e. “.cs”), service file (i.e. “.asmx” ) and compiled library (i.e. “.dll”) are generated dynamically.

After this code generation the third party applications can directly add a web-reference to the respective web-service and start using it.

class extenstions
    {
        public const string asmx = “.asmx”;
        public const string cs = “.cs”;
        public const string dll = “.dll”;
    }  #region WebService on Fly
string binPath = @”D:\StudioProjects\wwwroot\bin\”;
string codePath = @”D:\StudioProjects\wwwroot\code\”;
string asmxPath = @”D:\StudioProjects\wwwroot\webservices\”;
string code = “[WebService(Namespace = \”http://tempuri.org/\”)] “ +
              “[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]” +
              “public class dyna : System.Web.Services.WebService “ +
              “{ “ +
              “public dyna () {} “ +
              “[WebMethod]” +
              “public string HelloWorld() { “ +
              “return \”Hello World\”; “ +
              “}” +
              “}”;
string classname = “dyna”;
string[] assemblies = new string[] {
     “System”,“System.Web”,“System.Web.Services”,“System.Web.Services.Protocols”
};
 
//CS file generation
string codeFile =  codePath + classname + extenstions.cs;
StreamWriter writer = new StreamWriter(codeFile ,false);
foreach(string assembly in assemblies)
writer.WriteLine(“using “ + assembly + “;”);
writer.WriteLine();  
writer.Write(code);
 
writer.Close();
// ASMX  file generation
string asmxCode = “<%@ WebService Language=\”C#\” CodeBehind=\”~\\code\\”
+ classname + extenstions.cs +
“\” Class=\”” + classname + “\” %> “;
writer = new StreamWriter(asmxPath + classname + extenstions.asmx,false);
writer.WriteLine(asmxCode);
writer.Close(); 
Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider();
ICodeCompiler compiler = provider.CreateCompiler(); 
CompilerParameters param = new CompilerParameters();
param.CompilerOptions = @”/optimize”;
param.GenerateInMemory = false;
param.GenerateExecutable = false;
param.IncludeDebugInformation = false;
param.TreatWarningsAsErrors = false;
param.ReferencedAssemblies.Add(“System.dll”);
param.ReferencedAssemblies.Add(“System.Web.dll”);
param.ReferencedAssemblies.Add(“System.Web.Services.dll”);
param.OutputAssembly = binPath + classname + extenstions.dll;
CompilerResults results = compiler.CompileAssemblyFromFile(param, codeFile);
if (results.Errors.Count > 0)
{
StringBuilder err = new StringBuilder(); 
int i=1;
           
foreach (CompilerError error in results.Errors)
{
err.AppendLine(i + ” : “ + error.ErrorText);
err.AppendLine();
}
 
throw new Exception(err.ToString());
}
#endregion 

Now you can test your web-service directly on the local machine using a url. And the client application can directly include a web-reference to your web-service dll in the “webservices” directory of my application.

Incase you get a error saying “401 Access Denied” then just enable “Anonymous access” & “Intergrated Windows aunthentication” on the “webservices” folder.

 

-Bugs!

Advertisements

One Response to “Create, compile & generate web-services on the fly.”

  1. Sandeep Says:

    i didnt understand it…plz explain it to me….its not self-explantory…


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: