Overview
Notification service is a kind of message broadcast service, in DotNetAge it used to send the conent changes message to subscribers such as subscribe articles,
threads, comments and ect.
How does it work?
Before you start to use notification service let's learn how doest work first.
- Users subscribe some resource through UI.
- Notifier add the subscription token and user name to the subscription list.
- When content change the App (Controller/Module/Service) invoke the Notifier.Send(token) method to tell notification service content has been changed.
- Notification service send the changed message to all subscribers.
DotNetAge comes load email subscription sender and private message sender. Developers can extend the subscription sender to support more subscription sourc
such as MSMQ, Log , Web Service and ect.
How to use notification service
There are some basic concepts developers need to know:
Token
Token is used to identify a subscription list, it can be any string but must be unique. Recommended use the uri as a subscription token. e.g: There is a "Library"
category in top site categories, in DotNetAge each category has a unique permalink , so we could use the permalink as a token for subscription.
notes:We not recommended use the entry object id as subscription token because mybe duplicated to identity confusion.
Notifier
Typically, developers should use the Notifier to invoke Notification service and do not need to implement or instantiate INotificationService interface and
ISubscriptionSender interface.
Subscribe
Example : Subscribe a thread.
The following example decide current user whether in thead subscription list and add current user to the list.
var token=Url.Action("Index","Thread",new {Area="Community",id=1}).ToLower();
if (!Notifier.IsSubscribed(token))
{
Notifier.Subscribe(token);
}
The following table list the Subscribe methods for Notifier class:
Method | Parameters | Description |
---|---|---|
public static bool Subscribe(string token) | token : The subscription token | Add current user to a subscription list. |
public static bool Subscribe(string token, string userName) |
|
|
public static bool IsSubscribed(string token, string userName) |
|
|
public static bool IsSubscribed(string token) | token: Specified the subscription token | Returns true when the current user is in subscription list. |
Unsubscribe
Example:Unsubscibe the thread
The following example shows how to unsubsibe the current user from a thread subscription list.
var token=Url.Action("Index","Thread",new {Area="Community",id=1}).ToLower();
if (!Notifier.IsSubscribed(token))
{
Notifier.Unsubscribe(token);
}
The following table list the Unsubscribe methods for Notifier class:
Method | Parameters | Description | ||
---|---|---|---|---|
public static void Unsubscribe(string token) | token:Specified the subscription token. | Remove the current user from subscription list. | public static void Unsubscribe(string token, string userName) |
|
Send
Example:Send message to subscribers.
var wrapper = new CategoryWrapper(article.Category);
Notifier.Send(wrapper.PermaLink.ToLower(), wrapper.Title, wrapper.Summary);
The following table list the Send methods for Notifier class:
Method | Parameters | Description |
---|---|---|
public static void Send(string token, string title, string content, string contentType = "text/html", string encoding = "utf-8", string type = "") | ||
public static void Send(string token, IMessage message) |
|
Send a |
Subscribe by http request
DotNetAge allows developers in addition to use the notification service from code and also can subscribe by post http request to server.
In SyndicationController define an action method: pulbic bool Subscribe(string token) , it's a toggle method. When accept a http post it will descide current user
whether in subscription list.If user not exits in list will add it in and vice versa.
The follow example subscribe a category from ajax:
$.ajax({
url:"@Url.Action("Subscribe","Syndication",new {Area="",token=category.PermaLink.ToLower()})",
type:"POST"
});
How to extend the Notification service
Developers could implement the ISubscriptionSender and support more subscription source.
When confronted with these situations developers should consider extending the notification service:
- Need notification service send message to other subscription source such as MSMQ, Log or webservice.
- Send a object that implement from IMessage for special handling.
The following figure is the DotNetAge subscription senders class diagram:
ISubscriptionSender
///
/// Define the methods to send the message to subscribers.
///
public interface ISubscriptionSender
{
///
/// Send the message to subscribers.
///
/// The user name list of the sbscribers.
/// The message object to send.
void Send(string[] subscribers, IMessage message);
///
/// Get the message filter. Determin which message should send.
///
string Filter { get; }
}
To add a new subscription sender developers just need to do following steps:
- Implement the ISubscriptionSender interface.
- Register the sender class to unity.config.
Example:Create a WebService subscription sender
This example create a subscription sender to send the message to a WebService.
//The webservice
public myWebService.MessageSaver.Save(string[] users,string title,string content);
//Inherit from SubscriptionSenderBase abstract class
public class MySubscriptionSender:SubscriptionSenderBase
{
public override void Send(string[] subscribers, IMessage message)
{
var saver=new myWebService.MessageSaver();
saver.Save(subscribers,message.Title,message.Content);
}
}
Register subscription sender to unity.config
In order to add the new subscription sender to notification service developer must register the class to the unity.config after implemention.
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="MySubscriptionSender" type="Example.MySubscriptionSender,Example" />
<container>
<register type="ISubscriptionSender" mapTo="MySubscriptionSender" name="mySubscriptionSender" />
container>
unity>
-
-
Читает(589)
-
Trackback(0)
-
Постоянная ссылка
Комментарии (0)