这样就完成了在client端的config配置,下面就是客户端访问的工作流的代码了。
internal class Program
{
private const string workflowTypeName = "CloudOrderWorkflow2";
private const string activityName = "ReceiveOrder";
internal static void Main()
{
Console.Title = "Client";
var userName = ConfigurationManager.AppSettings["UserName"].ToString();
var password = ConfigurationManager.AppSettings["Password"].ToString();
var instanceId = ConfigurationManager.AppSettings["InstanceId"].ToString();
Console.WriteLine("Press [enter] to send the order to the cloud workflow");
Console.ReadLine();
var request = CreateMessage();
try
{
CallReceiveActivity(userName, password, workflowTypeName, instanceId, activityName, request);
}
catch (WebException ex)
{
Console.WriteLine("Error handled:");
using (var sr = new StreamReader(ex.Response.GetResponseStream()))
{
Console.WriteLine(sr.ReadToEnd());
}
}
Console.WriteLine("Press [enter] to exit");
Console.ReadLine();
}
private static string CreateMessage()
{ //注:为了测试工作流中的IFELSE流程,可在800运行之后修改为1400,这样会运行cloudDelay流程
return "<root><order><total>800</total></order></root>";
}
internal static void CallReceiveActivity(string userName, string password, string workflowName, string instanceId, string activityName, string message)
{
var url = String.Format(CultureInfo.InvariantCulture,
"http://{0}/workflowsHttp/{1}/workflows/{2}/instances/{3}/{4}",
WorkflowClientConfig.WorkflowHostName, userName, workflowName, instanceId, activityName);
var token = GetWorkflowGrantingToken(userName, password);
using (var httpRequest = new WebClient())
{
httpRequest.Headers.Add(HttpRequestHeader.ContentType, "text/xml");
httpRequest.Headers.Add("X-MS-Identity-Token", token);
var response = httpRequest.UploadString(url, message);
Console.WriteLine("Response by the server: {0}", response);
}
}
internal static string GetWorkflowGrantingToken(string userName, string password)
{
string tokenUrl = string.Format(CultureInfo.InvariantCulture,
"https://{0}/issuetoken.aspx?u={1}&p={2}",
WorkflowClientConfig.StsHostName,
userName,
password);
Console.WriteLine("Requesting authentication token");
HttpWebRequest tokenRequest = (HttpWebRequest)WebRequest.Create(tokenUrl);
HttpWebResponse tokenResponse = (HttpWebResponse)tokenRequest.GetResponse();
byte[] tokenBody = new byte[500];
int tokenBodyLength = tokenResponse.GetResponseStream().Read(tokenBody, 0, 500);
var authenticationToken = Encoding.UTF8.GetString(tokenBody, 0, tokenBodyLength);
Console.WriteLine("Authentication Token: {0}", authenticationToken);
return authenticationToken;
}
}
{
private const string workflowTypeName = "CloudOrderWorkflow2";
private const string activityName = "ReceiveOrder";
internal static void Main()
{
Console.Title = "Client";
var userName = ConfigurationManager.AppSettings["UserName"].ToString();
var password = ConfigurationManager.AppSettings["Password"].ToString();
var instanceId = ConfigurationManager.AppSettings["InstanceId"].ToString();
Console.WriteLine("Press [enter] to send the order to the cloud workflow");
Console.ReadLine();
var request = CreateMessage();
try
{
CallReceiveActivity(userName, password, workflowTypeName, instanceId, activityName, request);
}
catch (WebException ex)
{
Console.WriteLine("Error handled:");
using (var sr = new StreamReader(ex.Response.GetResponseStream()))
{
Console.WriteLine(sr.ReadToEnd());
}
}
Console.WriteLine("Press [enter] to exit");
Console.ReadLine();
}
private static string CreateMessage()
{ //注:为了测试工作流中的IFELSE流程,可在800运行之后修改为1400,这样会运行cloudDelay流程
return "<root><order><total>800</total></order></root>";
}
internal static void CallReceiveActivity(string userName, string password, string workflowName, string instanceId, string activityName, string message)
{
var url = String.Format(CultureInfo.InvariantCulture,
"http://{0}/workflowsHttp/{1}/workflows/{2}/instances/{3}/{4}",
WorkflowClientConfig.WorkflowHostName, userName, workflowName, instanceId, activityName);
var token = GetWorkflowGrantingToken(userName, password);
using (var httpRequest = new WebClient())
{
httpRequest.Headers.Add(HttpRequestHeader.ContentType, "text/xml");
httpRequest.Headers.Add("X-MS-Identity-Token", token);
var response = httpRequest.UploadString(url, message);
Console.WriteLine("Response by the server: {0}", response);
}
}
internal static string GetWorkflowGrantingToken(string userName, string password)
{
string tokenUrl = string.Format(CultureInfo.InvariantCulture,
"https://{0}/issuetoken.aspx?u={1}&p={2}",
WorkflowClientConfig.StsHostName,
userName,
password);
Console.WriteLine("Requesting authentication token");
HttpWebRequest tokenRequest = (HttpWebRequest)WebRequest.Create(tokenUrl);
HttpWebResponse tokenResponse = (HttpWebResponse)tokenRequest.GetResponse();
byte[] tokenBody = new byte[500];
int tokenBodyLength = tokenResponse.GetResponseStream().Read(tokenBody, 0, 500);
var authenticationToken = Encoding.UTF8.GetString(tokenBody, 0, tokenBodyLength);
Console.WriteLine("Authentication Token: {0}", authenticationToken);
return authenticationToken;
}
}
现在我们在本地分别打开两个VS2008,一个将service项目做为启动项目中,一个以Client作为启动项目,分别在相应的项目上击鼠标右键,在弹出菜单上选“调试”--》“启动新实例”。先运行service端如下:
然后是按上面所说方式启动Client端:
这里,我们在切换回服务端运行界面,如下:
到这里,我们只是测试了工作流中的ELSE分支(代码中Total为800),还未测试当Total>1000的分支。