locked
json on WCF service application RRS feed

  • Question

  • I'm building a wcf service using WCF Service Application template.

    I want my WCF service able to return a json format of data.

    After I have run my wcf service, I tried to type in the URL to get the data, but nothing but 400 status code return.

    Service1 class:

    [WebGet]
            public string GetData(int value)
            {
                return String.Format("This value is {0}", value);
            }

    IService:

    [OperationContract]
            [WebGet(UriTemplate="/data?id={value}", ResponseFormat=WebMessageFormat.Json)]
            string GetData(int value);

    The URl for the wcf is http://localhost:44330/Service.svc.

    What is the correct url for me to call the wcf and return the json-formatted data?

    Thursday, November 1, 2012 4:15 AM

Answers

  • Please look into this url

    http://stackoverflow.com/questions/2086666/wcf-how-do-i-return-clean-json


    With Thanks and Regards
    Sambath Raj.C
    click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you
    Happy Programming!

    • Proposed as answer by LeoTang Friday, November 2, 2012 3:15 AM
    • Marked as answer by LeoTang Sunday, November 11, 2012 8:59 AM
    Thursday, November 1, 2012 9:57 AM
  • Hi,

    In this case, you have duplicate [webget] attribute. If you want to use UriTemplate, please remove the one on service implementation as below:

            public string GetData(int value)
            {
                return String.Format("This value is {0}", value);
            }

    Then the url to call servie operation will be:

    http://localhost:44330/Service.svc/data?id=1

    Also, please remember to setup configuration for webHttpBinding in web.config file, it looks like:

      <system.serviceModel>
        <services>
          <service name="WcfService1.Service1">
            <endpoint binding="webHttpBinding" behaviorConfiguration ="Jason" contract="WcfService1.IService1">          
            </endpoint>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
              <serviceMetadata httpGetEnabled="true"/>
              <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="Jason">
              <webHttp/>
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
      </system.serviceModel>

    Leo Tang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by LeoTang Sunday, November 11, 2012 8:59 AM
    Friday, November 2, 2012 3:15 AM

All replies

  • Please look into this url

    http://stackoverflow.com/questions/2086666/wcf-how-do-i-return-clean-json


    With Thanks and Regards
    Sambath Raj.C
    click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you
    Happy Programming!

    • Proposed as answer by LeoTang Friday, November 2, 2012 3:15 AM
    • Marked as answer by LeoTang Sunday, November 11, 2012 8:59 AM
    Thursday, November 1, 2012 9:57 AM
  • Hi,

    In this case, you have duplicate [webget] attribute. If you want to use UriTemplate, please remove the one on service implementation as below:

            public string GetData(int value)
            {
                return String.Format("This value is {0}", value);
            }

    Then the url to call servie operation will be:

    http://localhost:44330/Service.svc/data?id=1

    Also, please remember to setup configuration for webHttpBinding in web.config file, it looks like:

      <system.serviceModel>
        <services>
          <service name="WcfService1.Service1">
            <endpoint binding="webHttpBinding" behaviorConfiguration ="Jason" contract="WcfService1.IService1">          
            </endpoint>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
              <serviceMetadata httpGetEnabled="true"/>
              <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="Jason">
              <webHttp/>
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
      </system.serviceModel>

    Leo Tang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by LeoTang Sunday, November 11, 2012 8:59 AM
    Friday, November 2, 2012 3:15 AM