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

Hosting a WCF REST Service with WebServiceHost

Aug 11 2010 1:00AM by Dhananjay Kumar   

WebServiceHost  factory helps us to host a WCF REST Service in  managed application.  This class is derived from ServiceHost class and automatically enables webHttpBinding  and webHttpBehavior on the service End points.

In this article, I will explain

  1. What is WebServiceHost Factory class?
  2. What is its function?
  3. Where to use it?
  4. One sample on hosting WCF REST Service using WebServiceHost.

What is WebServiceHost factory?

  1. This is useful to host WCF REST End points.
  2. This is derived from ServiceHost.
  3. WebServiceHost has some extra functionality.
  4. WebServiceHost works exactly the same way ServiceHost works.

WebServiceHost factory perform following special task

  • It disables meta data
    • It disables WSDL
    • It disables end point
    • It prevents meta data publishing
  • Autometically create End Point
    • It autometically creates the end point for all contracts using webHttpBinding
    • No need to call AddServiceEndPoint
  • Autometically add WebHttpBehaviour
    • It adds behavior to all end point
    • adding behavior ensures URI+Verb routing will work for all incoming requests

Where to use it?

Where to use it?

How to choose between ServiceHost and WebServiceHost?

How to choose between ServiceHost and WebServiceHost?

Example

In this example, I will show you step by step, how we can create a REST Service and host it in a console application using WebServiceHost factory. Below section is taken from my previous posted article on Hosting WCF REST Service in Console Application.

Follow the steps as below,

Step1

Create a New project. Select Console application as project type.

New Project

Step 2

Add a new project to same solution. Choose the project type as class library.

Class Library

Step 3

Add below references in both projects console and service library

System.ServiceModel;
System.ServiceModel.Description;
System.ServiceModel.Web;

Add Refrence

If you are not able to get System.ServiceModel.Webdll by default , when you are adding as Add Reference in your console application project and class library project , then follow the below steps

  1. Right click on your console application project or class library project
  2. Select properties from context menu
  3. From Application tab, if you see by default .NET Framework 4 Client profile is selected. Change that to .NET Framework 4.0. So your target framework should be .NET Framework 4.0.

>net Framework4

Now you should able to see all the dll when you add service reference in project.

Step 4

In this step, I will create contract of service

  1. Open the Contract (Class Library project).
  2. Delete Class1.
  3. Right click and add a new item then select Interface from Code tab.

    Create a contract
  4. Make Interface as public and put ServiceContract attribute.
  5. Declare two operation contracts. Make one attributed with WebGet and another attributed with Web Invoke.

IService.cs

using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.ServiceModel;
usingSystem.ServiceModel.Web; 

namespace Contracts
{
    [ServiceContract]
    publicinterfaceIService
    {
        [OperationContract]
        [WebGet]
        stringGetMessage(stringinputMessage);

        [OperationContract]
        [WebInvoke]
        stringPostMessage(stringinputMessage);
    }
}

Step 5

In this step, I will implement the contract in Service file. To do so,

  1. Right click and add a class in Console application.

    Implement contract
  2. Give any name; I am giving name here Service of the class.
  3. Implement the interface (Contract IService) in this class.

Service.cs

using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
using Contracts;

namespaceSelfHostedRESTService
{
    publicclassService :IService
    {       
        publicstringGetMessage(stringinputMessage)
        {
            return"Calling Get for you " + inputMessage; 
        }

        publicstringPostMessage(stringinputMessage)
        {
            return"Calling Post for you " + inputMessage; 
        }
    }
}

Step 6

In this step, I will host the service in a console application. So to do so

  1. Open Program.cs
  2. Create instanced of WebServieceHostFactory
    WebServiceHost host = new WebServiceHost(typeof(Service),
    			new Uri("http://localhost:8000"));
  3. Add a service end point 
    ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IService),
    		     new WebHttpBinding(), "");	
  4. Add service debug behavior 
    ServiceDebugBehavior stp=host.Description.Behaviors.Find<ServiceDebugBehavior>()
    stp.HttpHelpPageEnabled = false;

 

As we know, REST service used webHttpBindding. And we need to make sure that HttpHelpPageEanbled is false.

Program.cs

using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.ServiceModel;
usingSystem.ServiceModel.Description;
usingSystem.ServiceModel.Web;
using Contracts; 

namespaceSelfHostedRESTService
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
            WebServiceHost host = newWebServiceHost(typeof(Service), newUri("http://localhost:8000"));
            ServiceEndpointep = host.AddServiceEndpoint(typeof(IService),newWebHttpBinding(), "");
            ServiceDebugBehaviorstp=host.Description.Behaviors.Find();
            stp.HttpHelpPageEnabled = false;
            host.Open();
            Console.WriteLine("Service is up and running");
            Console.WriteLine("Press enter to quit ");
            Console.ReadLine();
            host.Close();
        }
    }
}

Step 7

Just press F5 to run the service.

Run the service

Now once service is up and running, I can test,

[OperationContract]
[WebGet]
stringGetMessage(stringinputMessage);

In browser.Because it is attributed with WebGet. To test this, I need to browse to URL, http://localhost:8000/GetMessage?inputMessage=dj

If you see WebServiceHost , I am hosting my REST service in http://localhost:8000/ and then I need to append the method name and then parameter I need to pass.

Webservicehost

Step 8

In this step, I will create a Console client which will consume the REST client. So to do so , follow the below steps

  1. Create a console project.
  2. Add reference of System.ServiceModel and System.ServiceModel.Web (See Step 3).
  3. Add reference of Contract class library (we created in Step 2)
  4. Write below code in Program.cs

Program.cs

using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.ServiceModel;
usingSystem.ServiceModel.Web;
usingSystem.ServiceModel.Description;
using Contracts; 
namespaceClientForRESTSelfHosted
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
             ChannelFactorycf = newChannelFactory(newWebHttpBinding(), "http://localhost:8000");
             cf.Endpoint.Behaviors.Add(newWebHttpBehavior());
             IService channel = cf.CreateChannel();
             Console.WriteLine(channel.GetMessage("Dhananjay Get"));
             Console.WriteLine(channel.PostMessage("Dhananjay Post"));
             Console.Read();
        }
    }
}

Explanation

  1. I am creating an instance of Channel Factory.
  2. I am passing IService (from dll of Contract class library) as type to channel factory.
  3. I am passing as parameter type of binding that is WebHttpBinding and address.
  4. Address is exactly as same as of the address was in host instance.
  5. I am creating a channel of type IService1.
  6. Calling the methods on the channel.

Press F5 to run the application,

Application

I hope this post was useful. Thanks for reading. Happy coding.

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


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



Submit

Your Comment


Sign Up or Login to post a comment.

    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]