Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding service references to multiple WCF services that shared classes

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

like image 270
Justin Avatar asked May 20 '09 18:05

Justin


2 Answers

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

like image 119
Nigel Sampson Avatar answered Sep 29 '22 08:09

Nigel Sampson


If you generate the proxies at the same time using svcutil.exe it will only generate one type. I don't know how to do this with adding a service reference to the project.

We run it in a batch file so I have clipped that down and changed the names to protect the innocent. It is really about mapping the service namespaces together and then including all the URLs together. It also has the collection type set (for lists) and includes an assembly reference (which some of the other answers reference.

@ECHO OFF

SET cmd=C:\"Program Files"\"Microsoft SDKs"\Windows\v6.0a\bin\SvcUtil.exe
SET cmd=%cmd% /out:Traffic.cs /noConfig /collectionType:System.Collections.Generic.List`1

SET cmd=%cmd% /reference:..\..\..\lib\Architecture.Frameworks.dll

REM ######### Service namespace mappings (Service Contracts and Message Contracts)
SET cmd=%cmd% /namespace:"http://services.test.com/app/2005/09/"
SET cmd=%cmd%,"app.ServiceProxies"

REM ######### Schema namespace mappings (Data Contracts)
SET cmd=%cmd% /namespace:"http://schemas.company.com/app/2005/09/"
SET cmd=%cmd%,"Co.ServiceProxies.app.DataContracts"

REM ######### Set all the URLs that have common types
SET cmd=%cmd% http://localhost/Services/MyService1.svc
SET cmd=%cmd% http://localhost/Services/MyService2.svc

%cmd%

PAUSE

If all the items are in the same service namespace, you could possibly get away with just having all the URLs and not worry about the namespaces, but I have not tried it that way.

like image 32
Brian ONeil Avatar answered Sep 29 '22 09:09

Brian ONeil