Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6217511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:26:26+00:00 2026-05-24T07:26:26+00:00

I have an application that executes workflows using WorkflowApplication as well as doing other

  • 0

I have an application that executes workflows using WorkflowApplication as well as doing other things. I would like both these other things and the WorkflowApplication interactions with the instance store prior to calling BeginRun() to be scoped by a transaction so I’ve wrapped everything in one. However, introducing this transaction causes calls such as WorkflowApplication.Persist() to hang. The following example is a modified version of the basic instance persistence sample (described here, download from here). I modified the original by scoping everything done in a transaction; this mimics very closely what my application does.

using System;
using System.Activities;
using System.Activities.DurableInstancing;
using System.Activities.Statements;
using System.Runtime.DurableInstancing;
using System.Threading;

namespace Microsoft.Samples.Activities
{
  using System.Transactions;

  class Program
  {
    static AutoResetEvent instanceUnloaded = new AutoResetEvent(false);
    static Activity activity = CreateWorkflow();
    static Guid id;

    const string readLineBookmark = "ReadLine1";

    static void Main()
    {
      StartAndUnloadInstance();
      LoadAndCompleteInstance();

      Console.WriteLine("Press [Enter] to exit.");
      Console.ReadLine();
    }

    static void StartAndUnloadInstance()
    {
      using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))
      {
        WorkflowApplication application = new WorkflowApplication(activity);
        InstanceHandle handle;
        application.InstanceStore = SetupInstanceStore(out handle);

        application.Completed = (e) =>
          { handle.Free(); };

        application.PersistableIdle = (e) =>
            {
              return PersistableIdleAction.Unload;
            };

        application.Unloaded = (e) =>
            {
              instanceUnloaded.Set();
            };

        application.Persist();
        id = application.Id;
        application.Run();
        ts.Complete();
      }

      instanceUnloaded.WaitOne();
    }

    static void LoadAndCompleteInstance()
    {
      using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))
      {
        string input = Console.ReadLine();

        WorkflowApplication application = new WorkflowApplication(activity);
        InstanceHandle handle;
        application.InstanceStore = SetupInstanceStore(out handle);

        application.Completed = (workflowApplicationCompletedEventArgs) =>
          {
            handle.Free();
            Console.WriteLine(
              "\nWorkflowApplication has Completed in the {0} state.",
              workflowApplicationCompletedEventArgs.CompletionState);
          };

        application.Unloaded = (workflowApplicationEventArgs) =>
          {
            Console.WriteLine("WorkflowApplication has Unloaded\n");
            instanceUnloaded.Set();
          };

        application.Load(id);

        //this resumes the bookmark setup by readline
        application.ResumeBookmark(readLineBookmark, input);
        ts.Complete();
      }

      instanceUnloaded.WaitOne();
    }

    static Sequence CreateWorkflow()
    {
      Variable<string> response = new Variable<string>();

      return new Sequence()
      {
        Variables = { response },
        Activities = { 
                        new WriteLine(){
                            Text = new InArgument<string>("What is your name?")},
                        new ReadLine(){ 
                            BookmarkName = readLineBookmark, 
                            Result = new OutArgument<string>(response)},
                        new WriteLine(){
                            Text = new InArgument<string>((context) => "Hello " + response.Get(context))}}
      };
    }

    private static InstanceStore SetupInstanceStore(out InstanceHandle handle)
    {
      SqlWorkflowInstanceStore instanceStore =
          new SqlWorkflowInstanceStore(@"Data Source=.;Initial Catalog=SampleInstanceStore;Integrated Security=True;Asynchronous Processing=True");

      handle = instanceStore.CreateInstanceHandle();
      InstanceView view = instanceStore.Execute(handle, new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(30));

      instanceStore.DefaultInstanceOwner = view.InstanceOwner;

      return instanceStore;
    }
  }
}

using System;
using System.Activities;
using System.Activities.DurableInstancing;
using System.Activities.Statements;
using System.Runtime.DurableInstancing;
using System.Threading;

namespace Microsoft.Samples.Activities
{
  using System.Transactions;

  class Program
  {
    static AutoResetEvent instanceUnloaded = new AutoResetEvent(false);
    static Activity activity = CreateWorkflow();
    static Guid id;

    const string readLineBookmark = "ReadLine1";

    static void Main()
    {
      StartAndUnloadInstance();
      LoadAndCompleteInstance();

      Console.WriteLine("Press [Enter] to exit.");
      Console.ReadLine();
    }

    static void StartAndUnloadInstance()
    {
      using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))
      {
        WorkflowApplication application = new WorkflowApplication(activity);
        InstanceHandle handle;
        application.InstanceStore = SetupInstanceStore(out handle);

        application.Completed = (e) =>
          { handle.Free(); };

        application.PersistableIdle = (e) =>
            {
              return PersistableIdleAction.Unload;
            };

        application.Unloaded = (e) =>
            {
              instanceUnloaded.Set();
            };

        application.Persist();
        id = application.Id;
        application.Run();
        ts.Complete();
      }

