|
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Web;
using SLDuplexDependency.Web.Classes;
namespace SLDuplexDependency.Web.DBNotificationService
{
/// <summary>
/// This is an implementation of IDBNotificationService interface
/// </summary>
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)]
public class DBNotificationService : IDBNotificationService
{
/// <summary>
/// The object used to lock code down when it is editing static data
/// </summary>
private Object syncRoot = new Object();
/// <summary>
/// This method should be executed to subscribe to notifications from server.
/// </summary>
public void SubscribeToNotifications()
{
IDBNotificationCallbackContract ch = OperationContext.Current.GetCallbackChannel<IDBNotificationCallbackContract>();
string sessionId = OperationContext.Current.Channel.SessionId;
//Any message from a client we haven't seen before causes the new client to be added to our list
lock (syncRoot)
{
if (!SilverlightClientsList.IsClientConnected(sessionId))
{
SilverlightClientsList.AddCallbackChannel(sessionId, ch);
OperationContext.Current.Channel.Closing += new EventHandler(Channel_Closing);
OperationContext.Current.Channel.Faulted += new EventHandler(Channel_Faulted);
}
}
}
/// <summary>
/// Handles the Closing event of the Channel control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void Channel_Closing(object sender, EventArgs e)
{
IContextChannel channel = (IContextChannel)sender;
ClientDisconnect(channel.SessionId);
}
/// <summary>
/// Handles the Faulted event of the Channel control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void Channel_Faulted(object sender, EventArgs e)
{
IContextChannel channel = (IContextChannel)sender;
ClientDisconnect(channel.SessionId);
}
/// <summary>
/// Disconnect the client.
/// </summary>
/// <param name="sessionId">The session id.</param>
private void ClientDisconnect(string sessionId)
{
lock (syncRoot)
{
if (SilverlightClientsList.IsClientConnected(sessionId))
SilverlightClientsList.DeleteClient(sessionId);
}
}
/// <summary>
/// This method should be executed to unsubscribe to notifications from server.
/// </summary>
public void UnsubscribeToNotifications()
{
ClientDisconnect(OperationContext.Current.Channel.SessionId);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.