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 4125496
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:52:37+00:00 2026-05-20T23:52:37+00:00

In order to get a WCF service working with JQuery I have added a

  • 0

In order to get a WCF service working with JQuery I have added a WebInvoke attribute on the operation contract to control the JSON serialisation as follows:

[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]

Is there a way to control this serialisation via the service bindings in the config instead as it limits this service from providing different serialisations to different endpoints.

  • 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-20T23:52:37+00:00Added an answer on May 20, 2026 at 11:52 pm

    I have a different solution, which works at the opposite end from @Marc Gravell’s: instead of duplicating the contract, derive 2 different classes from the service implementation. These are just type aliases; they are necessary because the WCF activation system (as least on IIS) will not allow you to activate the same service type at different URLS (I’m not sure why Gravell’s solution didn’t run into this problem – the error that I’m getting is “A registration already exists for URI…” when I try to activate the same service class at different URLs on the same site). Note that this is only a problem if you want to run the same service with pox and json simultaneously. If all you want is to control the response format, you don’t need this workaround.

    The key concept behind my solution is to use 2 different URIs for the same service, then use an endpoint behavior to set the default outbound response format. You can find out more in this question, which also touches on the question conceptual purity: should we be using contract attributes to specify properties that are part of the network protocol? I think Marc Gravell’s take on this issue is valid per se, but it is not consistent the original concept of WCF, in which contracts are supposed to be abstracted away from the protocol stack. But endpoint behaviors won’t let you specify all REST related attributes, you’ll have to use the attributes for URI templates and inbound format.

    Could REST have been implemented differently? Although the designers of WCF did an excellent job of designing a generic framework, I don’t think they saw REST coming. Some things, like the URI template, really do seem to belong with the contract.

    Enough talk! Here’s the code. First the web.config file. This is where the magic happens. Note that I’m using WCF 4 configuration based activation in this example, but you could also accomplish the same thing by having 2 svc files to represent the 2 URIs.

    <?xml version="1.0"?>
    <configuration>
      <system.web>
        <compilation debug="true" targetFramework="4.0" />
      </system.web>
      <system.serviceModel>
        <services>
          <service name="StackOverflow.QuoteOfTheDayAsJson">
            <endpoint binding="webHttpBinding" contract="StackOverflow.IQuoteOfTheDay" 
                      behaviorConfiguration="jsonBehavior" />
          </service>
          <service name="StackOverflow.QuoteOfTheDayAsPox">
            <endpoint binding="webHttpBinding" contract="StackOverflow.IQuoteOfTheDay" 
                      behaviorConfiguration="poxBehavior" />
          </service>
        </services>
        <behaviors>
          <endpointBehaviors>
            <behavior name="jsonBehavior">
              <webHttp defaultOutgoingResponseFormat="Json" />
            </behavior>
            <behavior name="poxBehavior">
              <webHttp defaultOutgoingResponseFormat="Xml"/>
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="false">
          <serviceActivations>
            <add relativeAddress="QuoteOfTheDayJson.svc" 
                 service="StackOverflow.QuoteOfTheDayAsJson"/>
            <add relativeAddress="QuoteOfTheDayPox.svc" 
                 service="StackOverflow.QuoteOfTheDayAsPox"/>
          </serviceActivations>
        </serviceHostingEnvironment>
      </system.serviceModel>
    </configuration>
    

    And here’s the code, including service contract, service implementation, and the type aliases that are necessary to make this work:

    namespace StackOverflow
    {
        [DataContract]
        public class Quotation
        {
            [DataMember]
            public string Text { get; set; }
    
            [DataMember]
            public string Author { get; set; }
        }
    
        [ServiceContract]
        public interface IQuoteOfTheDay
        {
            [OperationContract]
            [WebInvoke(Method="GET", UriTemplate="GetTodaysQuote")]
            Quotation GetTodaysQuote();
        }
    
        public class QuoteOfTheDayImp : IQuoteOfTheDay
        {
            public Quotation GetTodaysQuote()
            {
                return new Quotation()
                {
                    Text = "Sometimes it's better to appologize for not asking permission",
                    Author = "Admiral Grace Murray Hopper"
                };
            }
        }
    
        /// <summary>
        /// A type alias used for json activation
        /// </summary>
        public class QuoteOfTheDayAsJson : QuoteOfTheDayImp 
        {}
    
        /// <summary>
        /// A type alias used for pox activation
        /// </summary>
        public class QuoteOfTheDayAsPox : QuoteOfTheDayImp
        {}
    }
    

    If it weren’t for the necessity of the type aliases, I’d say this is a complete solution. It is a complete solution if you don’t want to support multiple formats simultaneously. This is superior to the multiple contract solution in the respect that, since there is only one contract, you don’t need to keep the 2 contracts in sync.

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

Sidebar

Related Questions

I have silverlight 2.0 application and which uses WCF service to get data and
I am working on providing a generic JSON serialiation/deserialization service as a WCF service.
I have a WCF service that has been working perfectly, and something has changed
In a WCF service, I have two classes with the [DataContract] attribute. One of
On Solaris, in order to get the msg_control field in struct msghdr and have
What's the best way to write an application in order to get it working
I have a WCF contract with circular references. For a simple parent-child relationship, the
I have WCF service with IParameterInspector class which suppose to store the message for
I have written a WCF service with the REST template that has the defaultOutgoingResponseFormat
I'm trying to call web service for few hours. I have added clientaccesspolicy.xml: <?xml

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.