Archive for the ‘WF4.0’ Category

Past two days I was so upset with my computer that whenever I tried running my workflow xamlx,it started throwing exception “A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections” .It was then I realized that my recent effort to install SQL Express 2012 has messed up my SQL Express 2008 with VS 2010.I tried to repair,remove,add etc options inorder to get my SQLExpress back & get my workflow back to normal.No luck & I was almost at the verge of crying thinking about what the admin guys are going to tell me if I take my computer to them “Just Re-image the machine,that’s the only thing we can do” .Today evening inspite being a holiday and a having bad cold,i decided to give a try once again if I can do something.I knew that if I delete the entries of SQLServer from registry it may help.My hubby darling was advicing me saying you are gonna mess up your windows as well if you play with registry.I wanted to listen to him,but I thought if I have to reimage the system next morning,there is nothing wrong in playing with registry.Here is what I did & that infact successfully helped me install SQLExpress 🙂
1) Open Run Menu and type regedit to open Registry Editor
2)Go to following location.
HKEY_LOCAL_MACHINE > Software > Microsoft > Microsoft SQL Server > 90
3)Remove Registry Key 90.

I manually removed the SQLServer instances from Control Panel\All Control Panel Items\Programs and Features and after that I ran visual studio setup and installed Microsft SQL Express.It got installed successfully & I could run my workflow after that :)It was worth giving a try.Eureka!!

Today I was having a look at one of our WCF Restful projects.To start with the code understanding,I decided to host that in my local host and then take a look.To my surprise,I was unable get a svc url as we usually get, for example: http://wordpress.com/Service1.svc .Digging further I realized, being a wcf restful service it’s not ideal having an extension this way and it has been achieved through System.Web.Routing integration .Routing allows to host a WCF restful service that responds to URIs without an extension .Cool right?It’s a new feature with .Net 4.0.I am avoiding a rewriting of an example code here as msdn has got a beautiful simple explanation over here.Take a look!

There is no context attached to the incoming message for the service and the current operation is not marked with “CanCreateInstance = true”.In order to communicate with this service check whether the incoming binding supports the context protocol and has a valid context initialized

If you run into exception,it clearly says current operation is not marked with CanCreateInstance = true,it means the first receive activity in the workflow is supposed to have CanCreateInstance = true property set,otherwise the workflow cannot activate.Hope this tip helps 🙂

If your Activities OutArgument is something complex,say List<string> for eg,then you need to initialize it first & If you don’t do so,you will get an exception The property ‘Collection’ of ‘AddToCollection<String>’ is not initialized.

For assigning default value to the variable,you can use the assign activity & initialize it to New List(Of String).Though your language is C#,if you look at,you will understand that the initialization to variable is a VB expression ! It’s not a big deal to write an expression in VB ,still I wonder why is it so when the project is C# :)If you know the answer,please share  that in the comments section below 🙂

WorkflowInvoker class provides a means for invoking a workflow as if it were a method call.

Namespace:System.Activities
Assembly:System.Activities (in System.Activities.dll)

A custom activity example which we will call using WorkFlowInvoker :

public sealed class SendEmailActivity : CodeActivity
{

[RequiredArgument]
public InArgument Subject { get; set; }

[RequiredArgument]
public InArgument Body { get; set; }

[RequiredArgument]
public InArgument To { get; set; }

[RequiredArgument]
public InArgument From { get; set; }

protected override void Execute(CodeActivityContext context)
{
// Obtain the runtime value of the Subject input argument
string text = context.GetValue(this.Subject);
}
}

Invokes SendEmailActivity :
public void TestSendEmailActivity()
{

WorkflowInvoker invoker = new WorkflowInvoker(
new SendEmailActivity
{
From = "Anupama_roy@testmail.com",
To = "Anupama_roy@testmail.com",
Subject = "Hello",
Body="How are you?"

});

invoker.Invoke();

}

This article gives a basic idea of how to create a custom activity in WF 4 deriving from CodeActivity.Steps below demonstrates how to do this.

• Open a New Project in vs2010,Choose WCF WorkFlow Service Application

• You can see Service1.xamlx added.Rename Service1.xamlx to Operation.xamlx

• Now Add a class to this project & name it as OperationActivity.cs

• Add namespace using System.Activities; to class

• Derive Class from CodeActivity

public class OperationActivity:CodeActivity
• Add In & Out arguments

[RequiredArgument]
public InArgument From { get; set; }
public OutArgument CallCount { get; set; }

Write the below code & compile. What this activity does is, takes an InArgument of type string. If it is not empty, increase the counter set and assign it to OutArgument CallCount.

namespace Anupama_s_WF_Sample
{
public class OperationActivity:CodeActivity
{

[RequiredArgument]
public InArgument From { get; set; }
public OutArgument CallCount { get; set; }
protected override void Execute(CodeActivityContext context)
{

try
{

string text = context.GetValue(this.From);
int count=0;
if (!string.IsNullOrEmpty(text))
{
count++;
context.SetValue(this.CallCount,count);

}

}
catch (Exception ex)
{
throw;
}
}

}
}

Now Open xamlx designer, you see two Activities added by default

1.ReceiveRequest

Receives the request from user

2.SendResponse

Sends result back to user

Open Tool Box as you see in the snapshot, you can see the compiled custom activity OperationActivity which you created now. Drag & drop as shown in snapshot.

It shows a validation symbol highlighted in red .It means value for the arguments are not supplied

• For this you have to add a variable

• Name variable as “From” .Set the variable type as string

• Click on receive request activity

Against Content,You can see View parameter.Click on it.It will open a dialog box.Click on Parameters radio button.Add new parameter FromInput & assign it to the previously created variable “From”.Click ok

• Select Operation Activity,Right Click to see properties.


• Against From in properties window,select the From variable declared.

• What we have done is,ReceiveRequest activity takes the input from user,assigns it to From variable & Operation activity utilizes the value from common variable set.

• We need one more variable to pass the output from custom activity to SendResponse activity

• Create variable Result of type int

• Assign Operation activity CallCount to Result,Add this Result as the input parameter to SendResponse activity.

Look at the designer now, you can see that all validation errors are disappeared as we satisfied the input & output requirements by assigning to appropriate variables.

Hope you got the basic idea of how to create a custom activity deriving from CodeActivity.