Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

run time Compiling

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Analizer1 - 13 Oct 2007 17:36 GMT
Hello all
I have a idea...and dont know if it is possible......
we have a pretty huge system at work and
we send EDI Special formatted Data to Several
Other Companies, via sFtp,dial up, vpn depending on these customers
requirements

one of the areas we are using a implimentation in vb6 (yuck) but
works...slow as well u know,

it implements msSqlServer Record set, has xml (config )instructions like
customer specific information then creates
a output Xml, then a text file via xlt.....

im Writing a completly new system using c#, and i had a idea, of a main
information dll, and c# source files for output on the server that we
program for each customer(partner) that will create the output format based
on partner requirements, output  can be NSF, Ansi ,hl7 csv, tab,  and weird
output formats they want...

is there a way to compile and run the code at Run time
if any changes in the partner specif ic source has changed
the main dll, compiles and runs the specific partner file to generate the
output.....this would only take place if a programmer has made any changes
to the source file

I hope i explaned myself
thanks alot
MikeJ
Jon Skeet [C# MVP] - 13 Oct 2007 19:13 GMT
<snip>

> is there a way to compile and run the code at Run time
> if any changes in the partner specif ic source has changed
> the main dll, compiles and runs the specific partner file to generate the
> output.....this would only take place if a programmer has made any changes
> to the source file

Yes, you *can* do that (CSharpCodeCompiler) but I think it would be a
much better idea for the programmer to make the change, compile it
themselves, and then put the compiled class library onto the server
after testing. You can then notice that the DLL has changed, reload the
assembly, and use that.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Analizer1 - 13 Oct 2007 19:45 GMT
where can i find examples on how to accompish this...

personally i would want to manage the code then place it up on the server
after any changes...but the other programmers are lazy...they want
easy....lazy...
so without arguing with them..i want to write a run on the fly system for
this...then eventually manage it as you said i should do...

thanks alot
Mike

> <snip>
>
[quoted text clipped - 10 lines]
> after testing. You can then notice that the DLL has changed, reload the
> assembly, and use that.
Jon Skeet [C# MVP] - 13 Oct 2007 22:09 GMT
> where can i find examples on how to accompish this...
>
[quoted text clipped - 3 lines]
> so without arguing with them..i want to write a run on the fly system for
> this...then eventually manage it as you said i should do...

Are they really too lazy to even compile their code and test that it
works, even to a cursory extent? It's scary to think that the partners
trust them...

CSharpCodeProvider is pretty easy to use - create one, then call
CompileAssemblyFromFile (or one of the appropriate
CompileAssemblyFromXXX methods) to generate an assembly.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Analizer1 - 14 Oct 2007 00:04 GMT
i got that working pretty well......
i want to unload the dll also
even if i were to manage these dll's (not dynamic)

i still need to load and unload them i could have lots of partner specific
dll's....

any help available is appriciated
Mike

>> where can i find examples on how to accompish this...
>>
[quoted text clipped - 11 lines]
> CompileAssemblyFromFile (or one of the appropriate
> CompileAssemblyFromXXX methods) to generate an assembly.
Jon Skeet [C# MVP] - 14 Oct 2007 00:25 GMT
> i got that working pretty well......
> i want to unload the dll also
[quoted text clipped - 4 lines]
>
> any help available is appriciated

You can't unload individual DLLs - you can only unload an AppDomain.
So, you may need to load each DLL in a separate AppDomain.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Analizer1 - 14 Oct 2007 01:07 GMT
I have a Dll....but each time i load and unload the appdomain...memory keeps
going up
the reason for loading and unloading
i could have hundreds of dll's that need to be loaded run the code and
unloaded

help with this

>> i got that working pretty well......
>> i want to unload the dll also
[quoted text clipped - 8 lines]
> You can't unload individual DLLs - you can only unload an AppDomain.
> So, you may need to load each DLL in a separate AppDomain.
Jon Skeet [C# MVP] - 14 Oct 2007 09:12 GMT
> I have a Dll....but each time i load and unload the appdomain...memory keeps
> going up
[quoted text clipped - 3 lines]
>
> help with this

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

DaveP - 14 Oct 2007 17:09 GMT
here is some running code ...
the dll im loading is a simple  c# code file static main
Prints "hello world"
Sample of the myasm.dll at end of this file
Im new At this...so lots of learning going on here......i wanna make sure
there is no
memory leaks and i can load, execute and unload dlls at run time

all you knowledge is very welcome
thanks
Mike

Example:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.CodeDom.Compiler;
using System.Windows.Forms;
using System.Reflection;
using Microsoft.CSharp;
using System.Threading;

namespace Compiler1
{
   class Program
   {
       static void Main(string[] args)
       {
           CompileAndRun.Compile();
           CompileAndRun.RunRemote();

       }
   }
   public static class CompileAndRun
   {
       public static void Run()
       {

           CompilerResults Result = CompileAndRun.Compile();
           //Execute the class

           Console.WriteLine(Result.CompiledAssembly.Location);
           Console.WriteLine(Result.CompiledAssembly.FullName);

           //  Assembly MyAsm =
NewDomainName.Load(Result.CompiledAssembly.Location); //"myasm.dll");
//Result.CompiledAssembly.Location);
             Assembly MyAsm = Result.CompiledAssembly;

           if (MyAsm == null)
           {
               Console.WriteLine("dll not loaded");
           }

           object myobj = MyAsm.CreateInstance("mynamespace.MyClass");

           if (myobj == null)
           {
               MessageBox.Show("Couldn't load class.");
               return;
           }
           object ret = myobj.GetType().InvokeMember("Test",
                BindingFlags.InvokeMethod, null, myobj, null);

           Console.WriteLine("back");

           System.Threading.Thread.Sleep(10000);

       }
       public static CompilerResults Compile()
       {
           CodeDomProvider Provider =
CodeDomProvider.CreateProvider("CSharp");
           CompilerParameters cp = new CompilerParameters();
           cp.ReferencedAssemblies.Add("System.DLL");
           //cp.ReferencedAssemblies.Add("System.IO.dll");
           cp.ReferencedAssemblies.Add("System.Windows.Forms.dll");
           cp.GenerateInMemory = false;
           cp.OutputAssembly = "d:\\cstest\\myasm.dll";
           CompilerResults Result = Provider.CompileAssemblyFromFile(cp,
"d:\\cstest\\class1.cs");
           Console.WriteLine(Result.Errors.Count);
           if (Result.Errors.HasErrors)
           {
               string eMsg = "";
               eMsg = Result.Errors.Count.ToString() + " Errors ";
               for (int x = 0; x < Result.Errors.Count; x++)
               {
                   eMsg = eMsg + "\r\n " +
                   Result.Errors[x].Line.ToString() + " - " +
                   Result.Errors[x].ErrorText;
               }
               MessageBox.Show(eMsg);
           }
           return Result;
       }
       public static void printall(AppDomain ad)
       {
           Assembly[] LoadedAssemblies =ad.GetAssemblies();
           foreach( Assembly a in LoadedAssemblies)
           {
               Console.WriteLine(a.GetName().Name);
           }
           Console.ReadKey();
       }
       public static void RunRemote()
       {
           //CSScriptLibrary.CSScript.Load("d:\\cstest\\class1.cs");
           for (int x = 0; x < 1000; x++)
           {

               AppDomain ad = AppDomain.CreateDomain("NewDomain");

               // Assembly as
               Assembly MyAsm = ad.Load("myasm");
               object myobj = MyAsm.CreateInstance("mynamespace.MyClass");
               object ret = myobj.GetType().InvokeMember("Main",
                  BindingFlags.InvokeMethod, null, myobj, null);
               AppDomain.Unload(ad);

               Thread.Sleep(1000);

           }
       }
   }
}

// Sample class1.cs using to compile and load in new appdomain

using System;
using System.IO;
using System.Windows.Forms;
namespace mynamespace
{
public class MyClass:MarshalByRefObject
{
   public static void Main()
   {

      Console.WriteLine("Hello World # 9million 38");
       //MessageBox.Show("hello World");
   }
}
}
Jon Skeet [C# MVP] - 14 Oct 2007 20:02 GMT
> here is some running code ...
> the dll im loading is a simple  c# code file static main
[quoted text clipped - 5 lines]
>
> all you knowledge is very welcome

<snip>

I'll try to have a look in the next couple of days - I'm pretty busy at
the moment, unfortunately :(

However, I would *imagine* that compiling within the "child" AppDomain
would make you less susceptible to accidentally loading the freshly
created DLL within the "parent" AppDomain and thus leaking.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

DaveP - 14 Oct 2007 17:09 GMT
im also having problems loading a dll from a Different path, other then the
Application Path....
Thanks again for all you help it is really appriciated
Mike

>> I have a Dll....but each time i load and unload the appdomain...memory
>> keeps
[quoted text clipped - 10 lines]
> See http://www.pobox.com/~skeet/csharp/complete.html for details of
> what I mean by that.
Analizer1 - 14 Oct 2007 19:04 GMT
Last But not least....
How to Compile changed code....
the previous Dll is locked,
Can't Generate output File.....

Tks
Mike

> im also having problems loading a dll from a Different path, other then
> the Application Path....
[quoted text clipped - 15 lines]
>> See http://www.pobox.com/~skeet/csharp/complete.html for details of
>> what I mean by that.
Ben Voigt [C++ MVP] - 15 Oct 2007 15:14 GMT
> Last But not least....
> How to Compile changed code....
> the previous Dll is locked,
> Can't Generate output File.....

This shows you didn't unload every AppDomain that loaded the assembly.  Like
Jon said, probably you managed to load the assembly into the primary
AppDomain.

> Tks
> Mike
[quoted text clipped - 18 lines]
>>> See http://www.pobox.com/~skeet/csharp/complete.html for details of
>>> what I mean by that.
MikeJ - 15 Oct 2007 21:15 GMT
yep Thats what i did.....
got it all working....works pretty sweet
1. compile source into dll or Exe
2. Load Dlll into secondary appdomain
3. Execute code via interface
4. Delete DLL created
4. unload (secondary appdomain) freeing up the dlls

Tested in a 10000  sleep(1000) loop all nite...
worked like a charm

I also want to thank you all very much, with out your help
would of taken lot longer
Thanks Again
Mike

>> Last But not least....
>> How to Compile changed code....
[quoted text clipped - 27 lines]
>>>> See http://www.pobox.com/~skeet/csharp/complete.html for details of
>>>> what I mean by that.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.