Step 4: Host the REST-based WCF Service to use the Service Bus

Important

This tutorial is moving to a new location, and this version will soon be retired. You can view the most recent version here: https://azure.microsoft.com/en-us/documentation/articles/service-bus-relay-rest-tutorial/.

This is the fourth of four tasks required to create a basic REST-based Service Bus service. For an overview of all four tasks, see Service Bus REST Tutorial. This topic describes how to run a web service using a console application on Service Bus. A complete listing of the code written in this task is provided in the example following the procedure.

Estimated time to completion: 10 minutes

To create a base address for the service

  1. In the Main() function declaration, create a variable to store the service namespace of your Service Bus project.

    string serviceNamespace = "InsertServiceNamespaceHere";
    

    Service Bus uses the name of your namespace to create a unique URI.

  2. Create a Uri instance for the base address of the service that is based on the namespace.

    Uri address = ServiceBusEnvironment.CreateServiceUri("https", serviceNamespace, "Image");
    

To create and configure the Web service host

  1. Create the Web service host, using the URI address created earlier in this section.

    WebServiceHost host = new WebServiceHost(typeof(ImageService), address);
    

    The service host is the WCF object that instantiates the host application. This example passes it the type of host you want to create (an ImageService), and also the address at which you want to expose the host application.

To run the Web service host

  1. Open the service.

    host.Open();
    

    The service is now running.

  2. Display a message indicating that the service is running, and how to stop the service.

    Console.WriteLine("Copy the following address into a browser to see the image: ");
    Console.WriteLine(address + "GetImage");
    Console.WriteLine();
    Console.WriteLine("Press [Enter] to exit");
    Console.ReadLine();
    
  3. When finished, close the service host.

    host.Close();
    

Example

The following example includes the service contract and implementation from previous steps in the tutorial and hosts the service in a console application. Compile the following code into an executable named ImageListener.exe:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Web;

namespace Microsoft.ServiceBus.Samples
{
    
    [ServiceContract(Name = "ImageContract", Namespace = "https://samples.microsoft.com/ServiceModel/Relay/")]
    public interface IImageContract
    {
        [OperationContract, WebGet]
        Stream GetImage();
    }

    public interface IImageChannel : IImageContract, IClientChannel { }

    [ServiceBehavior(Name = "ImageService", Namespace = "https://samples.microsoft.com/ServiceModel/Relay/")]
    class ImageService : IImageContract
    {
        const string imageFileName = "image.jpg";

        Image bitmap;

        public ImageService()
        {
            this.bitmap = Image.FromFile(imageFileName);
        }

        public Stream GetImage()
        {
            MemoryStream stream = new MemoryStream();
            this.bitmap.Save(stream, ImageFormat.Jpeg);

            stream.Position = 0;
            WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";

            return stream;
        }    
    }

    class Program
    {
        static void Main(string[] args)
        {
            string serviceNamespace = "InsertServiceNamespaceHere";
            Uri address = ServiceBusEnvironment.CreateServiceUri("https", serviceNamespace, "Image");

            WebServiceHost host = new WebServiceHost(typeof(ImageService), address);
            host.Open();

            Console.WriteLine("Copy the following address into a browser to see the image: ");
            Console.WriteLine(address + "GetImage");
            Console.WriteLine();
            Console.WriteLine("Press [Enter] to exit");
            Console.ReadLine();

            host.Close();
        }
    }
}

Compiling the Code

After building the solution, do the following to run the application:

  1. From a command prompt, run the service (ImageListener\bin\Debug\ImageListener.exe).

  2. Copy and paste the address from the command prompt into a browser to see the image.