Menu ▼

Home / blogs / Dotnet / WCF /

This example shows you how to create wcf rest api for get method step by step.

Declare OperationContract and set WebInvoke properties to the method.

[OperationContract]
[WebInvoke(Method = "GET",
    BodyStyle = WebMessageBodyStyle.Wrapped,
    ResponseFormat = WebMessageFormat.Json,
    UriTemplate = "ServerTimeInJSON")]
string ServerTimeInJSON();

In the above WebInvoke Properties
Method will be used to mention weather method is POST or GET
BodyStyle will be used to mention request and response can be wrapped or not
RequestFormat will be used to set the Request will be in format of XML or JSON
ResponseFormat will be used to set the Request will be in format of XML or JSON
UriTemplate will be used to set the URL to call the operation contract, it can be different from the OperationContract name.

Web.Config

Declare Service, end point under <system.serviceModel> section,
and end point behaviour under <system.serviceModel> -> <behaviors> section.
end point binding should be webHttpBinding and declare the endpoint behavior as webHttp.

<services>
  <service name="RestAPI.RestService" >
    <endpoint address="" binding="webHttpBinding" contract="RestAPI.IRestService" behaviorConfiguration="restbehavior" ></endpoint>
  </service>
</services>

  <endpointBehaviors>
    <behavior name="restbehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>

Example

RestApiJSONExample

RestApiXMLExample

Service & Operation Contract Interface:

[ServiceContract]
public interface IRestService
{

    [OperationContract]
    [WebInvoke(Method = "GET",
        BodyStyle = WebMessageBodyStyle.Wrapped,
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "ServerTimeInJSON")]
    string ServerTimeInJSON();

    [OperationContract]
    [WebInvoke(Method = "GET",
        BodyStyle = WebMessageBodyStyle.Wrapped,
        ResponseFormat = WebMessageFormat.Xml,
        UriTemplate = "ServerTimeInXML")]
    string ServerTimeInXML();

}

Interface Implementation:

public class RestService : IRestService
{
    public string ServerTimeInJSON()
    {
        return DateTime.Now.ToString();
    }

    public string ServerTimeInXML()
    {
        return DateTime.Now.ToString();
    }
}

And declare the web.config as discussed above.
Now you can run your first wcf rest api using get method.

you can download the source code form here... Download

Read More...
Submited By : mrkraju On 20 Sep 2014
SOA - Service Oriented Architecture has evolved from the Distributed Programming weakness and to overcome the limitations of the Distributed programming.
 
SOA is a software design and software architecture design pattern for distributed programming providing application functionality as services to other applications. It is independent of any vendor, product or technology. It is a standard as how services has to be written and consumed.
 
SOA or Service oriented architecture is an architecture style for building business applications by means of services. Application comprises collection of services which communicate through messages.
 
SOA is based on the concept of a service. 
 
An endpoint is the entry point for such a SOA implementation.
SOA specifies that the services requires loose coupling. Loose Coupling means they are mostly independent. Generally, loose coupling is recommended because it's easier to test and maintain.
 
Tenets of SOA
There are 4 tenets of SOA
  1. Boundaries are explicit: Services communicate by sending messages across boundaries. These boundaries are formal and explicit. The methods that a client may use shall be easy to use and well defined, so that the Services should be easy to consume.
  2. Services are Autonomous: Services shall not be dependent on others. Change of one service shall not affect another in any way.
  3. Services share schema and contract but not class: A scheme represent the data that will be sent, contract contains the defined methods for a service. To make a system loose coupled you share scheme and contract instead of classes and objects.
  4. Services compatibility is based on policy: A policy to use a service may be that a particular type of binding is required so it may be used. Anyone that want to use this service, must connect to it with this type of binding 
WCF is a Microsoft framework for building Service-Oriented applications.



Read More...
Submited By : Krishna Narra On 01 Dec 2013




Search dotnetlearners.com