Worker roles are mainly used for backgroung processes in
windows azure.We can host WCF services in worker role.
->Create new azure project and add new workerrole project.
->Open ServiceDefinition.csdef
file add new endpoint in workerrole tag.
<Endpoints>
<InputEndpoint name="Rest" protocol="http" port="80" />
</Endpoints>
->Add new IService interface.
Service Interface:
[ServiceContract]
public interface IService
{
[OperationContract]
[WebGet]
string SayHello(string
name);
}
->Add new Service class and implement IService
interface.
Service Implementation:
[AspNetCompatibilityRequirements(RequirementsMode
= AspNetCompatibilityRequirementsMode.Allowed)]
public class Service : IService
{
public string
SayHello(string name)
{
return "Hello "
+ name;
}
}
->Open WorkerRole.cs class,create serviceHost
varaible
and add CreateServiceHost
method to host our Wcf REST service.
//Srvice host varible.
private ServiceHost
serviceHost;
private void
CreateServiceHost()
{
//Url of service.
Uri httpUri = new Uri(String.Format("http://{0}/{1}", RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Rest"].IPEndpoint.ToString(), "Rest"));
//Assign url to service host.
serviceHost
= new ServiceHost(typeof(Service),
httpUri);
//Adding endpoint for REST service.
ServiceEndpoint ep =
serviceHost.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), "");
//Adding behavior for REST service.
ep.Behaviors.Add(new WebHttpBehavior()
{ HelpEnabled = true });
//Open the service host
to start listening for messages.
serviceHost.Open();
}
->Call CreateServiceHost function in OnStart()
method.
If you follow all these steps and upload it then you got
error like below.
HTTP could not register URL
http://+:80/Rest/. Your process does not have access rights to this namespace
(see http://go.microsoft.com/fwlink/?LinkId=70353 for details).
Source:System.ServiceModel.
Solution
to this problem is find out the port it needs to open and give the workerrole
permission to open the appropriate port.
->Create new console project PortDetect and configure
Target framework as 3.5.
->Open Program.cs file and add
below code which is used to detect the port and assign permission to open the appropriate
port.
class Program
{
static void Main(string[] args)
{
Process.Start("netsh",
string.Format("http
add urlacl url=http://+:{0}/Rest user=everyone listen=yes delegate=yes",
RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Rest"].IPEndpoint.Port.ToString()));
}
}
->Build the
project and get the executable into you workerrole
project.
->Set
the Copy to Output Directory property of
executable to Copy Always.
-> Open ServiceDefinition.csdef
file specify PortDetect.exe as a startup task.
<Startup>
<Task commandLine="PortDetect.exe" executionContext="elevated" taskType="simple">
</Task>
</Startup>
That’s it now you can access the
RestService which is hosted(at port 80)
on workerrole.
Let me know, if you have any feedback. Mail me for
source code. Enjoy reading my articles…
sekhartechblog@gmail.com
No comments:
Post a Comment