A cannot be cast to B exception

点点圈 提交于 2019-12-13 01:47:48

问题


I have a piece of code in my project which, in certain situations can be run in an IIS worker process (inside a HttpHandler) and also by a separate Windows service. There is a referenced assembly (DataVisualisation.dll) which I wrote which is used by both the website components and the Windows service, and both the website and the service have their own copy of the DLL.

[A]DataVisualisation.ReportingTimePeriod cannot be cast to [B]DataVisualisation.ReportingTimePeriod. Type A originates from 'DataVisualisation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' at location 'C:\[project]\Actions\DataVisualisation.dll'. Type B originates from 'DataVisualisation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'c:\[project]\LCFW\lcscheduler\bin\Debug\DataVisualisation.dll'.

The code which provides this is simply:

ReportingTimePeriod rp = (ReportingTimePeriod)resultFromAnotherFunction;

Which interestingly works when running in the website, but not in the Windows Service. I can't figure that out, and I'm not sure what information to provide to help anyone else figure it out. I would have expected the website and service to be using their own individual copies of DataVisualisation.dll, and therefore be relatively self contained. Somehow, the different processes seem to know about the other DLL though.

Strong naming would be my first port of call, potentially installing the DLL in the GAC, but unfortunately DataVisualisation.dll references a couple of DLLs I have no control over, which aren't strongly named/signed.

Are there any other options? I'm close to using Reflection to see if the properties I need exist and accessing them that way, but I'd rather not do that if I can avoid it.


回答1:


I think your problem is that you have the DataVisualisation code duplicated in each project?

i.e. SolutionA is located in C:\SolutionA with Website and DataVisualisation folders and code under it. and SolutionB is located in C:\SolutionB with Service and DataVisualisation folders and code located under it. and even though the DataVisualisation code is identical in the 2 Solutions, the actual project guid will be different...

The only other thing I can think of is that your reference in the service might be of an old version of the DataVisualisation class. Rather make sure both solutions reference the same project(folder).



来源:https://stackoverflow.com/questions/13119398/a-cannot-be-cast-to-b-exception

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