Freigeben über


Implementing REST service in WCF hosted in WorkerRole

I spent some time comparing WCF and WebAPI as frameworks for building the services and hosting them in Azure as WorkerRoles. There were some questions and I decided to implement a simple service in both frameworks just to feel the difference.

 

Just to recap, our goal is to build a sample REST service, in this post, using WCF hosted in Azure.

   

Initial setup

  1. Install the Azure SKD 2.2
  2. Create a cloud service project with worker role WcfWorkerRole

 

Define and implement a contract

  1. Create a folder 'Contract' in the WcfWorkerRole and add there operation service and data contract definition. 
  2. Create a folder 'Impl' in the WcfWorkerRole and implement the service as following:

Ctrl+click to enlarge the picture.

Expose the service to be accessible via REST

 Let's expose the service on the address https://localhost:8080/wcf. That's the pure the configuration topic, see app.config

 

 

Selfhost WCF service in the worker role

It's necessary to use WebServiceContext to selfhost the service within worker role.

 

 

Run the application using local full mode emulator

First catch: restart the studio with administrator rights.

 

Open the port

In order to expose the service correctly, we need to declare in .csdef file that our service will accept the incoming HTTP requests on port 8080. 

Let's see the application locally running inside the emulator.

 

After navigating to the URL: https://127.0.0.3:8080/wcf//Products/GetAll as we see that the service is running correctly. Let's host it in Azure.

Moving it to Azure

After publishing the service to Azure (right-click on the project Demo.Azure) we can test our service.

But the service is not running, it is not accessible, at all. You can find in the logs that

Could not start WCF service host. HTTP could not register URL https://+:8080/. Your process does not have access rights to this namespace (see https://go.microsoft.com/fwlink/?LinkId=70353 for details).; TraceSource 'WaWorkerHost.exe' event

 

The problem is that the port 8080 is not publicly open and it's necessary explicitly allow the port to be open.

There are two possible solutions:

  1. write a startup task which opens the port 
  2. run the webrole instance with the elevated permissions

I used the second choice which required adding the following line into the csdef file.

 

 

Republish the project to Azure

After republishing and the service is running correctly.

 

In the next blog post I'll write about building the same service using WebAPI 2.

Comments

  • Anonymous
    May 27, 2014
    I've created your sample WCF REST Azure service. I'm able to run it locally. I've deployed to Azure but am not able to access the service. I'm using the following URL; vincentr-rest.cloudapp.net/.../GetAll Is this the correct URL? I've looked in waAppAgent log file and don't see any errors. Thanks, Vincent vincent2@roggero.com