2

Not sure if this is possible in Azure services and if it is whats the best approach.

I have a class which is extracting data from my database and building an xml output. Under normal circumstances since this is running in a worker role, it will run when necessary and push the XML to an external web service. However I also need the ability to expose that functionality as a service so that a different external web service can make a web service call to my system and get that very same XML response. I can setup a RESTful webservice in a worker role and build the same functionality as exists in my worker role - the problem is that I now have duplicate code.

How can I set this up so that my code to extra data from the DB, perform some calculations, generate the xml all sits in one location that is accessible from both a webrole (triggered by external web service call) and worker role (on regular running schedule).

I have looked through all the questions regarding TCP internal endpoints and worker webrole communication but none of them seem to cover this case of allowing a webrole to call a method on the workerrole and pass that data on to the caller.

If someone can help me with this would be very much appreciated. Thanks

Gotts
  • 2,274
  • 3
  • 23
  • 32
  • On Second thought - I guess the answer here is to create a new generic project which contains any shared code and reference this from my web and worker role. This probably makes the most sense. Any issues doing this in Azure? – Gotts Feb 19 '13 at 18:54
  • That's basically what @sharptooth was suggesting, just with the presumption that the project was going to be compiled separately. – knightpfhor Feb 20 '13 at 19:31

2 Answers2

5

Well, technically the solution is that you create a class library (.NET assembly) that contains all the shared code and use it from both roles projects. You'll have to set "copy local" to "true" on the reference to that assembly in each role project so that the assembly gets packed into role packages.

However in your case there might be a better solution. You might just use the web role for both purposes. I mean a web role is like a worker role, just has a bonus IIS installed. Unless you really (really-really) need to scale and update them separately you can save a lot of management and deployment pain and also some money just using the web role for everything.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • Thank you. Very helpful. I will need to look into the copy local and consider your suggestion to just use a web role - but i believe we will need to keep a separate web and worker (for now anyway). This is my first deployment to Azure - if the class library I create with the shared code is referenced in both projects, will that not automatically make it included in the deployment? ie why is it such a hassle? – Gotts Feb 20 '13 at 18:38
  • @Gotts: Not much hassle, you would add a reference anyway, the only extra step is setting "copy local". I'd say the reason this is needed is because the Azure SDK and "tools" are outrageously buggy. – sharptooth Feb 21 '13 at 06:12
1

If I were you I would make it as the Web Role then would send a message to Worker Role and based on this message, would trigger the event on the worker role to start the required process.

You can do that using the queue storage or a WCF service.

Community
  • 1
  • 1
hhaggan
  • 619
  • 4
  • 11
  • Yes but that doesnt allow me to pass the worker role response synchronously back to the web role. I understand there are issues with synchronoous communication so i guess my question is really around shared code between worker and web. I guess its just not possible? – Gotts Feb 19 '13 at 17:51
  • did you check the connection between them using service bus, like creating the worker role with service bus queue http://middlewareinthecloud.com/2012/06/23/azureservice-bus-queues-in-worker-roles/ or the intercommunication between the roles . I found a similar question on MSDN I hope this helps you http://social.msdn.microsoft.com/Forums/en-US/windowsazureconnectivity/thread/5193bfa8-3d1a-44d8-8eb6-1c87250c3b30 – hhaggan Apr 10 '13 at 22:16
  • I think that this url can also help you http://windowsazurecat.com/2011/08/how-to-simplify-scale-inter-role-communication-using-windows-azure-service-bus/ – hhaggan Apr 10 '13 at 22:20