run time Compiling

A

Analizer1

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
 
J

Jon Skeet [C# MVP]

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.
 
A

Analizer1

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
 
J

Jon Skeet [C# MVP]

Analizer1 said:
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...

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.
 
A

Analizer1

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
 
J

Jon Skeet [C# MVP]

Analizer1 said:
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

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

Analizer1

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
 
J

Jon Skeet [C# MVP]

Analizer1 said:
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

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.
 
D

DaveP

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");
}
}
}
 
D

DaveP

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
 
A

Analizer1

Last But not least....
How to Compile changed code....
the previous Dll is locked,
Can't Generate output File.....

Tks
Mike

DaveP said:
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
 
J

Jon Skeet [C# MVP]

DaveP said:
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

<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.
 
B

Ben Voigt [C++ MVP]

Analizer1 said:
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.
 
M

MikeJ

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top