mefcontrib拦截目录导出错误

发布于 2024-12-04 13:50:00 字数 1535 浏览 0 评论 0原文

我正在尝试在 asp.net mvc2 应用程序中测试 mef 和 mefcontrib 但出现错误:

Cannot cast the underlying exported value of type LoggerExtSys.Domain.WebLogger 
(ContractName="LoggerExtSys.Domain.IWebLogger") to type LoggerExtSys.Domain.IWebLogger. 

我的测试项目 此处

Global.asax 中的代码:

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);

var catalog = new CatalogBuilder()
.ForAssembliesInDirectory(HttpRuntime.BinDirectory, "*ExtSys.dll")
.Build();

// Create interception configuration
var cfg = new InterceptionConfiguration()
                .AddInterceptor(new StartableStrategy());

// Create the InterceptingCatalog with above configuration
var interceptingCatalog = new InterceptingCatalog(catalog, cfg);

// Create the container
var container = new CompositionContainer(interceptingCatalog);

// exception here
var barPart = container.GetExportedValue<IWebLogger>();
barPart.Debug("Test");
}

当我尝试

在 WebLogger 中获取 GetExportedValue 代码时出现异常:

 [Export(typeof(IWebLogger))]
    public class WebLogger : IWebLogger
    {
        #region IWebLogger Members

        public void Debug(string str)
        {

        }

        #endregion

        #region ICoreExtension Members

        public void Initialize()
        {

        }

        #endregion
    }

但在桌面应用程序中一切正常。 如何修复它?谢谢大家

I am trying to test mef and mefcontrib in asp.net mvc2 app but i got an error:

Cannot cast the underlying exported value of type LoggerExtSys.Domain.WebLogger 
(ContractName="LoggerExtSys.Domain.IWebLogger") to type LoggerExtSys.Domain.IWebLogger. 

My test project here

code in Global.asax:

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);

var catalog = new CatalogBuilder()
.ForAssembliesInDirectory(HttpRuntime.BinDirectory, "*ExtSys.dll")
.Build();

// Create interception configuration
var cfg = new InterceptionConfiguration()
                .AddInterceptor(new StartableStrategy());

// Create the InterceptingCatalog with above configuration
var interceptingCatalog = new InterceptingCatalog(catalog, cfg);

// Create the container
var container = new CompositionContainer(interceptingCatalog);

// exception here
var barPart = container.GetExportedValue<IWebLogger>();
barPart.Debug("Test");
}

Exception when i try to get GetExportedValue

code in WebLogger:

 [Export(typeof(IWebLogger))]
    public class WebLogger : IWebLogger
    {
        #region IWebLogger Members

        public void Debug(string str)
        {

        }

        #endregion

        #region ICoreExtension Members

        public void Initialize()
        {

        }

        #endregion
    }

But in desktop app all working good.
How to fix it? Thanks for all

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏辞 2024-12-11 13:50:01

好的,问题出在加载程序集的代码块中:

public AggregateCatalog ForAssembliesInDirectory(string directory, string pattern)
        {
            IList<ComposablePartCatalog> _catalogs = new List<ComposablePartCatalog>();

            var dir = new DirectoryInfo(directory);
            Assembly assembly;
            foreach (var file in dir.GetFiles(pattern))
            {
                assembly = Assembly.LoadFile(file.FullName);            
                _catalogs.Add(new AssemblyCatalog(assembly));
            }

            return new AggregateCatalog(_catalogs);
        }

经过所有测试,我将其删除并使用 DirectoryCatalog。我不知道为什么,但它可以在桌面和网络应用程序中工作。
谁能告诉我为什么我的旧代码无法在网络应用程序中工作将得到接受的答案和 50 赏金。谢谢大家

Ok, the problem was in code block which load assemblies:

public AggregateCatalog ForAssembliesInDirectory(string directory, string pattern)
        {
            IList<ComposablePartCatalog> _catalogs = new List<ComposablePartCatalog>();

            var dir = new DirectoryInfo(directory);
            Assembly assembly;
            foreach (var file in dir.GetFiles(pattern))
            {
                assembly = Assembly.LoadFile(file.FullName);            
                _catalogs.Add(new AssemblyCatalog(assembly));
            }

            return new AggregateCatalog(_catalogs);
        }

After all test i remove it and use DirectoryCatalog. I dont know why but its work in desktop and web app.
Who will tell me why my old code not working in web app will get accepted answer and 50 bounty. Thanks for all

夜血缘 2024-12-11 13:50:01

我认为问题要么出在这里:

[Export(typeof(IWebLogger))]
public class WebLogger : IWebLogger
{

要么出在处理类型引用和解析的方式上。

我会尝试将行:更改

var barPart = container.GetExportedValue<IWebLogger>();

为:

var barPart = container.GetExportedValue<WebLogger>();

或者您也可以尝试始终使用完全限定名称,这样不仅可以使用 IWebLogger,还可以将其完整名称空间放在前面。

你说这在基于 Windows 的应用程序中运行良好,你在该项目中引用了哪些程序集,或者你如何在其中编写 Application_Start 事件处理程序的内容?你确定完全一样吗?

I think the problem is either here:

[Export(typeof(IWebLogger))]
public class WebLogger : IWebLogger
{

or in the way you handle type referencing and resolution.

I would try to change the line:

var barPart = container.GetExportedValue<IWebLogger>();

into:

var barPart = container.GetExportedValue<WebLogger>();

or you can also try to always use fully qualified names so not only IWebLogger but put its full namespace before.

you say this works well in windows based application, what assemblies did you reference in that project or how do you write in there the content of your Application_Start event handler? Are you sure it's absolutely the same?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文