Yossi Dahan [BizTalk]


Monday, August 09, 2010

Having trouble assigning a class to a message?

Despite some maintenance overhead that’s required, for a while now I’ve been a fan of assigning messages to classes and vice versa when needing to move between a BizTalk orchestration and code.

I’m pretty sure I (as well as others) blogged about this before, but for completeness sake the idea is that if you have a schema describing an entity, and a class describing exactly the same entity, and you ensure that you can serialise/deserialise from one to the other using Xml.Serialization attributes, BizTalk will quite happily do that serialisation and deserialisation for you, so if you have MyMessage in you orchestration, and a helper function somewhere that takes MyClass as a parameter, and MyMessage and MyClass correspond to each other, you should be able to put the following in an expression shape -


Equally, if MyFunction returns MyClass, you should be able to put the following in a message assignment shape -

MyMessage = MyHelper.MyFunciton();

Nice, isn’t it?

Occasionally, though, when doing this you get an error saying - ‘cannot implicitly convert type ‘MyClass’ to ‘MyMessage’ (or the other way around, depending on the direction), which I always find frustrating as it takes me ages to figure it out the reason. at first glance everything looks ok – I can serialise MyClass and validate it against the schema behind MyMessage and vice versa, but assignment in BizTalk will simply won’t work.

There are usually two reasons for this not to work -

A missing reference - In most cases a reference to the project containing the schema is added to the project containing the project(if different), as that’s required to define the message, but it’s easy to forget to add a reference to the project containing the classes (and any dependencies) without which the type cannot be resolved properly.

A modified root name – often Xml.Serialization attributes are used to control the shape of the xml generated from a class on serialisation, I certainly do that a lot; however - when BizTalk is expected to automatically serialise and deserialise between a class and a message, the root node of the xml much match the name of the class, the root class name must not be modified using attributes such as XmlRoot (although namespace can, and should, be provided)

Make sure you get these right and it should all work just fine!



  • Thanks Yossi. The second reason (modified root name) was the issue hitting me: my class had been genereated using the xsd utility but for some reason (complex schema...) the name of the root element in the schema did not match the name of the type in the generated class. I changed the type to match the schema root element and that did it :-) Thanks again.

    By Anonymous E., at 21/08/2012, 07:30  

Post a Comment

<< Home