WF4.0 Beta1之旅(7):WF调用PowerShell
在WF4 beta1中与PowerShell交互提供了两个活动InvokePowerShell和InvokePowerShell<T>活动
InvokePowerShell:调用一个不包含返回值的PowerShell cmdlet。InvokePowerShell可以用来调用简单的cmdlets和脚本。我们还可以向cmdlet传递参数和输入对象。执行之后,此活动会提供一组错误信息(如果发生错误的话)。
InvokePowerShell<T>:此活动调用PowerShell cmdlet,并接收返回的结果。这种类型的活动都要比非泛型版本多一个InitializationAction。InitializationAction用来将cmdlet的执行结果隐射到工作流的变量。
下面就举例说明这个两个活动如何使用,下面的例子中我们完成如下功能,输入一个进程名,然后调用powershell停止该进程,在获取最新的所有进程并显示出来,我们首先来完成几个自定义活动:
1.用于接收输入的活动ReadLine,如下:
namespace CaryPowerShell
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
public class ReadLine : NativeActivity
{
public OutArgument<string> Result { get; set; }
public InArgument<string> BookmarkName { get; set; }
protected override void Execute(ActivityExecutionContext context)
{
string name = this.BookmarkName.Get(context);
if (name == null)
{
throw new Exception(string.Format("ReadLine {0}: 书签不能为空",this.DisplayName));
}
context.CreateNamedBookmark(name, new BookmarkCallback(OnReadComplete));
}
void OnReadComplete(ActivityExecutionContext context, Bookmark bookmark, object state)
{
string input = state as string;
context.SetValue(this.Result, input);
}
}
}
2.用于输出进程信息的活动PrintResult,如下:
namespace CaryPowerShell
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
public class PrintResult<T> : CodeActivity<T>
{
public InArgument<ICollection<T>> Collection { get; set; }
protected override void Execute(CodeActivityContext context)
{
ICollection<T> underlyingCollection = this.Collection.Get<ICollection<T>>(context);
if (underlyingCollection.Count == 0)
{
Console.WriteLine("没有进程");
}
else
{
foreach (T obj in underlyingCollection)
{
Console.WriteLine(obj.ToString());
}
}
}
}
}
3.获取进程的活动GetProcesses,使用的是泛型版本的InvokePowerShell<T>,如下图:
4.停止进程的活动StopProcess,使用的是InvokePowerShell活动,如下图:
5.我们设计的工作流如下图:
6.宿主程序如下:
namespace CaryPowerShell
{
using System;
using System.Linq;
using System.Threading;
using System.Activities;
using System.Activities.Statements;
class Program
{
static void Main(string[] args)
{
bool running = true;
AutoResetEvent syncEvent = new AutoResetEvent(false);
WorkflowInstance myInstance = new WorkflowInstance(new Sequence1());
myInstance.OnCompleted = delegate(WorkflowCompletedEventArgs e)
{
running = false;
syncEvent.Set();
};
myInstance.OnIdle = delegate()
{
syncEvent.Set();
return IdleAction.Nothing;
};
myInstance.OnUnhandledException = delegate(WorkflowUnhandledExceptionEventArgs e)
{
Console.WriteLine(e.UnhandledException.ToString());
return UnhandledExceptionAction.Terminate;
};
myInstance.OnAborted = delegate(WorkflowAbortedEventArgs e)
{
Console.WriteLine(e.Reason);
syncEvent.Set();
};
myInstance.Run();
// main loop (manages bookmarks)
while (running)
{
if (!syncEvent.WaitOne(10, false))
{
if (running)
{
if (myInstance.GetAllBookmarks().Count > 0)
{
string bookmarkName = myInstance.GetAllBookmarks()[0].BookmarkName;
myInstance.ResumeBookmark(bookmarkName, Console.ReadLine());
syncEvent.WaitOne();
}
}
}
}
System.Console.WriteLine("工作流执行结束");
System.Console.ReadKey();
}
}
}
8.相关文章:
WF4.0 Beta1之旅(1):基本介绍
WF4.0 Beta1之旅(2):异常处理
WF4.0 Beta1之旅(3):全新的FlowChart
WF4.0 Beta1之旅(4):Bookmark的使用
WF4.0 Beta1之旅(5):规则引擎的变化
WF4.0 Beta1之旅(6):自定义活动