Click here to Skip to main content
12,830,433 members (47,107 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 16 Jul 2007

Sharing Types Between WCF Service and Client

, 16 Jul 2007
Rate this:
Please Sign up or sign in to vote.
An article on sharing types between WCF service and client.


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.


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 = "")]
public class SharedType
    private string name;

    public string Name
        get { return name; }
            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.Runtime.Serialization", "")]
public partial class SharedType : object, 
    private System.Runtime.Serialization.ExtensionDataObject 
    private string[] AttributesField;
    private string NameField;

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

    public string[] Attributes
        get { return this.AttributesField; }
        set { this.AttributesField = value; }

    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"

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.


  • No updates



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
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralThanks for the tip Pin
venkatpv19-Mar-12 4:42
membervenkatpv19-Mar-12 4:42 
GeneralMy vote of 5 Pin
Member 331429029-Nov-11 12:03
memberMember 331429029-Nov-11 12:03 
GeneralThis article is not relevant anymore ! Pin
Froyke13-Sep-10 5:03
memberFroyke13-Sep-10 5:03 
GeneralNo proxy.cs generated when /r is used Pin
Jigar B Patel24-Apr-08 0:18
memberJigar B Patel24-Apr-08 0:18 
QuestionGenerating constants using svcutil Pin
syamala_198111-Dec-07 20:20
membersyamala_198111-Dec-07 20:20 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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