Καλώς ορίσατε στο dotNETZone.gr - Σύνδεση | Εγγραφή | Βοήθεια

C# and .NET Tips and Tricks

Quests in programming in .NET

Ιανουάριος 2011 - Δημοσιεύσεις

A simple REST service in C#

In this post, we will see a simple implementation of a REST service.The service will consist of only one method and it can be consumed from your websites, directly from your browser or from a Desktop application.

 

When implementing a service you need to specify the following:

 

  • The service contract (the methods it offers).
  • How do you know which one to access from the URL given (URL Routing).
  • The implementation of the service.
  • How you will host the service.

 

Defining the Contract

Create a new Class Library Project and call it “RESTService.Lib”. Add references to “System.ServiceModel” and “System.ServiceModel.Web”. Create an Interface class called IRESTDemoServices and add the definitions of the methods that represent the services offered. Our interface will offer just one service as follows:

 

public interface IRESTDemoServices
{
    string GetClientNameById(string Id);
}

 

In order to tell the framework to treat this interface as a service we need to decorate it as follows:

 

[ServiceContract(Name = "RESTDemoServices")]
public interface IRESTDemoServices
{
    [OperationContract]
    string GetClientNameById(int Id);
}

 

Apparently we could offer more than one methods in our service by adding more methods in the interface.

 

Defining the URL to be used to access the service (URL Routing)

Although you can actually specify the routing within the interface it is better to create a new static class that will hold all your routing paths. Create a new static class named Routing and provide the following:

 

public static class Routing
{
    public const string GetClientRoute = "/Client/{id}";
}

 

Note the {id} element which specifies that the value supplied there should be matched with a parameter of the method of the interface. The connection of the URL Route to the method in the interface is achieved by decorating the interface with an attribute as follows:

 

[ServiceContract(Name = "RESTDemoServices")]
public interface IRESTDemoServices
{
    [OperationContract]
    [WebGet(UriTemplate = Routing.GetClientRoute, BodyStyle = WebMessageBodyStyle.Bare)]
    string GetClientNameById(string Id);
}

 

The WebGet attribute also specifies that the method will be accessed by a typical GET request to the specified URL.

 

Implementing the service

Implementing the service is as simple as creating a class that implements the service’s interface and decorating it with two specific attributes:

 

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, 
                 ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class RestDemoServices:IRESTDemoServices
{
    public string GetClientNameById(string Id)
    {
        Random r = new Random();
        string ReturnString="";
        for (int i = 0; i < Id; i++)
            ReturnString += char.ConvertFromUtf32(r.Next(65, 85));

        return ReturnString;

    }
}

 

This is of course dummy code just to return a string. In real life this is the place to access the database and request for the name of the client with this id.

 

And this is it! Your service is ready to be hosted.

 

Hosting your service

The simplest solution is to create a console application that will act as the server of your service. Create a new project “Console Application” and add the same references as in the class library project along with the reference to the class library project itself. There is a possibility that you may need to change the “Target Framework” of your app to .NET Framework 4 from the one with the client profile in order to be able to include the “System.ServiceModel.Web” assembly. In the main method you host the service as follows:

 

static void Main(string[] args)
{
    RestDemoServices DemoServices = new RestDemoServices();
    WebHttpBinding binding = new WebHttpBinding();
    WebHttpBehavior behavior = new WebHttpBehavior();

    WebServiceHost _serviceHost = new WebServiceHost(DemoServices, 
                                                     new Uri("http://localhost:8000/DEMOService"));
    _serviceHost.AddServiceEndpoint(typeof(IRESTDemoServices), binding, "");
    _serviceHost.Open();
    Console.ReadKey();
    _serviceHost.Close();
}

 

That is you initialize the service and then you host it under the root URL of http://localhost:8000/DEMOService. And off you go! Run the application and open your browser and type the following:

 

image

 

That’s it. Of course in a production environment you may have to open the port your service communicates.

Alternatively you may host this service in IIS. Create a new Empty Web Application Project. Add a reference to the class library project containing the Demo service and create an empty .svc file with the following contents (when you add it from the Add Item menu you will get some extra cs flies which you can safely delete):

 

<%@ ServiceHost Language="C#" Debug="true" Service="RESTService.Lib.RestDemoServices" 
                Factory="System.ServiceModel.Activation.WebServiceHostFactory"%> 

 

This is it. Right Click on the svc file and select “View in Browser”. You will achieve the same result as before:

 

image

 

Deploying the .svc file to a web server is similar to deploying a webpage.

 

This was a very simple demonstration of creating a web service. I haven’t shown any POST requests neither I have demonstrated session management. But apart from those, I think you will see that there will be a lot of times that a simple web service with only GET Requests will be the only one thing needed and this approach will come in handy.

 

Again the project for this post can be downloaded here

Shout it
Posted: Σάββατο, 29 Ιανουαρίου 2011 10:51 μμ από iwannis | 0 σχόλια
Δημοσίευση στην κατηγορία: ,
Interception and Interceptors in C# (Aspect oriented programming)
In this post, we see how to define specific actions to be executed before or after the execution of every call to a method in our code. This is also known as “ intercepting ” the execution of a method and is related to AOP (Aspect Oriented Programming). We will explore two different ways of achieving this:   Using Microsoft’s Unity framework Using the PostSharp library   CastleDynamicProxy is another library allowing you to intercept methods and resembles a lot to the Unity framework’s approach so it will not be described here. You can also use .NET framewrok’s Emit namespace or CodeDom to create your intercepting Proxy objects from scratch but this approach is out of the scope of this post. Aspect Oriented Programming deals with horizontal concerns in the specifications of your programs. Horizontal concerns are requirements that span through every business logic or specific behavior of your system. Take for example the following figure displaying some typical modules of an e-shop application. The classes

Διαβάστε περισσότερα »

Posted: Κυριακή, 16 Ιανουαρίου 2011 10:38 πμ από iwannis | 0 σχόλια
Δημοσίευση στην κατηγορία:
ListBoxes with DataBound Images in WPF
In this post, we see how we can add a DataBound image in the DataTemplate of a WPF ListBox. We experiment with three different approaches of achieving that and discuss on which one to use depending on the situation. In all cases, the image files are embedded within the application as “Resource” files. Later in this post, we will see what needs to be changed in order to support having the image files in a folder outside of the application Assembly. Our object for binding will be the class named Item having the following properties: public class Item { public int PictureID { get ; set ; } public string Name { get ; set ; } } We want to display a specific image depending on the value of the property PictureID. The ListBox in the .xaml file be: < ListBox ItemTemplate ="{ DynamicResource DataTemplateItem }" ItemsSource ="{ Binding Items }" /> The definition of the DataTemplate for the items of the ListBox is: < DataTemplate x : Key ="DataTemplateItem"> < Canvas

Διαβάστε περισσότερα »

Posted: Τετάρτη, 12 Ιανουαρίου 2011 9:19 μμ από iwannis | 0 σχόλια
Δημοσίευση στην κατηγορία: