Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9075401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:51:11+00:00 2026-06-16T18:51:11+00:00

From what I can see I have to create a special datatype for each

  • 0

From what I can see I have to create a “special” datatype for each kind of data I want to transfer over WCF, so if I have a shared class like

public class District
{
    public long Id { get; set; }
    public string Name { get; set; }
}

and I want to send a District object using WCF, I have to create a datacontract and therefore I have to create a new WCF class

[DataContract]
public class WCFDistrict
{
    [DataMember]
    public long Id { get; set; }

    [DataMember]
    public string Name { get; set; }
}

And then when I use it in a WCF service in my implementation I have to parse the data from one object to the other

public WCFDistrict GetDistrict(long id)
{
    var district = _districtRepository.GetDistrict(id);
    return new WCFDistrict {Id = district.Id, Name = district.Name};
}

Is there someway to just reuse the shared class as a DataContract, without having those attributes on it? Or should I create a interface on the classes they can share so I can just cast it between them? Or something third?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T18:51:12+00:00Added an answer on June 16, 2026 at 6:51 pm

    First, you are not strictly required to provide a DataContract; WCF will serialize Plain Old Class Objects (POCO) correctly as long as you are on .Net 3.5 SP1 or later.

    Second, you can share the same physical class file in projects on both the server and client side; we have projects that have hundreds of classes (and code) that are directly shared this way and it saves a tremendous amount of time and effort in development and testing.

    There are a couple of steps required to get this up and running (doing this from memory so I may need adjust the answer):

    1) In the client side, if you are using VB, create a project in the same default namespace as the classes that you want to use in the client side (for C#, this isn’t important since the namespace is embedded in the classes).

    2) Add the class files to the project as links so that you have one physical copy of the class.

    3) Add a dataContractSerializer to your WCF configuration if you don’t have one already.

    4) In the client-side, right-click on the service and choose Configure Service Reference… In the resulting dialog, ensure that Reuse types in all referenced assemblies is checked and that the Reuse types in all referenced assemblies option is chosen.

    5) The trickiest part to getting this working is for collections.

    a) Decorate the collection with the CollectionDataContract attribute.

    b) Add an entry for this collection to the reference.svcmap‘s CollectionMappings table. To find the reference.svcmap, show all files in the project and then expand the service. To edit it, just double-click on the file. You will add an entry in here for each specific collection that you are serializing and you need to differentiate between those items that have a List<> base and those that have a Dictionary<> base. If you don’t take this step, WCF will automatically serialize these classes to the underlying generic signature and you will lose the use of your classes.

    The entries in this table look something like this:

    <CollectionMappings>
      <CollectionMapping TypeName="System.Collections.Generic.Dictionary`2" Category="Dictionary" />
      <CollectionMapping TypeName="System.Collections.Generic.List`1" Category="List" />
      <CollectionMapping TypeName="System.Collections.Specialized.StringCollection" Category="List" />
      <CollectionMapping TypeName="My.Namespace.MyDictionaryCollection" Category="Dictionary" />
    

    When you add these entries and save the file, the WCF client-side generator will rebuild the reference.cs or reference.vb file depending on what language you are using. You can tell if you don’t have the references configured correctly by looking at the generated code: if that code contains class definitions, then the WCF code generator was not able to map into your copied classes for some reason.

    One final note: sometimes the WCF code generator completely fails to generate the code and this is always due to a problem in the service (usually a class isn’t unique enough or a type was not able to be serialized for one reason or another).

    In order to debug this type of problem, the easiest thing to do is add WCF diagnostic logging, which will generate a file that can be opened by a special tool (forget the name of it) that allows you to drill into the error messages and discover exactly what went wrong. This has saved us untold hours of work. To configure this logging, add the following to your web.config anywhere in the <configuration> section:

      <system.diagnostics>
        <sources>
          <source name="System.ServiceModel"
                  switchValue="Information, ActivityTracing"
                  propagateActivity="true">
            <listeners>
              <add name="traceListener"
                  type="System.Diagnostics.XmlWriterTraceListener"
                  initializeData="c:\log\WcfTrace.svclog"  />
            </listeners>
          </source>
        </sources>
      </system.diagnostics>
    

    Once you have added this and saved web.config, attempt to update the service reference in the client, then double-click on the log file you specified and the tool will open.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web page where users can see data from MongoDB on a
I have a setup a SQL Server instance that I can see from all
I have some really funky code. As you can see from the code below
As you can see from the documentation the way to start an Activity to
From what I can see ui:dialog isnt a standard psuedo css element, so I
As you can see from the Client class, if the while breaks then the
Hi below link from Vaadin you can see how to set checkboxes on tabletree,
From a security perspective, I can see simply doing an 'eval' on incoming JSON
How can I remove the animation from this jQuery modal window? You can see
I'm using code from other c# app and I can see that in WP7

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.