srm.exe install shell extension: Fails with “Cannot cast the underlying exported value”

混江龙づ霸主 提交于 2019-12-11 04:50:11

问题


I have written this simplistic shell extension (explorer context menu) with SharpShell:

[ComVisible(true)]
[COMServerAssociation(AssociationType.AllFiles)]
public class SampleExtension : SharpContextMenu
{
    protected override bool CanShowMenu()
    {
        return true;
    }
    protected override ContextMenuStrip CreateMenu()
    {
        var menu = new ContextMenuStrip();
        var item = new ToolStripMenuItem
        {
            Text = "Hello world!"
        };
        menu.Items.Add(item);
        return menu;
    }
}

It works in debug with SharpShell Server Manager, but when I try to install it via srm.exe on command line I get:

srm.exe install ..\SampleExtension\bin\Debug\CountLinesExtension.dll -codebase  

System.ComponentModel.Composition.CompositionContractMismatchException was unhandled
  HResult=-2146233088
  Message=Cannot cast the underlying exported value of type 'SharpShell.SharpShellServer (ContractName="SharpShell.ISharpShellServer")' to type 'SharpShell.ISharpShellServer'.
  Source=System.ComponentModel.Composition
  StackTrace:
       at System.ComponentModel.Composition.ExportServices.CastExportedValue[T](ICompositionElement element, Object exportedValue)
       at System.ComponentModel.Composition.ExportServices.GetCastedExportedValue[T](Export export)
       at System.ComponentModel.Composition.ExportServices.<>c__DisplayClassa`1.<CreateStronglyTypedLazyOfT>b__7()
       at System.Lazy`1.CreateValue()
       at System.Lazy`1.LazyInitValue()
       at System.Lazy`1.get_Value()
       at ServerRegistrationManager.Application.<LoadServerTypes>b__2(Lazy`1 st)
       at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
       at ServerRegistrationManager.Application.InstallServer(String path, RegistrationType registrationType, Boolean codeBase)
       at ServerRegistrationManager.Application.Run(String[] args)
       at ServerRegistrationManager.Program.Main(String[] args)

How to fix this?


回答1:


We ran in into the same issue. Turns out we were just not running srm.exe from the same directory as as our dll. Our dll included other dlls so it would fail to load that way. Once we ran srm.exe with our dlls all in the same directory, it worked.



来源:https://stackoverflow.com/questions/25440626/srm-exe-install-shell-extension-fails-with-cannot-cast-the-underlying-exported

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