How to write JSON Enabled WCF service for server side in asp.net with c#
I h开发者_Go百科ave 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?
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...
find here
http://dotnetslackers.com/articles/ajax/JSON-EnabledWCFServicesInASPNET35.aspx
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/
精彩评论