Click here to Skip to main content
Click here to Skip to main content

Sharing Types Between WCF Service and Client

By , 16 Jul 2007
 

Introduction

One option in WCF I find very valuable is the ability to share types between the service and the client proxy. The /reference option of the SvcUtil allows the generated client proxy to reuse some types defined in a shared assembly.

Background

When a client proxy for a web service is generated, a definition for all complex types is generated as well. This creates two problems:

  • If you use the same type in more than one service, the generated client code may have multiple definitions for the same type
  • Any useful properties, constructors and collections used in the server type definition are lost

While the second problem is simply an annoyance, the first one may create a lot of headaches.

Let's assume the SharedType class is defined in the code used by the service like this:

[DataContract(Namespace = "http://mycode.com/types")]
public class SharedType
{
    private string name;

    [DataMember]
    public string Name
    {
        get { return name; }
        set
        {
            if (value == null || value.Length < 3)
                throw new ApplicationException();

            name = value;
        }
    }

    [DataMember(Name = "Attributes")]
    private List<string> attributes = new List<string>();

    public ICollection<string> Attributes
    {
        get { return attributes; }
    }

    public SharedType()
    {
    }

    public SharedType(string name)
    {
        this.Name = name;
    }
}

If the client proxy is generated without reference to the assembly with the data types, the SharedType class is generated like this:

[System.CodeDom.Compiler.GeneratedCodeAttribute
            ("System.Runtime.Serialization", "3.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute
                (Namespace="http://mycode.com/types")]
public partial class SharedType : object, 
            System.Runtime.Serialization.IExtensibleDataObject
{
    private System.Runtime.Serialization.ExtensionDataObject 
                            extensionDataField;
    private string[] AttributesField;
    private string NameField;

    public System.Runtime.Serialization.ExtensionDataObject ExtensionData
    {
        get { return this.extensionDataField; }
        set { this.extensionDataField = value; }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public string[] Attributes
    {
        get { return this.AttributesField; }
        set { this.AttributesField = value; }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public string Name
    {
        get { return this.NameField; }
        set { this.NameField = value; }
    }
}

It is easy to notice that this type does not have the constructor with argument name and the Attributes property is implemented as Array instead of List.

To generate client proxy that refers to the original type use the following options of the ServiceModel Metadata Utility Tool (Svcutil.exe):

"c:\Program Files\Microsoft SDKs\Windows\v6.0\Bin\SvcUtil"
    /l:cs
    /out:SharedTypeService.cs
    /config:..\App.config
    /n:*,MyClient.Proxy
    <b>/r:..\..\MyTypes\bin\MyTypes.dll</b>
    http://localhost/MyService/SharedTypeService.svc?wsdl 

Now the client proxy refers to the assembly with the data contracts and the client may take advantage of all accessors and useful methods coded in it.

Using the code

The code is a sample that demonstrates how a client proxy can be generated by referring to the data contracts used in the service.

History

  • No updates

References

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Mike Robski
Web Developer
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralThanks for the tipmembervenkatpv19 Mar '12 - 3:42 
Perfectly works in VS2010 when you use SVCUtil.exe to generare the proxy
 
SvcUtil.exe /n:*,MyService /r:C:\Windows\Microsoft.NET\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll
http://localhost/MyService/DataService.svc?wsdl
 
Thanks a LOT !!
GeneralMy vote of 5memberMember 331429029 Nov '11 - 11:03 
This solution is still perfectly relevant to VS2010 development if you choose to generate your proxy code in batch fashion. This trick saved my day!
GeneralThis article is not relevant anymore !memberFroyke13 Sep '10 - 4:03 
I'm using .Net 4.0 and Visual Studio 2010.
Now(and I don't know from what version this was supported), all you need to do is to reference your shared objects dll (aka DTO) on the client project as well. In the service reference settings make sure that 'Reuse types in reference assemblies' is checked.
 
That's it. from now on your wcf client will use the shared dll's objects instead of generating proxies.
GeneralNo proxy.cs generated when /r is usedmemberJigar B Patel23 Apr '08 - 23:18 
Well, This is what something i was looking for. But when i use /r (or /refernece) in svcutil, it does not generate Proxy.cs file, but only the config file.
 
I read on other sites that it is a bug with svcutil. Confused | :confused:
 
Any alternative to this.
 
Regards
~Paras
QuestionGenerating constants using svcutilmembersyamala_198111 Dec '07 - 19:20 
Thanks for the post. It was very helpful. I dont know whether this is the correct place to post this but I have a doubt using svcutil to generate the code.
 
I have an application where a class is being shared across the client (WPF) and the server (WCF). I have used svcutil to generate the server proxy. The service interface and the common class were generated but the generated common class does not have constants with it. In other words, svcutil generated only the private data members of the class. I could not generate those constants on the proxy. I need those constants in the UI. How can I generate the constants using svcutil?
 
FYI, I have used the following command to generate the code:
svcutil.exe http://localhost:8000/MyService/?wsdl /out:MyServiceClient.cs /noconfig
 
Thanks in advance,
Ravi Kumar

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 16 Jul 2007
Article Copyright 2007 by Mike Robski
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid