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");
This is very useful. If you happened to have a little sample project it would be very very useful.
ReplyDeleteThanks,
Don Rule
Hi Don,
ReplyDeleteYeah. Good suggestion. But I do not have any meaningful stuff in my mind.
If u have any idea. Maybe we can work together to creat a project.
Haha looking forward to it.
Thanks,
Xiaomin WU
Have you gotten this to work with Http in the cloud? Could you attach a sample project?
ReplyDelete@Jason
ReplyDeleteSorry for the late reply.
Ok..i will do it... there were some changes as what i aware.... i will update this blog post Once i got some time..
can you create an instance of the ChannelFactory class?? I'm getting an exception on doing so.
ReplyDelete