开发者

mefcontrib interceptingCatalog export error

开发者 https://www.devze.com 2023-04-05 04:54 出处:网络
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

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


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


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消