Jaa


Microsoft.Activities and Microsoft.Activities.UnitTesting now available via NuGet

When I first saw NuGet I knew I had to find a way to take advantage of this great new way to deliver code to you.  It took a few months but finally the other day I decided to spend an afternoon working on a NuGet package for Microsoft.Activities.  Two and a half days later (isn’t that how it always goes) I produced packages for Microsoft.Activities and Microsoft.Activities.UnitTesting

How To Get Microsoft.Activities or Microsoft.Activities.UnitTesting with NuGet

Step 0: Install NuGet

If you don’t already have NuGet, install it now (you will have to restart Visual Studio if it is already running)

Step 1: Create a project

Your project can be any kind of project.  NuGet will automatically add references that are required.  For this example, I’m going to create a Class Library project.

Step 2a: Install the Microsoft.Activities Package (Using the Package Manager Console)

  1. Select View / Other Windows / Package Manager Console
  2. Type Install-Package Microsoft.Activities

image

Step 2b: Install the Microsoft.Activities Package (Using Library Package Reference)

If you would rather have a UI to browse packages,

  1. Right click on the references folder
  2. Select Add Library Package Reference…
  3. In the search box type Microsoft.Activities
  4. Click the Install button to add a library package reference

SNAGHTML4b4210

What Happens to my Project / Visual Studio when I install this?

Your Project

  • Add reference to System.Activities
  • Add reference to ($SolutionDir)packages\Microsoft.Activities.1.8.1.506\lib\Net40\Microsoft.Activities.dll) with copy local = true so the assembly will be in your bin directory when you build.

Visual Studio

  • Added toolbox icons for the activities in Microsoft.Activities to your toolbox pointing to the last installation location for Microsoft.Activities.dll
  • Add a new Workflow to your project to see the two groups (Dictionary and Microsoft.Activities)

image

What if I want to use .NET 4 Platform Update 1 so I can create a State Machine workflow?

The Microsoft.Activities package includes a version built for .NET 4 Platform Update 1.  As you see in the folder structure there are Net40 and Net401 folders.  NuGet automatically detects which version of the .NET Framework your project is targeting and will add a reference to the appropriate version

image

What if I change my target framework to .NET 4 Platform Update 1 after installing the Microsoft.Activities Package?

If you change your target framework after installing your project will continue to reference the .NET 4 version of Microsoft.Activities.  You can update the reference manually by browsing the packages folder and adding a reference or you can uninstall / reinstall the package.

What if I create a new project and drag an activity from Microsoft.Activities onto a workflow without first installing Microsoft.Activities?

Toolbox activities remain in the toolbox across projects.  When you create a new project and open the workflow designer you will see the Microsoft.Activities and Dictionary tabs.  When you drag one of these activities onto the workflow designer, Visual Studio will add a reference to the assembly that the toolbox icon is associated with.

If you installed the Microsoft.Activities package to a project named ClassLibary1 and then created another project named WorkflowConsoleApplication1 as soon as you start dragging an activity onto a workflow from WorkflowConsoleApplication1 you will see an assembly reference added to the Microsoft.Activities package installed under the ClassLibrary1 solution.

What if I delete the solution where Microsoft.Activities was installed?

If you try to delete it while Visual Studio is open you will get an error because Visual Studio has the assembly loaded.  If you close Visual Studio and delete the solution, the next time you run Visual Studio and open the Workflow Designer you will see the icons are still in the toolbox.  However when you try to drop an activity from the missing assembly, Visual Studio won’t allow it because it is unable to locate the assembly to add a reference.

image

How do I fix the toolbox icons if they refer to an assembly that no longer exists?

Each time install the Microsoft.Activities package, if the toolbox icons exist, they are removed and replaced with a toolbox icon that refers to the Microsoft.Activities assembly that is installed with the package in current project.

What if I want to store the Microsoft.Activities assembly in some other location?  How do I keep the NuGet package from updating my toolbox icons?

If you change the name of the Tab group from Microsoft.Activities to something else, the package installation script won’t find the activities and won’t update them.  However it will add a new activity group.

How do I uninstall the Microsoft.Activities package?

  1. Open the Package Manager console
  2. Type Uninstall-Package Microsoft.Activities
  3. In the toolbox, right click on the Microsoft.Activities tab group and select Delete Tab
  4. Repeat for the Dictionary Group

Sometimes you will get an error when trying to uninstall because Visual Studio has one of the assemblies open.  In this case you will have to manually delete the package. 

 PM> Uninstall-Package Microsoft.Activities
Successfully removed 'Microsoft.Activities 1.8.1.506' from WorkflowConsoleApplication10. 
Access to the path 'd:\ngt\WorkflowConsoleApplication10\packages\Microsoft.Activities.1.8.1.506\lib\Net40\Microsoft.Activities.dll' is denied. 
Access to the path 'd:\ngt\WorkflowConsoleApplication10\packages\Microsoft.Activities.1.8.1.506\tools\Microsoft.Activities.NuGet.dll' is denied. 
The directory is not empty.

What if I install Microsoft.Activities.UnitTesting and I don’t have Microsoft.Activities?

Not to worry.  Microsoft.Activities.UnitTesting has a dependency on the Microsoft.Activities package so NuGet will automatically deploy it as well.

 PM> Install-Package Microsoft.Activities.UnitTesting 
'Microsoft.Activities (≥ 1.8.1)' not installed. Attempting to retrieve dependency from source... 
Done. 
Successfully installed 'Microsoft.Activities 1.8.1.509'. 
Successfully installed 'Microsoft.Activities.UnitTesting 1.8.1.509'. 
Successfully added 'Microsoft.Activities 1.8.1.509' to WorkflowConsoleApplication14. 
Successfully added 'Microsoft.Activities.UnitTesting 1.8.1.509' to WorkflowConsoleApplication14.

What if I install Microsoft.Activities.UnitTesting and then try to Uninstall Microsoft.Activities

 PM> Uninstall-Package Microsoft.Activities
Uninstall-Package : Unable to uninstall 'Microsoft.Activities 1.8.1.509' because 'Microsoft.Activities.UnitTesting 1.8.1.509' depends on it.
At line:1 char:18
+ Uninstall-Package <<<<  Microsoft.Activities
    + CategoryInfo          : NotSpecified: (:) [Uninstall-Package], InvalidOperationException
    + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.UninstallPackageCommand

What do you think?

I’m really happy with NuGet so far.  I just updated the uninstall scripts so that they will remove the toolbox tabs (Microsoft.Activities, Dictionary and Unit Testing) created by the install scripts.  Of all the things these packages do the interaction with the toolbox is the one area that I’m not sure about.  Hopefully what I’ve done here will work for you.  If not, tell me how we can do it better.

Ron Jacobs

https://blogs.msdn.com/rjacobs

Twitter: @ronljacobs https://twitter.com/ronljacobs