Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
BRH 58
#ASP.NET 55
ASP.NET 50
#DOTNET 49
.NET 40
WCF 21
DOTNET 12
c# 8
windows azure 7
SILVERLIGHT 7

Archive · View All
April 2011 9
March 2011 9
February 2011 8
December 2010 7
November 2010 5
September 2010 5
August 2010 5
May 2011 4
October 2010 4
January 2011 2

Walkthrough creating REST Service in WCF 4.0

Feb 1 2011 7:29AM by Dhananjay Kumar   

Objective

This article will explain step to step explanation of, how to create a REST based service and how to consume that in a managed client.

Step 1

Create a new project as WCF Service application type.


Step 2

Delete all the default code from IService.cs and Service.svc.cs

Now open Web.Config file and delete below highlighted code in Visual Studio 2010. In other words delete all endpoints for existing service.

<system.serviceModel>
    <services>
      <service behaviorConfiguration="RestServicePublishing.Service1Behavior"
        name="RestServicePublishing.RestService">
        <endpoint address="" binding="wsHttpBinding" contract="RestServicePublishing.IRestService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="RestServicePublishing.Service1Behavior">
         <!-- 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>
    </behaviors>
  </system.serviceModel>

If you are using VS2010 then delete below code from Web.Config

<serviceHostingEnvironment multipleSiteBindingsEnabled = "true" />

Step 3

Right click on Service.svc and open View markup.

In Markup of RestService.cs , add below code there

Factory="System.ServiceModel.Activation.WebServiceHostFactory"

So after adding code the markup would look like

Markup of RestService.cs

<%@ServiceHost Language="C#" 
Debug="true"
Service="WcfService3.Service1"
Factory="System.ServiceModel.Activation.WebServiceHostFactory"
%>

Step 4

Add following references to the WCF Service Application project, if you are using VS2010

Microsoft.Http.dll

Microsoft.Http.Extension.dll

System.ServiceModel.Web.dll

Step 5

Add a new project as of type class library and give it name UtilityClasses.

Add a class Number to this class library.

Number.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UtilityClasses
{
    [Serializable]
    public class NumberService
    {
        public int Number1 { get; set; }
        public int Number2 { get; set; }
    }
}

Step 6

Define the Service Contract as below,

IService1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;
using UtilityClasses;

namespace WcfService3
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract(Name="AddParameter")]
        [WebInvoke(UriTemplate = "/",Method="POST")]
        int  Add(NumberService n1);
        [OperationContract(Name = "Add")]
        [WebGet(UriTemplate = "/")]
        int Add();
    }    
}

This code is used to construct URI for REST service.

[WebInvoke(UriTemplate = "/",Method="POST")]

Method parameter says what type of HTTP request; this URI is going to entertain. In this case it is Http POST.

UriTemplate parameter says, what would be URI for this particular method. In this case it is one back slash means; it is root URI of this service.

So, to invoke this method add (), the URI address would be

http://localhost:3602/RestService.svc/

Where 3602 is port number of web server, where this service is running.

Let, if the above code is modified as

[WebInvoke(UriTemplate = "/Add/MyAdd",Method="POST")]

Then it would be invoked as

http://localhost:3602/RestService.svc/ADD/MyAdd

Step 7

Now we need to implement the service

Service1.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using UtilityClasses; 

namespace WcfService3
{
    public class Service1 : IService1
    {
        public int res = 100;

        public int Add(NumberService n1)
        {
            res = Convert.ToInt32(n1.Number1) + Convert.ToInt32(n1.Number2);
            return res;
        }
        public int Add()
        {
            return res;
        }
    }
}

Step 8

Test the Service in Browser

  1. Build the Service
  2. Right click on Service1.svc and select view in browser.

Output 100 is returned in the browser by the service. It is because, below service method. URI of below GET method is mapped to root of the URI. And this method is returning 100.

[WebGet(UriTemplate = "/")]
int Add();

Step 9

Consume the service

Add new console project in solution and give it name as ConsoleTestProject.

Add reference of project UtilityClasses Add following references to the console project.

Microsoft.Http.dll

Microsoft.Http.Extension.dll

System.ServiceModel.Web.dll

Step 10

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Microsoft.Http; 
using Microsoft.ServiceModel.Web; 
using Microsoft.ServiceModel.Web.SpecializedServices; 
using System.Runtime.Serialization;
using UtilityClasses;

namespace ConsoleTestProject
{
    class Program
    {
        static void Main(string[] args)
        {

            uri = "http://localhost:3602/RestService.svc/";
            // Calling without parameter
            Console.WriteLine(AddWithoutParameter());
            Console.Read();
            //Calling with parameter
            NumberService obj = new NumberService() { Number1 = 7, Number2 = 2 };
            Console.WriteLine(AddWithParameter(obj));
            Console.Read();     

        }

        public static string  AddWithoutParameter()
        {
            using (HttpResponseMessage response = new HttpClient().Get(uri))
            {
               
                int res = response.Content.ReadAsDataContract();
                return res.ToString();
            }
        }

        public static string  AddWithParameter(NumberService obj)
        {
            using (HttpResponseMessage  response = new HttpClient().Post(uri,HttpContentExtensions.CreateDataContract(obj)))
            {
                
                int res = response.Content.ReadAsDataContract();
                return res.ToString();
            }

        }

        }
    }
}

There are two static methods

AddWithoutParameter() -> To Invoke HTTP Get on Service URI. {In Green}

AddWithParameter() -> To Invoke HTTP POST on Service URI. {In Yellow}

Output

Tags: #DOTNET, #WCF, WCF, #ASP.NET, ASP.NET, BRH, .NET,


Dhananjay Kumar
49 · 4% · 1198
2
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

1  Comments  

  • It does not work and it fails on compiling on the "program.cs". It fails on the "int res = response.Content.ReadAsDataContract();" saying that "The type arguments for method "System.Runtime.Serialization.DataContractContentEXtensions.ReadAsDataContract(MIcrosoft.Http.HttpContent)" cannot be inferred from the usage. Try specifying the type arguments explicitly.

    commented on Jan 18 2012 4:26PM
    pmak
    2701 · 0% · 3

Your Comment


Sign Up or Login to post a comment.

"Walkthrough creating REST Service in WCF 4.0" rated 5 out of 5 by 2 readers
Walkthrough creating REST Service in WCF 4.0 , 5.0 out of 5 based on 2 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]