简体   繁体   中英

Ajax-Enabled WCF Service - implementing Observer Design Pattern

I created a Notifications system that would notify the user each time there are new notifications.

Currently in my clientside script below, I am calling the web service call every second to ping the server for new notifications. How would I be able to instead have the Ajax-enabled WCF notify the client each time there is a new notification?

Does anyone have any resources, suggestions, or tutorials to implement an Observer Design pattern using an Ajax-enabled WCF?

*Note: using Observer pattern might not be the best way to implement this, any advice on the best pattern (could be my current implementation) is much appreciated.

Clientside:

$(document).ready(function () {

self.setInterval("getNewNotificationsCount()", 1000);
});

function getNewNotificationsCount() {
$.ajax({
    type: "POST",
    url: site_root + "services/NotificationService.svc/json/GetNewNotificationsCount",
    contentType: "application/json; charset=utf-8",
    data: {},
    dataType: "json",
    error: function (request, error, u) {
        alert('error: ' + error);
    },
    success: function (result, status) {
        $('#hip_badge').text(result.d);
    }

});}

Notification Service

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class NotificationService {

{
    [OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json)]
    public int GetNewNotificationsCount()
    {
        NotificationManager nm = new NotificationManager();

        return nm.getNewNotifications(GetUserName());
    }

Are you tied to using WCF? SignalR might be more appropriate for this kind of push notifications.

Update: Just to clarify; SignalR abstracts a mechanism for the server to push data to a number of clients, one of which is JavaScript. On the client you can hook event handlers that deal with notifications as they are sent by your notification manager...

Cheers, Dean

As far as my understanding of the web model goes, there's no way the server(WCF service) can notify the client(AJAX application) without a client requesting for notification. That seems to be the core of the whole Request-Response system between a Client and a Server.

I understand that you need this to make your application more efficient and there are a few suggestions that might help:

  • HTTP polling duplex WCF channel - This doesn't exactly give what you want, as the client would still be polling for changes(notifications) to the client. But still, it is tailor-made for what you would like in your application.
  • If you cannot /don't want to use polling based WCF and stick to your current mechanism, I'd suggest you to change the service from a POST to GET as apparently, you wont have to send any complex data while fetching notifications. A GET is a bit quicker and than a POST in an AJAX scenario.

This will be possible in the new release of WCF (4.5). You can use Websockets for two way communication with HTTP clients. You won't ne using AJAX then, but javascript with websockets. Check out the following link for more info: http://www.codeproject.com/Articles/338789/What-s-new-in-WCF-4-5-WebSocket-support-Part-1-of

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM