Miha Markic

Let's say I have an activity. Within that activity I would want to set its Roles property in code. I can, no problems there. However, the security check does nothing unless Roles property is bound to an external property. Is that so

Re: Windows Workflow Foundation Can I set Roles dynamically, without binding?


Hi Miha

It sounds like you're running into activity execution context cloning.

If your code looks like this (say you have a HandleExternalEvent activity called handleExternalEvent1 in an EventDriven in a State in a state machine workflow):

this.handleExternalEvent1.Roles = new WorkflowRoleCollection[] { new WebWorkflowRole("Admin") }; // code just from my head...

The Roles property will be null at runtime - the code applies to the workflow template rather than the executing clone. And if the Roles property is null (which it will be in this case), then role-based security is not applied.

So binding the Roles property is the way to go. You can write the property that you are going to bind the Roles property to on the workflow and set it either from the host application (for example if you need to use the same workflow both with web workflow roles and Active Directory roles) or within the workflow itself.

I think if there was a MethodInvoking event that you could handle, then you could cast the sender argument and set the Roles property that way, but there isn't...!

Hope that helps...


Re: Windows Workflow Foundation Can I set Roles dynamically, without binding?

Miha Markic

Hi Nigel,

I have something like this, for testing purposes (PilotRole is WorkflowRole derived class, straightforward):

Code Snippet

public partial class SignatureActivity: HandleExternalEventActivity


public SignatureActivity()



Roles = new WorkflowRoleCollection();

PilotRole role = new PilotRole("Administrator", new List<string>(new string[] { "Miha" }));



protected override void OnActivityExecutionContextLoad(IServiceProvider provider)


Console.WriteLine("Roles when OnActivityExecutionContextLoad: " + Roles.Count);




protected override void OnInvoked(EventArgs e)


Console.WriteLine("Roles when invoked: " + Roles.Count);




Security is ignored if I do so (also PilotRole.IncludesIdentity method is never called).

However, both console outputs show 1 and Roles property isn't null.

I'll probably end up with binding anyway.