Why is Azure Add > Worker role project in solution … greyed out in VS2010?
Asked Answered
D

1

17

I have a C# .Net 4 class library that I think I have set up to be an Azure Worker Role, i.e. the class implements Microsoft.WindowsAzure.ServiceRuntime.RoleEntryPoint and has a Run() and an OnStart() method. However when I right-click on the Roles folder in my empty Windows Azure project and click Add the option to add a "Worker role project in solution …" is greyed out. What do I need to do for VS2010 to realise there's a potential worker role project in the solution?

enter image description here

Deutero answered 31/5, 2012 at 8:26 Comment(0)
S
31

The "Add > Worker Role Project in solution..." only displays worker role projects not class libraries. Even though they look the same, they aren't.

You can create a worker role project by using the following:

  • When creating a new cloud project

enter image description here

  • By right clicking a cloud project

enter image description here

Then you'll be able to add that worker role project in an other cloud project if you like. In that case the "Add > Worker Role Project in solution..." menu option will be enabled.

Now, you can also 'convert' a class library to a worker role project. Right click the class library and choose Unload Project .. then (once that's unloaded), Edit Project File and add the RoleType element in the first property group:

<PropertyGroup>
   <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
   <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
   <ProductVersion>8.0.30703</ProductVersion>
   <SchemaVersion>2.0</SchemaVersion>
   <ProjectGuid>{7E9F972F-BE92-4CF7-998D-E76B61B21C37}</ProjectGuid>
   <OutputType>Library</OutputType>
   <AppDesignerFolder>Properties</AppDesignerFolder>
   <RootNamespace>ClassLibrary1</RootNamespace>
   <AssemblyName>ClassLibrary1</AssemblyName>
   <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
   <FileAlignment>512</FileAlignment>
   <RoleType>Worker</RoleType>  <-------------------- HERE
 </PropertyGroup>

Finally - reload the project back into the solution -> Right-Click Reload Project. (NOTE: if the Xml file is still open, you'll get asked to close it.. which is perfect. Say yes and close it). .. Now your class library has been re-added back but it's a Worker Role Class Library.

If you do this, Visual Studio will recognize the project as being a worker role and you'll be able to add it to the cloud project:

enter image description here

Note: Your class library will need a class deriving from RoleEntryPoint. More information: http://blogs.msdn.com/b/jnak/archive/2010/02/11/windows-azure-roleentrypoint-method-call-order.aspx

Scute answered 31/5, 2012 at 9:44 Comment(5)
Interestingly there was no "Edit Project File" when I right clicked the class library but it was straightforward to edit it in Notepad.Deutero
Ah sorry, it could be that this button is only available through a VS extension (VSCommands, PowerCommands or Productivity Power Tools). In that case, editing in notepad is also a valid solution.Scute
You can "Unload project" and then right click the Unloaded project to edit it.Charlsiecharlton
Solid Sandrino, many thanks, added the RoleType tag in the proj file and it worked like a champ.Corroborate
Perfect @SandrinoDiMattia :) Just added the Unload project stuff to the answer.Heintz

© 2022 - 2024 — McMap. All rights reserved.