AppDomain DoCallBack FileNotFoundException - C# [英] AppDomain DoCallBack FileNotFoundException - C#

查看:21
本文介绍了AppDomain DoCallBack FileNotFoundException - C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下代码的 C# 2.0 类:

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
        }
    }

此类包含在类库项目下,然后从 Web 应用程序项目中引用.

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

因此,在我的网页中,我只是实例化对象,期望类的构造函数会创建新的 AppDomain 并执行指定的操作:

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

但我不断收到此错误:

无法加载文件或程序集MyNamespace.MyClass,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"或其依赖项之一.该系统找不到指定的文件.

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));

正如你所看到的,它发生在 MyClass 上,所以我不明白如果代码已经在执行,它怎么会告诉我无法找到程序集???我只是不明白.

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.

顺便说一句,我是 AppDomain 使用的新手,可能我对某些概念感到困惑.

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

推荐答案

您创建的 AppDomain (_app) 似乎在其程序集中定位 MyClass 类时遇到问题.您的 CreateDomain 未提供任何 AppDomainSetup 信息,因此它没有当前应用程序域目录或程序集的上下文.您有两个选择,提供一个正确配置的 AppDomainSetup 或在您的 DoCallBack 方法之前调用 _app.Load(Assembly.GetExecutingAssembly.FullName).

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.

这篇关于AppDomain DoCallBack FileNotFoundException - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