[RESOLVED]Type mismatch in "asp.net mvc"

this is a runtime error,the error occurs not regular, when break in

code here:
if(context is WebContext) //context is not null and context is base on WebContext { webContext = context as WebContext; }
from now on,"webcontext" is always null,and any cast to base type operation failed.
recomplie project or restart website,everything is normal.

abiho

from now on,"webcontext" is always null

What do you mean by "now" ? When have been worked? What’s the difference?

thanks for your post.

"now" means after the error occurs

i think may be i changed something what make application restart,but static objects (single instance) will not create again.

abiho

"now" means after the error occurs

What error? SHow full stack trace, please

here is:

System.ComponentModel.Composition.CompositionContractMismatchException: 

Cannot cast the underlying exported value of type “App.SwitchCommandExports.CreateSwitchCommand (ContractName="CommandConfigContract")”
to type “System.Func`2[System.String,App.CommandBase]”

like this in stackoverflow:http://stackoverflow.com/questions/14316978/compositioncontractmismatchexception-when-trying-to-use-mef-with-mvc-controller

abiho

ystem.ComponentModel.Composition.CompositionContractMismatchException: 

Cannot cast the underlying exported value of type “App.SwitchCommandExports.CreateSwitchCommand (ContractName="CommandConfigContract")”
to type “System.Func`2[System.String,App.CommandBase]”

this is not obviously not raising from this code:: 


abiho

if(context is WebContext)   //context is not null and context is base on WebContext
{
    webContext = context as WebContext;
}

Hi abiho,

Thanks for your post.

abiho

System.ComponentModel.Composition.CompositionContractMismatchException: 

Cannot cast the underlying exported value of type “App.SwitchCommandExports.CreateSwitchCommand (ContractName="CommandConfigContract")”
to type “System.Func`2[System.String,App.CommandBase]”

As ignatandrei mentioned,this error is not obviously raising from the code that you provide.please you’d better set a breakpoint to trace.

abiho

like this in stackoverflow:http://stackoverflow.com/questions/14316978/compositioncontractmismatchexception-when-trying-to-use-mef-with-mvc-controller

Did you add  MEF to ASP.NET MVC?please check this:http://www.hanselman.com/blog/ExtendingNerdDinnerAddingMEFAndPluginsToASPNETMVC.aspx

Hope this can be helpful.

Best Regards,

Eileen

hi ignatandrei,Eileen

thanks a lot,I got it!

I have a "MEF ServiceLocator" that load "All Exports" of all Assemblies in application,but AppDomain.CurrentDomain.GetAssemblies() method after asp.net restart not load all assemblies like first run.

run first:
AppDomain.CurrentDomain.GetAssemblies()
{System.Reflection.RuntimeAssembly[103]}

run second:
AppDomain.CurrentDomain.GetAssemblies()
{System.Reflection.RuntimeAssembly[38]}

_DomainAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();

foreach (var asm in _DomainAssemblies)
{
    AssemblyName asmName = asm.GetName();
    if (canLoad(asmName.Name))
    {
        TryLoadAssembly(asm);
    }
}

...
// reload files not in current domain
var files = System.IO.Directory.GetFiles(pp, "*.dll");
foreach (var file in files)
{
    string asmName = GetAssemblyName(file); 
    if(string.IsNullOrEmpty(asmName))
    {
        continue;
    }
    if (_DomainAssemblies.FirstOrDefault(a=>a.GetName().Equals(asmName))!=null)
    {
        continue;
    }
    if (canLoad(asmName))
    {
        TryLoadAssembly(file);
    }
}

Best Regards

abiho

Leave a Reply