使用 GAC 在 AppDomain 之间共享数据 (System.InvalidCastException)

Sharing data between AppDomain using GAC (System.InvalidCastException)

我创建了一个 'StrongNamed' dll 并成功将其安装到 GAC。

应用正常运行和共享数据;然而,当我在 Visual Studio 内处于调试模式时,我得到以下 InvalidCastException.

为什么不能正确调试?

相反,我收到以下错误消息:(代码基于 http://geekswithblogs.net/akraus1/archive/2012/07/25/150301.aspx

Unhandled Exception: System.InvalidCastException: [A]AppDomainTests.CrossDomainData cannot be cast to [B]AppDomainTests.CrossDomainData. Type A originates from 'StrongNamedDomainGateDll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=98f280cda3cbf035' in the context 'Default' at location 'C:\Source\WindbgAuto\bin\AnyCPU\Release\StrongNamedDomainGateDll.dll'. Type B originates from 'StrongNamedDomainGateDll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=98f280cda3cbf035' in the context 'Default' at location 'C:\Source\WindbgAuto\bin\AnyCPU\Release\StrongNamedDomainGateDll.dll'.
   at AppDomainTests.DomainGate.DoSomething(Int32 gcCount, IntPtr objAddress) in C:\Source\WindbgAuto\Tests\AppDomainTests\DomainGate.cs:line 24
   at AppDomainTests.DomainGate.DoSomething(Int32 gcCount, IntPtr objAddress)
   at AppDomainTests.DomainGate.Send(DomainGate gate, Object o) in C:\Source\WindbgAuto\Tests\AppDomainTests\DomainGate.cs:line 50
   at AppDomainTests.Program.Main(String[] args) in C:\Source\WindbgAuto\Tests\AppDomainTests\Program.cs:line 41

我的猜测是您没有在项目调试设置中禁用 Visual Studio 托管进程。 Visual Studio 将在没有程序集共享的 AppDomain 中托管托管进程中的代码。