This question is locked. New answers and comments are not allowed.
Hello,
I'm trying to use OpenAccess in a WCF service. I followed the "SofiaCarRental" Sample that's avaible in the OpenAccess SDK.
Atm I have only a limited project for testing concurrency. And that's where the problems start.
My WCF Contract has 2 methods:
-Customer GetCustumer(int i_customer)
-void UpdateCustomer(Customer newCustomer)
I've wrote a simple console client to test concurrency:
static void Main(string[] args)
{
Service1Client client = new Service1Client();
Customer c = client.GetCustomer(107193);
Customer c2 = client.GetCustomer(107193);
c.Firstname = "blaaaaaaaaaaaaaaa";
client.UpdateCustomer(c);
c2.Lastname = "bluuuuuuubbbbbbbb";
client.UpdateCustomer(c2);
Console.WriteLine("Done!");
Console.ReadLine();
}
As you can see it gets the same Customer 2 times and changes the "firstname" field of the first copy and the "lastname" field of the 2nd copy. Both copies are send to the service to be updated.
This is how the service looks like:
The UpdateCustomer Method gets the Customer again from the database then updates all the field of that instance with the ones of the Method parameter if they have been changed. This is just like in the "SofiaCarRental" Sample.
However, no optimistic concurrency mode is working with this approach.
When I use timestamp, OpenAccess doesn't use the timestamp field from "newCustomer". My only guess is it uses an internal field somewhere that is set when getting "oldCustomer".
When I use "Changed" as concurrency mode, the second update overwrites the first one, setting the "firstname" field back to it's original value.
How can I solve this problem?
Thanks for any help you can give me!
I'm trying to use OpenAccess in a WCF service. I followed the "SofiaCarRental" Sample that's avaible in the OpenAccess SDK.
Atm I have only a limited project for testing concurrency. And that's where the problems start.
My WCF Contract has 2 methods:
-Customer GetCustumer(int i_customer)
-void UpdateCustomer(Customer newCustomer)
I've wrote a simple console client to test concurrency:
{
Service1Client client = new Service1Client();
Customer c = client.GetCustomer(107193);
Customer c2 = client.GetCustomer(107193);
c.Firstname = "blaaaaaaaaaaaaaaa";
client.UpdateCustomer(c);
c2.Lastname = "bluuuuuuubbbbbbbb";
client.UpdateCustomer(c2);
Console.WriteLine("Done!");
Console.ReadLine();
}
As you can see it gets the same Customer 2 times and changes the "firstname" field of the first copy and the "lastname" field of the 2nd copy. Both copies are send to the service to be updated.
This is how the service looks like:
public class Service1 : IService1
{
public Customer GetCustomer(int i_customer)
{
CustomerModel customerModel = new CustomerModel();
var query = from cust in customerModel.Customers
where cust.I_customer == i_customer
select cust;
Customer customer = query.First();
return customer;
}
public void UpdateCustomer(Customer newCustomer)
{
CustomerModel customerModel = new CustomerModel();
var query = from cust in customerModel.Customers
where cust.I_customer == newCustomer.I_customer
select cust;
Customer oldCustomer = query.FirstOrDefault();
PropertyInfo[] newCust = newCustomer.GetType().GetProperties();
foreach (PropertyInfo prop in newCust)
{
if (prop.GetValue(oldCustomer, null) == null || prop.GetValue(oldCustomer, null).Equals(prop.GetValue(newCustomer, null)))
continue;
prop.SetValue(oldCustomer, prop.GetValue(newCustomer, null), null);
}
customerModel.SaveChanges();
}
}
The UpdateCustomer Method gets the Customer again from the database then updates all the field of that instance with the ones of the Method parameter if they have been changed. This is just like in the "SofiaCarRental" Sample.
However, no optimistic concurrency mode is working with this approach.
When I use timestamp, OpenAccess doesn't use the timestamp field from "newCustomer". My only guess is it uses an internal field somewhere that is set when getting "oldCustomer".
When I use "Changed" as concurrency mode, the second update overwrites the first one, setting the "firstname" field back to it's original value.
How can I solve this problem?
Thanks for any help you can give me!