简体   繁体   中英

WCF DataContract Equivalence confusion

I have a server side DataContract like this:

[DataContract(Name = "CommonType", Namespace = "http://mycompany.com/2014/09/DataService")]
public class CompositeType
{
    bool boolValue = true;
    string stringValue = "Hello ";

    [DataMember(Name="Property1")]
    public bool BoolValue
    {
        get { return boolValue; }
        set { boolValue = value; }
    }

   [DataMember(Name = "Property2")]
    public string StringValue
    {
        get { return stringValue; }
        set { stringValue = value; }
    }
}

The server side class name is CompositeType with a specified DataContract name and namespace.

The client side class looks like this:

[DataContract(Name = "CommonType", Namespace = "http://mycompany.com/2014/09/DataService")]
public class ClientType
{
    bool boolValue;
    string stringValue;

    [DataMember(Name = "Property1")]
    public bool ClientBoolValue
    {
        get { return boolValue; }
        set { boolValue = value; }
    }

    [DataMember(Name = "Property2")]
    public string ClientStringValue
    {
        get { return stringValue; }
        set { stringValue = value; }
    }
}

The client side class is a different type but same DataContract name and namespace.

The ServiceContract looks like this:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);

}

The implementation is this:

public class Service1 : AES.Services.Data.IService1
{
    public AES.Services.Data.CompositeType GetDataUsingDataContract(AES.Services.Data.CompositeType composite)
    {
        if (composite == null)
        {
            throw new ArgumentNullException("composite");
        }
        if (composite.BoolValue)
        {
            composite.StringValue += "Suffix";
        }
        return composite;
    }
}

How do I leverage my client side type to be used for the datacontract type? The client side generates a proxy to the service and the type generated is new class. Shouldn't I be able to use the client side type that has the DataContract name that matches the server side DataContract?

I'm expecting to be able to execute this code on the client, no?

        Service.Service1Client client = new Service.Service1Client();

        ClientType ct = new ClientType();
        client.GetDataUsingDataContract(ct);

But the above code fails because the argument I'm passing is not the proxy class that was generated via the service reference generation. How am I suppose to use my client side version of the data contract????

UPDATE

The solution I have found to work was to use the channel factory, and create my own client side ServiceContract and DataContract, at first I didn't like it but I realized that is exactly what the ServiceReference is doing anyway. I put a common name and namespace on the ServiceContractAttribute and the DataContractAttribute on both the client and server. Now I can have independent development/evolution of my classes.

This link supports this approach as well: http://www.codeproject.com/Articles/55690/WCF-Loose-coupling-of-WCF-Service-and-Data-Contrac

The key to using client-side entities with WCF is to: a) Place them in a separate assembly/project (if they are not already there). b) Remove the service reference if you already added it. c) Add a project reference from the client project to the entities project. d) Re-add the service reference

SvcUtil, which is used by VS to add service references, will use the entities in the referenced project instead of generating them.

There is one caveat to this. Adding a service reference may not work if the entities in the client and server are in different assemblies. I have to dig further into why this is sometimes the case. But if adding a service reference does not work, then all you have to do is use a ChannelFactory instead.

Cheers, Tony

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM