I’m attempting to split up my WCF web services into a few services instead of 1 giant service. But the Visual Studio (Silverlight client) duplicates the common classes shared by both services. Here is a simple example to illustrate my problem.
In this example there are two services. Both return the type “Person”. By default VS will create two seperate Person proxy’s under unique NameSpaces. This means that the “Person” returned by the different services cannot be consumed by the client as the same thing. How do I fix this? Is it possible without writing the proxy classes myself?
Common
[DataContract]
public class Person
{
[DataMember]
string FirstName { get; set; }
[DataMember]
string LastName { get; set; }
[DataMember]
string PrivateData { get; set; }
}
StaffService.svc
[ServiceContract(Namespace = "")]
public class StaffService
{
[OperationContract]
public Person GetPerson ()
{
return new Person {"John", "Doe", "secret"};
};
}
PublicService.svc
[ServiceContract(Namespace = "")]
public class PublicService
{
[OperationContract]
public Person GetPerson ()
{
return new Person {"John", "Doe", "*****"};
};
}
Thanks for you help!
Justin
There is a check box under the Advanced section of “Add Service Reference” named “Reuse types in referenced assemblies”. This will hunt for types used in your service and if they already exist in a referenced assembly then they’ll be used rather than a proxy class generated.
One caveat here is that it’s only “referenced assemblies” that are searched, so it won’t pick up proxies generated by other services (and I believe the different namespace would stop it as well).
I usually have a business / domain project in my Silverlight project so I add my shared classes to that project (usually with the “Add Existing Item” > “Add as Link” so the code is shared).
Once that’s done you can generate your service references and they should pick up your existing types.
Hope this helps