Problems with versions after building the logging block from source.

Topics: Logging Application Block
Feb 1, 2013 at 1:19 PM
I have a problem with a copy of the Logging Block I have built from source. Specifically I am trying to use the MSMQ Distributor, which I builds successfully and the service installer works correctly too. However, the moment I try to write something, I get the following message in my Event Log:
Exception Type: System.InvalidCastException
[A]Microsoft.Practices.EnterpriseLibrary.Logging.LogEntry cannot be cast to
Type A originates from 'Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\Windows\assembly\GAC_MSIL\Microsoft.Practices.EnterpriseLibrary.Logging\5.0.414.0__31bf3856ad364e35\Microsoft.Practices.EnterpriseLibrary.Logging.dll'.
Type B originates from 'Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\Users\username\Documents\Visual Studio 2010\Projects\EntLib50Src\Blocks\Logging\MsmqImplementation\Microsoft.Practices.EnterpriseLibrary.Logging.dll'.
Data: System.Collections.ListDictionaryInternal
TargetSite: Microsoft.Practices.EnterpriseLibrary.Logging.LogEntry Deserialize(System.String)
The stack trace looks like this:
StackTrace Information Details: 
   at Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.BinaryLogFormatter.Deserialize(String serializedLogEntry)
   at Microsoft.Practices.EnterpriseLibrary.Logging.MsmqDistributor.MsmqLogDistributor.ReceiveQueuedMessages()
   at Microsoft.Practices.EnterpriseLibrary.Logging.MsmqDistributor.MsmqLogDistributor.CheckForMessages()
The line of code that references LogEntry is:
return (LogEntry)GetFormatter().Deserialize(binaryStream);
Which seems quite uncontroversial.

I do not have the strong named version mentioned anywhere in my configuration, I definitely used the locally built version of the configuration tool to build the .config file and I don't believe it to be referenced by any part of the solution. I am wondering whether Unity is trying to Dependency Inject the wrong version into my code for some reason. I do have the binary version of Enterprise Library installed as well- is this simply incompatible?

Can anyone cast any light on what is going on and what I need to do to prevent it?
Feb 1, 2013 at 4:52 PM
Edited Feb 7, 2013 at 4:16 AM
Usually I would ask you to check all of the configuration files and verify that the assemblies are not strong named (sn -vf <assembly name>).

But it sounds like you did that already. A double check never hurts. :)

I'm going to guess that the application generating (serializing) the LogEntry's is using the signed version and when your new unsigned Distributor Service tries to deserialize it fails with the error because the 2 LogEntry's are different types. Unfortunately, the binary formatter is quite particular about formats.

To avoid the error you would have to use the same assembly version from all application logging to MSMQ. Another approach would be to standardize on another formatter (and modify the Distributor Service appropriately -- which it seems like you've already done).

Randy Levy
Enterprise Library support engineer
Support How-to
Feb 4, 2013 at 12:50 PM
Thanks Randy.

What I ended up doing was uninstalling the binary distribution of EntLib so I only had the version I had built. Once that was done ( and I had manually removed a couple of classes from the GAC that seemed to want to stay there ) I could get everything working correctly.

Later I discovered that I didn't actually need to make any changes to the Distributor, so in fact I could have kept using the original code. Easy to be wise after the event.