Menu

Implement messages using AsyncWcfLib

asy proger

Downlod the binary package from http://sourceforge.net/projects/asyncwcflib and open file Test2.Messages.Test2Rsp.cs to find following code in context.
See [AsyncWcfLib package diagram] and [AsyncWcfLib class diagram messages] for an overview.

Message class requirements

Message classes can be serialized to XML-text (SOAP) and deserialized from this representation into running objects in memory of the receiving host system.
The WCF subsystem needs some attributes to do this using reflection:

  • A message class must have the [DataContract] attribut and the communication-namespace definition.

  • It must be derived from AsyncWcfLib.WcfMessage base class.

  • Before first reception of a message, its type must be registered by calling AsyncWcfLib.WcfMessage.AddKnownType().

  • User defined data members contained in the message class must be added to the known types also.
    For convenience, all types of a message assembly may be registered by the same static method (AddKnownMessageTypes).


[DataContract (Namespace = WcfDefault.WsNamespace)]
public class Test2Rsp: WcfMessage
{
  public static void AddKnownMessageTypes()
  {
    AddKnownType(typeof(Test2Req));
    AddKnownType(typeof(Test2Rsp));
    AddKnownType(typeof(Test2MessageItem));
  }
}


Message data member requirements

  • Data members that are serialized and sent over the network need the [DataMember] attribute.

  • Upon reception of a message, an object of the message class is created in memory and all known, deserialized data members are initialized with the received value.
    Some members may not be received because either they have no [DataMember] attribute on the sending side or do not exist in an older version of the message class on the sending side.
    These members are initialized with 0 or null. No constructor is called unless you define a method with [OnDeserialized] attribute.

  • Basic types like int, string and bool are known by the WCF subsystem, they can be serialized and deserialized as [DataMembers].
    Some more complex classes like Version, URI, Lists, ... are known also. But not all .NET types may be serialized as [DataMembers].
    To send such data, you have to define your own user class or send some private members of basic type that allow you to create the object on the receiving side.

  • User defined [DataMembers] like Test2MessageItem need the [DataContract] attribute, must be registered as known type, but must not be derived from WcfMessage.


[DataContract (Namespace=WcfDefault.WsNamespace)]
public class Test2Rsp: WcfMessage
{
  [DataMember] 
  public List<Test2MessageItem>  Items; // serialized to XML, the contained type
                                        // Test2MessageItem has been added to the known types.
  public int Index = 0; // not serialized
}

[DataContract (Namespace=WcfDefault.WsNamespace)]
public class Test2MessageItem
{
  [DataMember] public int          Index;
  [DataMember] public string       ItemName;
  [DataMember] public List<object> Parameter;
}

Related

Wiki: AsyncWcfLib class diagram messages
Wiki: AsyncWcfLib package diagram
Wiki: Home
Wiki: Run AsyncWcfLib.Test1

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.