I am implementing a duplex channel and all works ok. Now I have to implement it in a thread safe manner.I am kind of stuck and wondering if you could point me or suggest anything.
This is just a quick demo code to give you an idea of what I am doing.
How can I make the Buy Function Process asyncronous and ThreadSafe?
[ServiceContract(CallbackContract = typeof(ICustomerServiceCallback))]
public interface ICustomerService
{
[OperationContract(IsOneWay = true)]
void Buy(List<Product> productList);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class CustomerService : ICustomerService
{
private ICustomerServiceCallback _customerServiceCallback;
//NEED TO CONVERT THIS TO BE WORKING ON A DIFFERENT THREAD AND NOTIFY.
public void Buy(List<Product> productList)
{
_customerServiceCallback = OperationContext.Current.GetCallbackChannel<ICustomerServiceCallback>();
foreach (var product in productList)
{
//go to the db or do something and notify me after each product is processed.
_customerServiceCallback.NotifyAfterEachOrder(true);
}
}
}
public interface ICustomerServiceCallback
{
[OperationContract]void NotifyAfterEachOrder(bool status);
}
[DataContract]
public class Product
{
[DataMember]public String Name { get; set; }
}
[CallbackBehavior(UseSynchronizationContext = false)]
public class Client : ICustomerServiceCallback
{
public void NotifyAfterEachOrder(bool status)
{
//do something now.
}
}