Before we continue, i assume you already know how to create a helloworld WCF web service, also know how to deploy application onto Windows Azure and know what is input endpoint.
Step 1 - Create a WCF Service
When creating a WCF service, always we need to define a contract and them implement it.
There is one thing i need to raise. When implementing the contract, and if you want to use any other binding rather than BasicHttpBinding, we need to specify
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WorkerRole1
{
[ServiceContract]
public interface IMessageDeliver
{
[OperationContract]
void DoWork(string message);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;
namespace WorkerRole1
{
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class MessageDeliver : IMessageDeliver
{
public void DoWork(string message)
{
Trace.TraceInformation("{0} Receive Message - {1}.", RoleEnvironment.CurrentRoleInstance.Role.Name, message);
}
}
}
Step 2 - Open a port to listen
In the ServiceDefinition.csdef, we need to define a InputEndpoint, so that the outside world can talk to our worker role.
I am going to use NetTcpBinding, so i make the port as tcp. The other options you can have are http and https, which need to be match to the binding u are going to use.
Step 3 - Combine WCF with the port we open
ServiceHost serviceHost = new ServiceHost(typeof(MessageDeliver));
NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
// define an external endpoint for client traffic
RoleInstanceEndpoint externalEndPoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["MessageDeliver"];
string endpoint = String.Format("net.tcp://{0}/MessageDeliver", externalEndPoint.IPEndpoint);
serviceHost.AddServiceEndpoint(typeof(IMessageDeliver), binding, endpoint);
serviceHost.Open();
Step 4 - Create a client to double check
ChannelFactoryDone !!! Have fun ...cfactory = new ChannelFactory (new NetTcpBinding(SecurityMode.None), "net.tcp://accountname.cloudapp.net:10080/MessageDeliver");
var client = cfactory.CreateChannel();
client.DoWork("hi from shrimpy");