I've figured out how to create a WCF service exposed internally but have absolutely no idea how to consume this service from another web role or web app?
I have come across some examples that answer this exact scenario but the links explaining more info all seem to be dead.
Anyone able to help?
Let us imagine this scenario:
You have a WCF services hosted in a worker role with internal endpoint. It implements the contract ICalculator which is a simple Calculator(ADD,Mult...etc).
on the other end you have a Webform app when button Clicked you just send some data to be calculated.
here is the code for consuming this webservice:
protected void Button1_Click(object sender, EventArgs e)
{
var factory = new ChannelFactory<WorkerHost.ICalculator>(new NetTcpBinding(SecurityMode.None));
var channel = factory.CreateChannel(GetRandomEndpoint());
Label3.Text =channel.Add (Convert.ToInt32(TextBox1.Text) , Convert.ToInt32(TextBox2.Text)).ToString();
}
and the code for getRanDomeEndpoint() is
private EndpointAddress GetRandomEndpoint()
{
var endpoints= RoleEnvironment.Roles["WorkerHost"].Instances.Select(i=>i.InstanceEndpoints["CalculatorService"].IPEndpoint).ToArray();
var r = new Random(DateTime.Now.Millisecond);
return new EndpointAddress(string.Format("net.tcp://{0}/Calculate", endpoints[r.Next(endpoints.Count() - 1)]));
}
I have uploaded the service on azure check it out here