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