I'd like to create a standalone windows or console application that I can use in testing/qa to manipulate data in the database.
This simplest thing I am trying to do is write a windows app that creates a new user and adds him to the admin group.
This is the code in general. It does not run successfully...
User newUser = UserDataSource.CreateUser(txtUsername.Text+"U", txtUsername.Text, txtPassword.Text, string.Empty, string.Empty, true, 0, out status);
User u = UserDataSource.CreateUser(txtUsername.Text, txtPassword.Text);
u.UserGroups.Add(new UserGroup(u.UserId, g.GroupId));
u.Save();
I have a default data source connection string set up in the app config. And it looks like AC is finding it if I inspect various objects in the debugger. For example if do:
Token tok = Token.Instance;
then inspect tok.Database... I can see my connection string in there.
I can't even do something super simple like:
Group g = GroupDataSource.Load(1);
That returns null. No exceptions are throw in any of this so I get no hint as to what the problem is.
How to I do this?
thanks
Using CommerceBuilder API from standalone windows/consol app
Re: Using CommerceBuilder API from standalone windows/consol app
I think CommerceBuilder uses HttpContext.
Re: Using CommerceBuilder API from standalone windows/consol app
I would really like to be able to develop standalone operations/maintenance applications that can be run periodically against the system using the CommerceBuilder API. The alternative is to code directly to the datamodel, but that would not be as good as using the same APIs the site uses to access our business data.
Re: Using CommerceBuilder API from standalone windows/consol app
I do this using a webservice to expose the necessary AbleCommerce API objects and methods.
Nick Cole
http://www.ethofy.com
http://www.ethofy.com
Re: Using CommerceBuilder API from standalone windows/consol app
I like that idea. I also just figured out how to do it natively. Code...
The Token Init sequence was what was needed. I'm guessing the Token.Instance is thread local.
Now, the next question is: how do I do the above in an atomic transaction? Ah.. i see... here is transactional code.
Code: Select all
Store store = new Store(1);
// store.Load(1);
Token tok = Token.Instance;
tok.InitStoreContext(store);
Group g = GroupDataSource.LoadForName("Super Users");
MembershipCreateStatus status;
User u = UserDataSource.CreateUser(txtUsername.Text, txtUsername.Text, txtPassword.Text, string.Empty, string.Empty, true, 0, out status);
u.UserGroups.Add(new UserGroup(u.UserId, g.GroupId));
u.Save();
Now, the next question is: how do I do the above in an atomic transaction? Ah.. i see... here is transactional code.
Code: Select all
Store store = new Store();
store.Load(1);
Token tok = Token.Instance;
tok.InitStoreContext(store);
tok.Database.BeginTransaction(IsolationLevel.Serializable);
Group g = GroupDataSource.LoadForName("Super Users");
MembershipCreateStatus status;
User u = UserDataSource.CreateUser(txtUsername.Text, txtUsername.Text, txtPassword.Text, string.Empty, string.Empty, true, 0, out status);
u.UserGroups.Add(new UserGroup(u.UserId, g.GroupId));
u.Save();
tok.Database.RollbackTransaction();