      instanceUnloaded.WaitOne();
    }

    static void LoadAndCompleteInstance()
    {
      using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))
      {
        string input = Console.ReadLine();

        WorkflowApplication application = new WorkflowApplication(activity);
        InstanceHandle handle;
        application.InstanceStore = SetupInstanceStore(out handle);

        application.Completed = (workflowApplicationCompletedEventArgs) =>
          {
            handle.Free();
            Console.WriteLine(
              "\nWorkflowApplication has Completed in the {0} state.",
              workflowApplicationCompletedEventArgs.CompletionState);
          };

        application.Unloaded = (workflowApplicationEventArgs) =>
          {
            Console.WriteLine("WorkflowApplication has Unloaded\n");
            instanceUnloaded.Set();
          };

        application.Load(id);

        //this resumes the bookmark setup by readline
        application.ResumeBookmark(readLineBookmark, input);
        ts.Complete();
      }

      instanceUnloaded.WaitOne();
    }

    static Sequence CreateWorkflow()
    {
      Variable<string> response = new Variable<string>();

      return new Sequence()
      {
        Variables = { response },
        Activities = { 
                        new WriteLine(){
                            Text = new InArgument<string>("What is your name?")},
                        new ReadLine(){ 
                            BookmarkName = readLineBookmark, 
                            Result = new OutArgument<string>(response)},
                        new WriteLine(){
                            Text = new InArgument<string>((context) => "Hello " + response.Get(context))}}
      };
    }

    private static InstanceStore SetupInstanceStore(out InstanceHandle handle)
    {
      SqlWorkflowInstanceStore instanceStore =
          new SqlWorkflowInstanceStore(@"Data Source=.;Initial Catalog=SampleInstanceStore;Integrated Security=True;Asynchronous Processing=True");

      handle = instanceStore.CreateInstanceHandle();
      InstanceView view = instanceStore.Execute(handle, new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(30));

      instanceStore.DefaultInstanceOwner = view.InstanceOwner;

      return instanceStore;
    }
  }
}

When run, the application hangs on the Persist() call in StartAndUnloadInstance(). The hang ends after about five minutes (there’s a timeout somewhere). While hung, you can see the reason why by looking at the Activity Monitor in SQL Server. The call to create the workflow owner in SetupInstanceStore() acquires an exclusive lock to a row in the LockOwnersTable. The call to Persist() attempts acquire the same lock. Whoops.

Anyway, the broader question is as stated in the title: can the instance store interactions I do before calling BeginRun() work within the context of the ambient transaction or not? If so, what am I doing wrong? If not, what options do I have? Thus far, I have been able to eliminate the hang/deadlock by surrounding each case where WorkflowApplication accesses the instance store with:

using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Suppress))
{
  // Method call that interacts with the instance store.
  ts.Complete();
}

This moves me forward slightly but reduces me to writing compensation code to handle these calls failing. This should not be necessary. Insights would be greatly appreciated.

EDIT: To elaborate further, I want the workflow to be persisted before execution within the same transaction as other database activity. I want this to:

  • Allow for the workflow to be re-run if the host process dies during the execution of the workflow.
  • Ensure that persistence is rolled back and the workflow is never run if the other database activity fails.

The workflow itself does not need to be run within a transaction, only the instance store interactions that occur prior to the call to BeginRun().

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:26:27+00:00Added an answer on May 24, 2026 at 7:26 am

    Alright, so the answer to my question appears to be no. I can’t find a way to eliminate the hang while the ambient transaction is present. What I’ve done is surround each instance store interaction (create workflow owner, persist) with

    using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Suppress))
    {
      // Method call that interacts with the instance store.
      ts.Complete();
    }
    

    This allows this structure for kicking off workflows:

    • Start top-level unit of work (NHibernate session).
      • Do stuff.
      • Get the instance store handle, execute a CreateWorkflowOwnerCommand to acquire the lock.
      • Create the workflow application.
      • Persist the workflow.
      • Call BeginRun.
    • Commit.

    The downside? If an exception is thrown after the Persist, there’s no rollback; the workflow will run at some point. I have some infrastructure in place to minimize the problem but the long-term answer is to find a way to use AppFabric (not possible at this point due to scale of change required but will be looked at for the next version).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ruby application that executes ant as a subprocess using backtick. This
I have a command line application that executes other programs according to a user
I have an application that executes the following MySQL query: SELECT 402 AS user_id,
I have a C++ application that executes test cases. It is possible that some
I've been reading about Skip Lists lately. I have a web application that executes
Well, the problem is as follows: I have a WPF Application built using the
I have an application that executes this piece of code (JQuery 1.4.2) in an
I have a Java application hosted on a remote tomcat instance that executes a
I have an application that executes an upgrader application automatically whien it starts. However,
I have a web application that executes on tomcat 6. I have a MysqlDb

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.