This project is read-only.

Creating User with code gives error:

Topics: Troubleshooting, Writing modules
Feb 7, 2011 at 3:04 PM

I am trying to create a user in code as follows:

var orchardUser = _membershipService.GetUser("username");
if (orchardUser == null)
	orchardUser = _membershipService.CreateUser(new Security.CreateUserParams("username", "password", "", null, null, true));
	_membershipService.SetPassword(orchardUser, "password");

I do get the following error however when trying to create the user:

The operation is not valid for the current state of the enlistment.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The operation is not valid for the current state of the enlistment.

Source Error: 

Source File: TransactionManager.cs    Line: 43 

Stack Trace: 

[No relevant source lines]
[InvalidOperationException: The operation is not valid for the current state of the enlistment.]
   System.Transactions.EnlistmentState.InternalIndoubt(InternalEnlistment enlistment) +40
   System.Transactions.VolatileDemultiplexer.BroadcastInDoubt(VolatileEnlistmentSet& volatiles) +40
   System.Transactions.TransactionStatePromotedIndoubt.EnterState(InternalTransaction tx) +86
   System.Transactions.TransactionStatePromotedBase.InDoubtFromEnlistment(InternalTransaction tx) +22
   System.Transactions.DurableEnlistmentDelegated.InDoubt(InternalEnlistment enlistment, Exception e) +61
   System.Transactions.SinglePhaseEnlistment.InDoubt(Exception e) +207
   System.Data.SqlClient.SqlDelegatedTransaction.SinglePhaseCommit(SinglePhaseEnlistment enlistment) +547
   System.Transactions.TransactionStateDelegatedCommitting.EnterState(InternalTransaction tx) +196
   System.Transactions.TransactionStateDelegated.BeginCommit(InternalTransaction tx, Boolean asyncCommit, AsyncCallback asyncCallback, Object asyncState) +50
   System.Transactions.CommittableTransaction.Commit() +184
   System.Transactions.TransactionScope.InternalDispose() +402
   System.Transactions.TransactionScope.Dispose() +1450
   Orchard.Data.TransactionManager.System.IDisposable.Dispose() in TransactionManager.cs:43
   Autofac.Core.Disposer.Dispose(Boolean disposing) +79
   Autofac.Util.Disposable.Dispose() +46
   Autofac.Core.Lifetime.LifetimeScope.Dispose(Boolean disposing) +21
   Autofac.Util.Disposable.Dispose() +46
   Orchard.Environment.<>c__DisplayClass2.<.ctor>b__0() in WorkContextAccessor.cs:75
   Orchard.Environment.HttpContextScopeImplementation.System.IDisposable.Dispose() in WorkContextAccessor.cs:80
   Orchard.Mvc.Routes.HttpAsyncHandler.EndProcessRequest(IAsyncResult result) in ShellRoute.cs:158
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8841105
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
Feb 8, 2011 at 6:55 AM

Anybody else experiencing this? It is quite a showstopper for my application I am developing so any assistance in this regard would be greatly appreciated...

Feb 16, 2011 at 6:21 AM

I am getting quite desperate with this issue as I am no closer to resolving. If anybody can share some insights or suggest any alternatives I would be eternally greatful....

Feb 16, 2011 at 6:36 AM

Where are you doing that from?

Feb 16, 2011 at 7:32 AM

I have a custom module with added administrative functionality where an administrator can add a new customer. When the customer is created I want to automatically generate a user for that customer and email their logon details to them.

Feb 16, 2011 at 7:41 AM

Right, but what kind of class is your code in? We need a little more context to understand what's going on.

Feb 16, 2011 at 8:30 AM

OK, I created a partial controller class with constructor to inject Orchard services:

	public partial class AdminController : Controller
		private readonly IOrchardServices _orchardServices;
		private readonly IAuthenticationService _authenticationService;
		private readonly IMembershipService _membershipService;

		private Localizer T { get; set; }

		public AdminController(IOrchardServices orchardServices, IAuthenticationService authenticationService, IMembershipService membershipService)
			_orchardServices = orchardServices;
			_authenticationService = authenticationService;
			_membershipService = membershipService;
			T = NullLocalizer.Instance;
Above is simply to reuse code and so I have created some more partial class .cs files that extend the AdminController. 
In one class file I have a method that creates the customer and then the code that attempts to create the user as first post in this thread.
I have debugged the error to the Orchard.Data ITransactionManager. This error occurs when the TransactionScope Dispose method is invoked.
    public class TransactionManager : ITransactionManager, IDisposable {
        private TransactionScope _scope;
        private bool _cancelled;

        public TransactionManager() {
            Logger = NullLogger.Instance;

        public ILogger Logger { get; set; }

        void ITransactionManager.Demand() {
            if (_scope == null) {
                Logger.Debug("Creating transaction on Demand");
                _scope = new TransactionScope(TransactionScopeOption.Required);

        void ITransactionManager.Cancel() {
            Logger.Debug("Transaction cancelled flag set");
            _cancelled = true;

        void IDisposable.Dispose() {
            if (_scope != null) {
                if (!_cancelled) {
                    Logger.Debug("Marking transaction as complete");

                Logger.Debug("Final work for transaction being performed");
                _scope.Dispose(); //ERROR GENERATED HERE...
                Logger.Debug("Transaction disposed");

Hope this makes sense in diagnosing...