我也面临着类似的问题,每当工作流设计发生变化时,数据库中的所有旧工作流实例都会使用抛出错误。
为了克服这个问题,我为每种类型的工作流创建了跟踪配置文件。如果我更改了任何工作流,我将更新配置文件的版本。这对我很有用……这是示例代码-
为此,您需要将跟踪服务添加到工作流运行时。
trackingprofile profile=createprofile();
storeprofiletodb(profile,connstring,typeof(exceptionwf.parametereexceptionwf),“2.0.0.0”);
专用静态跟踪配置文件createprofile()
{
trackingprofile myprofile=new trackingprofile();
ActivityTrackingLocation stateActivityLocation = CreateActivityLocation(typeof(StateActivity));
AddActivityExecutionStatus(stateActivityLocation);
ActivityTrackingLocation eventDrivenActLoc = CreateActivityLocation(typeof(EventDrivenActivity));
AddActivityExecutionStatus(eventDrivenActLoc);
ActivityTrackPoint actPt = new ActivityTrackPoint();
actPt.MatchingLocations.Add(stateActivityLocation);
actPt.MatchingLocations.Add(eventDrivenActLoc);
myProfile.ActivityTrackPoints.Add(actPt);
WorkflowTrackPoint workflowTrack = CreateWorkflowTrackPoint();
myProfile.WorkflowTrackPoints.Add(workflowTrack);
UserTrackPoint utp = new UserTrackPoint();
UserTrackingLocation ul = new UserTrackingLocation();
ul.ActivityType = typeof(HandleExternalEventActivity);
ul.ArgumentType = typeof(object);
ul.MatchDerivedArgumentTypes = true;
ul.MatchDerivedActivityTypes = true;
utp.MatchingLocations.Add(ul);
myProfile.UserTrackPoints.Add(utp);
myProfile.Version = new Version("1.0.0.0");
return myProfile;
}
private static void StoreProfileToDB(TrackingProfile profile, string connString, Type wfType,string version)
{
TrackingProfileSerializer serializer = new TrackingProfileSerializer();
System.IO.StringWriter writer = new System.IO.StringWriter(new StringBuilder());
serializer.Serialize(writer, profile);
SqlConnection conn = null;
try
{
if (!String.IsNullOrEmpty(connString))
{
conn = new SqlConnection(connString);
string storedProc = "dbo.UpdateTrackingProfile";
SqlCommand cmd = new SqlCommand(storedProc, conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
SqlParameter param = new SqlParameter("@TypeFullName", SqlDbType.NVarChar, 128);
param.Direction = ParameterDirection.Input;
param.Value = wfType.FullName;
cmd.Parameters.Add(param);
param = new SqlParameter("@AssemblyFullName", SqlDbType.NVarChar, 256);
param.Direction = ParameterDirection.Input;
param.Value = wfType.Assembly.FullName;
cmd.Parameters.Add(param);
param = new SqlParameter("@Version", SqlDbType.VarChar, 32);
param.Direction = ParameterDirection.Input;
//Note that you should increment version number for your
//TrackingProfile to be able to use new TrackingProfile.
//Default version is "1.0.0.0, It uses the default profile if not increamented.
param.Value = version;
cmd.Parameters.Add(param);
param = new SqlParameter("@TrackingProfileXml", SqlDbType.NText);
param.Direction = ParameterDirection.Input;
param.Value = writer.ToString();
cmd.Parameters.Add(param);
conn.Open();
cmd.ExecuteNonQuery();
}//if
}// try
catch (Exception ex)
{
if (ex is SqlException)
{
//Check to see if it's a version error
if (ex.Message.Substring(0, 24) == "A version already exists")
{
EventLogger.Log("A profile with the same version already exists in database");
}//if
else
{
EventLogger.Log("Error writing profile to database : " + ex.ToString());
}
}
else
{
EventLogger.Log("Error writing profile to database : " + ex.ToString());
}
}
finally
{
if (conn != null) { conn.Close(); }
}
}