c# - Calling Runspace.Open() inside a TransactionScope changes Transaction.Current and throws exception -


if system.management.automation.runspaces.runspace.open() happens inside system.transactions.transactionscope apparently changes transaction.current in turns causes 'system.invalidoperationexception' @ transaction's dispose time.

so this:

using (var scope = new transactionscope()) {     using (var runspace = runspacefactory.createrunspace())     {         runspace.open();     }      scope.complete(); } 

throws: "transaction.current has changed inside of transactionscope."

i wondering if missing crucial parameter in transaction or runspace or has power-shell/msdtc/etc configurations?

had same issue myself. runspace buried within several nested transactionscope layers. upon executing runspace.open(), transaction.current changed , exception throw per op's question.

the solution me wrap code inside transactionscope, time transactionscopeoption.suppress parameter.

    using (var transactionscope = new transactionscope(transactionscopeoption.suppress))         {             ienumerable<object> results = null;              using (runspace runspace = runspacefactory.createrunspace())             {                 runspace.open();                  using (pipeline pipeline = runspace.createpipeline())                 {                     command command = new command(script, true, true);                      if (parameters != null && parameters.any())                         foreach (var param in parameters)                             command.parameters.add(param.key, param.value);                      pipeline.commands.add(command);                      results = pipeline.invoke();                 }                  runspace.close();                  transactionscope.complete();                  return results;             }         } 

this works me. no more exception.


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -