Notify My Wcf Service When My Database Is Updated

Notify my WCF service when my database is updated

If your database is SQL Server 2005 and above you can try this solution: Remove pooling for data changes from a WCF front end.

As a side note, never call external processes from a trigger, don't make web calls from a trigger. Is a guaranteed recipe for disaster.

Update

For those interested in mixing Query Notifications with LINQ to SQL I recommend Using SQLDependency objects with LINQ.

Notify WCF Service to cancel the currently executing operation and return remaining data.

What InstanceContextMode are you using?

What you want to do will work with InstanceContextMode Singleton or Session, but not PerCall.

I suppose that your method that initiates the delete in bulk operation is OneWay.

Create a bool field (initially false) in your service. Your loop that deletes records one by one checks that flag and stops if the flag is true.

Create another method in your service that sets that field to true. Call the new method from your website when you press the "Cancel Delete" button. The looop for deleting records will break.

Returning the records that were not deleted will be a bit tricky. Your list of records to delete will need to be a memebr of the service class also, so that the method that stops the delete has access to the list also. The method that deletes from the db shoulkd also remove the records from the list one by one so you can keep track of remaining items.

You could return immediately in the second method the list, but thia does not guarantee accurate results. What I would do is do a wait of 200 ms before returning. That way you know that the item removed from the list is really deleted in the db.

Hope this is clear...

WCF Duplex - Push different notifications to each client?

In any way with duplex communication, you need to maintain TCP channel opened from server to client to be able to send notification.

Client is the one who initiate connection to server, and you need to keep this connection open, if this connection is lost you can't (shouldn't) initiate connection from server to client, because client can be behind NAT, have firewall, etc.

So in any way there must be some static (singleton) object on server side that keeps clients connections list, thought this is not necessarily WCF service. You can dependency inject this object into service constructor.

public class ProductRepository
{
private EventAggregator eventAggregator;

public void Add(Product product)
{
//...
eventAggregator.Publish(new NewProductEvent(product))
}
}
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
public class EventPublishingService
{
private IClientCallback client;
private EventAggregator eventAggregator;
private Func<Product, bool> predicate;

public EventPublishingService(...)
{
eventAggregator.Subscibe<NewProductEvent>(OnNewProduct);
}
private void OnNewProduct(NewProductEvent e)
{
if (predicate(e.Product)==true) client.Notify(e.Product);
}

public void Subscribe()
{
client = OperationContext.Current.GetCallbackChannel<IClientCallback>()
var user = ServiceSecurityContext.PrimaryIdentity;
predicate = GetFilterForUser(user);
}
}

Best way to notify clients for an update?

I think that the pull model that you propose is probably the easiest to implement and most straightforward solution to the problem. Knowing that you are using WCF webservices, however, you have the possibility to implement a push model using WCF Callbacks. In this model you write service contracts just as normal but here the clients register themselves on the server. On data updates on the server the server calls a callback function on all registered clients, which respond by fetching new data.

The typical showcase application for this model is a ticketing system, where tickets are booked and released often. This approach eliminates the need for clients to constantly poll the server.

The article called Eliminate Server Polling with WCF Callbacks gives more details on the subject and also comes with a ticketing system example. This article also describes this method.

I am not saying that this is better than what you suggest yourself, but it may be worth looking into.



Related Topics



Leave a reply



Submit