I have created a function to compile C# code in a string in memory and use it at runtime. It works very well.
Next I created a small class library (lets call it mynew.dll) and placed it at c:\mylibraries\mynew.dll.
In my code I can add a referenced assembly. I do it like this:
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters
{
GenerateExecutable = false,
GenerateInMemory = true
};
parameters.ReferencedAssemblies.Add(@"c:\mylibraries\mynew.dll");
CompilerResults results = provider.CompileAssemblyFromSource(parameters, mycode);
Now, in my C# code-string (mycode) is a function called "Execute". When I declare the namespace and class from the mynew.dll in this function I get this error:
Could not load file or assembly 'MyNew, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
When I copy the dll in the bin/debug folder of the main application is suddenly works, but I don't want this. I want the code, that is compiled at runtime, to use it, not the main application.
This I did before posting here:
- Search Stackoverflow
- Used Google
- Tried to compile the dll to the disk and not in memory. This works... Problem now is that the dll is locked in IIS and I cannot delete it without restarting the IIS server; no option since the code can change any moment and I don't want to restart the server when people are on the website.
Is there a way to fix this?