Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable WCF Service to use with JSON

Tags:

json

c#

.net

wcf

I have created a wcf service. That is working fine when i am using simply in .net by adding as a webservice. But i want to make it able to use for iPhone app as JSON call. For testing i have used it in .net with JSON but its not working.

i know this kind of question is asked before, i have looked in for this cant find solution for me.

my configuration:

<system.serviceModel>
<behaviors>
  <serviceBehaviors>
    <behavior name="servicebehavior">
      <serviceMetadata httpsGetEnabled="true" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="endpointBehavior">
      <enableWebScript />
      <webHttp defaultBodyStyle="Wrapped" defaultOutgoingResponseFormat="Json" />
    </behavior>
  </endpointBehaviors>

</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
  <service name="MyService" behaviorConfiguration="servicebehavior">
    <endpoint address=""
              behaviorConfiguration="endpointBehavior"
              binding="webHttpBinding"
              contract="IMyService" />
  </service>
</services>

interface code:

[ServiceContract]
public interface IGolfPyramidService
{



    [WebInvoke(UriTemplate = "/Test", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    [OperationContract]
    string Test();

}

Myservice.cs code:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService : IMyService
{        
    public string Test()
    {
        return "success";
    }
}

i want to make it possible to call the method using url format like : http://example.com/MyService.svc/test

like image 920
Finisher001 Avatar asked Apr 22 '13 10:04

Finisher001


People also ask

Can WCF use JSON?

WCF supports serializing data in JSON format.

Can we return JSON from WCF service?

WCF has option to send the response in JSON object. This can be configured with WebGet or WebInvoke attribute. In this sample we can create the sample RESTful service to expose the method to read/add/update/delete the employee information.


1 Answers

if you are beginner then this will guide you create json and xml enabled web service which can be consumed by IOS and android.
http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide

like image 146
Arshad Avatar answered Sep 17 '22 19:09

Arshad