broadcasting a message to all worker and all web roles
-
27 ноября 2011 г. 0:34
I have 3 instances of a web role and 3 instances of a worker role and I want any one of the 6 to be able to boradcast a message to the other 5. As my code stands now, the 3 web instances can talk to each other, and likewise the worker instances, but I can't get between the role types. My code is below. I imagine this line is wrong,
.Select(instance => instance.InstanceEndpoints["RoleToRole"]);
and I wish there were something like this that would return all 6 of my roles that are all on the same internal endpoint using TCP
.Select(instance => instance.AllServiceEndpoints["RoleToRole"]);
thanks!
private void PokeAllWorkers()
{
var currentInstance = RoleEnvironment.CurrentRoleInstance;
var endPoints = currentInstance.Role.Instances
.Where(instance => instance != currentInstance)
.Select(instance => instance.InstanceEndpoints["RoleToRole"]);
foreach (var ep in endPoints)
{
IService1 worker = WorkerRole.factory.CreateChannel(new EndpointAddress(String.Format("net.tcp://{0}/Service1", ep.IPEndpoint)));
try
{
Trace.WriteLine(worker.SayHello(currentInstance.Id.ToString()), "Information");
}
catch (Exception e)
{
Trace.TraceError("Unable to poke worker role instance '{0}' . {1}", ep.RoleInstance.Id, e.Message);
((ICommunicationObject)worker).Abort();
}
}
}
Все ответы
-
27 ноября 2011 г. 2:02Отвечающий
currentInstance.Role is giving you access to only the current role. You need to use RoleEnvironment.Roles to gain access to all the roles in your service.
-
27 ноября 2011 г. 20:00
Sorry, but I need a bit more help. I am trying to get all 6 of my instanceEndpoints
with
var currentInstance = RoleEnvironment.CurrentRoleInstance;
I can get all the instances for this current role and do:
var endPoints = currentInstance.Role.Instances
.Where(instance => instance != currentInstance)
.Select(instance => instance.InstanceEndpoints["RoleToRole"]);
foreach (var ep in endPoints)
{
}
--------------------------------------------
RoleEnvironement WILL give me the list of Roles,
but then Roles doesn't seem to let me get to InstanceEndpoints.
RoleEnvironment.Roles.there-is-nothinguseful; It will give me the Roles, but then there is no Role.something that gets me to other roles's endpoints?
I need a
RoleEnvironment.Roles.SomeOtherRoleInstance that lets me get to all the endpoints for a role that I am not in?thanks
- Изменено kurtfr 27 ноября 2011 г. 20:03
-
28 ноября 2011 г. 3:50ОтвечающийSee the code sample in the MSDN page for Roles. It shows how to get the RoleInstance, from when you can get instance endpoints.
- Помечено в качестве ответа MingXu-MSFTModerator 4 декабря 2011 г. 16:02
- Снята пометка об ответе kurtfr 9 декабря 2011 г. 23:45
-
9 декабря 2011 г. 23:45
Sorry, I keep reading, and I find stuff like this:
You can use the Instances property to retrieve instances of a role. First use the CurrentRoleInstance to return a reference to the current role instance, and then use the Role property to return a reference to the role itself.
The Instances property returns a collection of RoleInstance objects. This collection always contains the current instance. If the role does not define an internal endpoint, the collection includes the current instance but no other instances. The number of role instances in the collection will always be 1 in the case where no internal endpoint is defined for the role. If the role defines an internal endpoint, its instances are discoverable at runtime, and the number of instances in the collection will correspond to the number of instances specified for the role in the service configuration file.
Which I think means from within any one of my worker roles, I can find the endpoints of the fellow instances of my worker roles. And likewise, from inside my web role, I can find the endpoints of the fellow instances of this web role.
BUT ... if I want to send messages from my worker role ==> to my web role (TCP, not HTTP), how do you get the endpoint for a role of the other type?
In Visual Studio, I gave both types of roles the same name for the internal endpoint and spec'd both as TCP.- Изменено kurtfr 9 декабря 2011 г. 23:49
-
10 декабря 2011 г. 1:47
Ok, finally figured it out ...
foreach (var roleDefinition in RoleEnvironment.Roles)
{
var myInstance = roleDefinition.Value.Instances;
for (int i = 0; i < myInstance.Count; i++)
{
var myEndpoint = myInstance[i].InstanceEndpoints;
Trace.WriteLine("endpoint>>>>>>>>>>>>>>" + myEndpoint);
}
- Помечено в качестве ответа kurtfr 10 декабря 2011 г. 2:49

