AppDomain DoCallBack FileNotFoundException - C#

这一生的挚爱 提交于 2019-12-24 15:15:26

问题


I have a C# 2.0 class with the following code:

public class MyClass : MarshalByRefObject, IDisposable 
    {
        private string _appName;
        private AppDomain _app;

        public MyClass(string appName)
        {
            _appName = appName;
            _app = AppDomain.CreateDomain("NewDomain" + _appName);
            _app.DoCallBack(new CrossAppDomainDelegate(CallBackMethod));
        }

        public void Dispose()
        {
            AppDomain.Unload(_app);
        }

        public static void CallBackMethod()
        {
            //some operations
        }
    }

This class is contained under a Class Library Project, then referenced from a Web Application Project.

So, in my web page I just instance the object, expecting that the constructor of the class would create that new AppDomain and perform the specified operations:

MyClass objMyClass = new MyClass("12022012213456");

But I keep getting this error:

Could not load file or assembly 'MyNamespace.MyClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

But the curious thing here is that this exception is happening at this line:

_app.DoCallBack(new CrossAppDomainDelegate(CallBackMethod));

And as you can see, it's happening on MyClass, so I don't understand how could it tell me that the assembly could not be found if the code is already being executed??? I just don't get it.

BTW, I'm a newbie at the usage of AppDomain, maybe I'm confused with some concept.


回答1:


It appears that the AppDomain you created (_app) is having problems locating your MyClass class in it's assembly. Your CreateDomain has not provided any AppDomainSetup information so it has no context for the current app domains directories nor assemblies. You have two options, provide a propertly configured AppDomainSetup or call _app.Load(Assembly.GetExecutingAssembly.FullName) prior to your DoCallBack method.




回答2:


I had this problem but the solution was different. In my situation, I needed to explicitly provide my own AppDomainSetup object and specify the ApplicationBase folder, so that it could actually find my assembly.

My code was loaded by another app, and that's why my assembly wasn't able to be found using the default settings.



来源:https://stackoverflow.com/questions/9550419/appdomain-docallback-filenotfoundexception-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!