I have written the .net-module (dll-file) which can be loaded in AutoCAD by means of command NETLOAD. Has checked up - all works. But I want to have possibility not only to load, but also unload such modules - therefore I want to load them at once in separate AppDomain.
I read MSDN
here. Hasn't understood, what sense in AppDomain. Load if it loads the code not only in the domain necessary to me but also in the main:
If the current AppDomain object represents application domain A, and the Load method is called from application domain B, the assembly is loaded into both application domains. For example, the following code loads MyAssembly into the new application domain ChildDomain and also into the application domain where the code executes:
AppDomain ad = AppDomain.CreateDomain("ChildDomain");
ad.Load("MyAssembly");
?
That the code was loaded only in that module which it is necessary - I have tried to use
CrossAppDomainDelegate in my code sample (C# code):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using acad = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System.Reflection;
[assembly: CommandClass(typeof(DomainUseSample.Class1))]
namespace DomainUseSample
{
public sealed class Class1
{
static AppDomain dom;
string path = @"D:\MD\Visual Studio 2010\CmdSample\CmdSample\bin\Debug\CmdSample.dll";
[CommandMethod("bush", "dload", CommandFlags.Session)]
public void Load()
{
if (dom != null)
{
acad.DocumentManager.MdiActiveDocument.Editor.WriteMessage("The domain already exists.\n");
return;
}
dom = AppDomain.CreateDomain("TestDomain");
try
{
dom.DoCallBack(new CrossAppDomainDelegate(LoadDll));
}
catch (System.Exception ex)
{
acad.DocumentManager.MdiActiveDocument.Editor.WriteMessage(string.Format("Excetion message: \"{0}\"\n", ex.Message));
return;
}
acad.DocumentManager.MdiActiveDocument.Editor.WriteMessage("Successful loading.\n");
}
[CommandMethod("bush", "dunload", CommandFlags.Session)]
public void Unload()
{
if (dom == null)
{
acad.DocumentManager.MdiActiveDocument.Editor.WriteMessage("The domain isn't created yet! At first launch a command dload.\n");
return;
}
try
{
AppDomain.Unload(dom);
dom = null;
}
catch (System.Exception ex)
{
acad.DocumentManager.MdiActiveDocument.Editor.WriteMessage(string.Format("Excetion message: \"{0}\"\n", ex.Message));
return;
}
acad.DocumentManager.MdiActiveDocument.Editor.WriteMessage("Successful unloading.\n");
}
void LoadDll() {
Assembly asm = Assembly.LoadFrom(path);
}
}
}
When I load this code and I launch a command dload - I receive an exception:
Command: netload
Command: dload
Excetion message: "Type 'DomainUseSample.Class1' in assembly 'DomainUseSample,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as
serializable."
Ok, I mark class Class1 with attribute Serializable:
[Serializable]
public sealed class Class1
{
...
}
Now, when I load this code and I launch a command dload - I receive an other exception:
Command: netload
Command: dload
Excetion message: "Type is not resolved for member
'DomainUseSample.Class1,DomainUseSample, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null'."
How correctly to load and unload .net-libraries in separate AppDomain by operation with AutoCAD?