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

  • Home
  • SEARCH
  • 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 3219716
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:40:43+00:00 2026-05-17T15:40:43+00:00

The background I’m developing a REST API for a C#.NET web application using WCF.

  • 0

The background

I’m developing a REST API for a C#.NET web application using WCF. I configured it to use the XmlSerializer rather than its default DataContractSerializer, for greater control over the XML format. I created a generic ResponseContract<TResponse, TErrorCode> data contract, which wraps the response with <Api> and <Response> for generic data like request status, error messages, and namespaces. An example method:

ResponseContract<ItemListContract, ItemListErrorCode> GetItemList(...)

An example response from the above method:

<?xml version="1.0" encoding="utf-8"?>
<Api xmlns="http://example.com/api/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <Response Status="OKAY" ErrorCode="OKAY" ErrorText="">
      <Data Template="ItemList">
         <Pages Template="Pagination" Size="10" Index="1" Count="13" Items="126" />
         <Items>
            <Item example="..." />
            <Item example="..." />
            <Item example="..." />
        </Items>
      </Data>
   </Response>
</Api>

The problem

This works very well for services whose methods all have the same generic ResponseContract types. WCF or XmlSerializer expects each contract to have a unique name within its namespace, but the service is now returning a generic contract with different types having the same XML root name:

ResponseContract<ItemListContract, ItemListErrorCode> GetItemList(...)
ResponseContract<ItemContract, ItemErrorCode> GetItem(...)

With the resulting exception:

The top XML element 'Api' from namespace 'http://example.com/api/' references distinct types Company.Product.ApiServer.Contracts.ResponseContract`2[Company.Product.ApiServer.Contracts.Items.ItemListContract,Company.Product.ApiServer.Interfaces.Items.ItemListErrorCode] and Company.Product.ApiServer.Contracts.ResponseContract`2[Company.Product.ApiServer.Contracts.Items.ItemContract,Company.Product.ApiServer.Items.ItemErrorCode]. Use XML attributes to specify another XML name or namespace for the element or types.

The service must allow different return types. This is difficult to achieve because the ResponseContract<TResponse, TErrorCode> (which sets the name & namespace) is generic and returned by all API methods. I also need to maintain WSDL metadata integrity, which means no dynamic changes using reflection.

Attempted solutions

  1. Changing the XML attributes declaratively is not possible, since the <Api> root element and its attributes are completely generic (in ResponseContract).

  2. Changing the attribute namespace at runtime using reflection (eg, ‘http://example.com/api/Items/GetItemList&#8217;) has no effect. It’s possible to get attributes, but changes to them have no effect. This would break WSDL anyway.

  3. When implementing IXmlSerializable, the writer is already positioned after the <Api> start tag when WriteXml() is invoked. It’s only possible to override the serialization of <Api>‘s child nodes, which cause no problem anyway. This wouldn’t work anyway, since the exception is thrown before the IXmlSerializable methods are called.

  4. Concatenating the constant namespace with typeof() or similar to make it unique doesn’t work, because the namespace must be a constant.

  5. The default DataContractSerializer can insert type names into the name (like <ApiOfIdeaList>), but DataContractSerializer‘s output is bloated and unreadable and lacks attributes, which is not feasible for the external reusers.

  6. Extending XmlRootAttribute to generate the namespace differently. Unfortunately there is no type information available when it’s invoked, only the generic ResponseContract data. It’s possible to generate a random namespace to circumvent the problem, but dynamically changing the schema breaks the WSDL metadata.

  7. Making ResponseContract a base class instead of a wrapper contract should work, but would result in a lot of duplicated generic data. For example, <Pages> and <Item> in the example above are also contracts, which would have their own equivalent <Api> and <Response> elements.

Conclusion

Any ideas?

  • 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-05-17T15:40:43+00:00Added an answer on May 17, 2026 at 3:40 pm

    I got the tumbleweed badge for this one!

    I abandoned the described approach because I couldn’t find a viable solution. Instead, every contract inherits a nullable QueryStatus<TErrorCode> property from the generic BaseContract<TContract, TErrorCode>. This property is populated automatically for the main contract, and null for subcontracts.

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

Sidebar

Related Questions

Background: At my company we are developing a bunch applications that are using the
Background: Some time ago, I built a system for recording and categorizing application crashes
Background I am writing and using a very simple CGI-based (Perl) content management tool
Background: I have a little video playing app with a UI inspired by the
Background: I need to reserve an amount of memory below 0xA0000 prior to my
Background I have a massive db for a SharePoint site collection. It is 130GB
Background I am trying to create a copy of a business object I have
Background: Over the next month, I'll be giving three talks about or at least
Background I work for a large organization which has thousands of MS Access applications
Background I have been asked by a client to create a picture of the

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.