1

I have a client in Android and iphone...I need to write server side JSON Enabled WCF service with c#. How to create JSON Enabled WCF service?

1
  • Can you please create a new tag, etxt, for this post? It's a new API platform we're launching for developers in Africa and this question/answer is very relevant to them. link Commented Feb 3, 2012 at 10:54

3 Answers 3

3

there's not a whole lot of difference in writing JSON enabled stuff vs standard WCF. I'm guessing you want a REST API (my WCF services for android worked REST), which means your calls are GET requests as opposed to HTTP posts, using the URL as a way of passing parameters:

http://example.rest.com/myservice/categories/en/videos

where "en" and "videos" would be the parameters you wanted to use on your URL.

WCF works off an interface, the interface defines the service contract. For REST services, you can specify the JSON / URL format as below:

[ServiceContract()] // Required: this is a WCF endpoint
public interface IMyService
{
     [OperationContract()] // Required so the method actually is included
     [WebGet(
          ResponseFormat = WebMessageFormat.Json, // Return results as JSON
          UriTemplate = "/categories/{language}/{category}")]
     CategoryResponse Find(string language, string category);
}

The URI template category forms your URI structure, saying that when somebody hits your service, with a "categories" then two values separated by slashes, it will call this Find method, passing the first parameter as the language parameter and the category parameter will equal the second:

e.g: http://yourdomain/yourservice.svc/categories/it/bob

will call this method, passing "it" as language, and "bob" as category.

The return object is just a standard datacontract, if you want to control the format just use the DataContract + DataMember attributes (have named parameters like name, namespace, order etc).

Then the final part is just to configure your service properly, in this case

a) define webHttp service behavior:

<behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>          
        </behavior>        
      </endpointBehaviors>

and b) define your endpoint using webHttpBinding (and using the endpoint behaviour defined above: see we set behaviourConfiguration = "web"):

    <services>     
          <service behaviorConfiguration="standard"
 name="Your.Implementing.ClassThatImplementsIMyService">
            <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding"
                      contract="Namespace.To.IMyService" />
          </service>
        </services>

and that's basically it...

Sign up to request clarification or add additional context in comments.

Comments

0

find here

http://dotnetslackers.com/articles/ajax/JSON-EnabledWCFServicesInASPNET35.aspx

Comments

-1

I found this to be helpful when working on a similar project - http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.