I'm trying to deserialize application data and cast it to a specific type from within my event handler, but this seems impossible as it refuses to find my class even though it's compiled into the same assembly as the event handler.
Regardless if I use the ApplicationData utility GetAs<T>()
or deserialize it myself I get the same error:
Could not cast as MyClass, reason: Unable to find assembly 'MyAssembly.MyClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
at System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo assemblyInfo, String name)
at System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String objectName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, Object[] typeInformationA, Int32[] memberAssemIds, ObjectReader objectReader, Int32 objectId, BinaryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record)
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryHeaderEnum binaryHeaderEnum)
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
Has anyone had a similar issue and solved it?
The only way forward that I can see, is either sticking the assembly in the GAC and pray that it can find it, or reduce my object to a string.
File: myeventhandler.cs
namespace MyAssembly
{
[TcmExtension("This does stuff")]
public sealed class MyEventHandler : TcmExtension
{
public MyEventHandler()
{
EventSystem.Subscribe<PublishTransaction, SaveEventArgs>(
(subject, args, phase) =>
// pseudo code!
foreach (var item in PublishTransaction))
{
var appData = item.ReadApplicationData(MyAppDataId);
if(appData != null)
{
var myAppData = appData.GetAs<MyClass>();
// do stuff with myAppData
}
}
},
EventPhases.TransactionCommited
);
}
}
}
File: myclass.cs
namespace MyAssembly
{
[Serializable]
public class MyClass
{
// has properties and constructors
}
}
<add assemblyFile>
entry in the Tridion.ContentManager.config file. I know it's the correct version of the assembly as additional debug logging added to the event handler shows up correctly.