views:

3970

answers:

1

Hi,

I'm writing my first WCF service. I decided to write the service just as a DLL to begin with and then aspect the WCF stuff on afterwards which is where I am now.

I was advised by the arcitect that I should stick to a specific format for message objects which I have done. However I've used Interfaces, complex types and lists thereof in my message objects. I'm coming to adding the attributes on and I'm getting a bit confused.

Here's a show example of my code.

[ServiceContract]
public interface MyServiceContract
{
     [OperationContract]
     MyMethodResponseMessage MyMethod(MyMethodRequestMessage request);
}

public class MyService : MyServiceContract
{
    public MyMethodResponseMessage MyMethod(MyMethodRequestMessage request)
    {
        //Do things
    }
}

//Messages
[MessageContract]
public class MyMethodResponseMessage 
{
    [MessageBodyMember]
    public MyMethodResponse Body { get; set; }
}

[DataContract]
public class MyMethodResponse
{
    [DataMember]
    public IMyComplexTypeItem { get; set; }

    [DataMember]
    public List<IMyComplexType> Items { get; set; }

    [DataMember]
    public bool Success { get; set; }
}

//DTO    
public interface IMyComplexType 
{
    [DataMember]
    string Identity { get; set; }
}

[DataContract]
public class MyComplexType1 : IMyComplexType
{
     [DataMember]
     public virtual string Identity
}

Can anyone comment on the correctness in the use of MessageContract, DataContract, DataMember and Serializable etc? Any pointers or glaring mistakes?

Also which serializer is the best one to use? and what is the best strategy to ensure I get well formed XML from this so that other clients can consume my service easily?

+4  A: 

Re the request/response - a [DataContract] would work just as well. One of the advantages of message-contracts is that you can set privacy against members, but in many cases this isn't necessary. In such cases, I prefer to keep the contract as simple as possible, just as a data-contract.

Re which serializer - that is largely a factor of the configuration. By default over http, for example, it will be DataContractSerializer.

I'm not sure, however, that the list of IMyComplexType is going to work very well. You could try, but generally it wants concrete types. Note that with base-classes you can use [KnownType] to specify the allowed sub-types.

Note that unlike XmlSerializer, it is not a requirement for collection members to have setters - although you might need to add an OnDeserializing callback method to initialize the list if you do that (WCF doesn't call constructors).

Aside: you can also use protobuf-net with data-contracts and WCF (as long as they have an explicit Order); this is more densely packed than the regular xml. It has no support for message-contracts at the moment, though.

Marc Gravell
Thanks Marc, you're right about the Base class. It is working a bit better. I'll have a look at the OnDeserializing method, cheers for the heads up on that. I'm constrained to Xml for this by design.
Rob Stevenson-Leggett