|
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Json;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml;
using SLDuplexDependency.BrowserMaintenanceService;
namespace SLDuplexDependency
{
public partial class MainPage : UserControl
{
DBNotificationClient client;
public MainPage()
{
InitializeComponent();
this.InitialisePageFunctionality();
}
/// <summary>
/// Initialises the page functionality.
/// </summary>
void InitialisePageFunctionality()
{
// make initial call to get employees and bind Sqldependency object to database
SqlServiceReference.SqlNotificationsClient dbService = new SqlServiceReference.SqlNotificationsClient();
dbService.GetEmployeeListCompleted += new EventHandler<SqlServiceReference.GetEmployeeListCompletedEventArgs>(InitialGetEmployeesCompleted); // method to call after initial data retrival
dbService.GetEmployeeListAsync(DateTime.Now.AddYears(-50), DateTime.Now.AddYears(10));
// reference the notifications service and craete the callback hook for database changes
client = new DBNotificationClient(new PollingDuplexHttpBinding(), new EndpointAddress("http://localhost:52333/DBNotificationService/DBNotificationService.svc"));
client.SendNotificationToClientsReceived += (sender, e) =>
{
if (e.data != null) UpdatedAuthorsReceived(e.data); // something to process
};
this.Subscribe(); // make the browser (instance) known to server
}
/// <summary>
/// Initial method for binding the data to the grid
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="SLDuplexDependency.SqlServiceReference.GetEmployeeListCompletedEventArgs"/> instance containing the event data.</param>
void InitialGetEmployeesCompleted(object sender, SLDuplexDependency.SqlServiceReference.GetEmployeeListCompletedEventArgs e)
{
this.authorGrid.ItemsSource = e.Result;
}
/// <summary>
/// Update the grid with new, updated or deleted employee data.
/// </summary>
/// <param name="data">The data.</param>
void UpdatedAuthorsReceived(string data)
{
this.authorGrid.ItemsSource = DeserializeObject(data);
}
/// <summary>
/// Subscribes this instance of the browser,
/// </summary>
void Subscribe()
{
client.SubscribeToNotificationsCompleted += (sender, e) => { };
client.SubscribeToNotificationsAsync();
}
/// <summary>
/// Unsubscribes this instance of the browser
/// </summary>
void Unsubscribe()
{
client.UnsubscribeToNotificationsCompleted += (sender, e) => { };
client.UnsubscribeToNotificationsAsync();
}
/// <summary>
/// Deserializes the object.
/// </summary>
/// <param name="pObject">The p object.</param>
/// <returns></returns>
List<SqlServiceReference.Employee> DeserializeObject(Object objEmployees)
{
try
{
MemoryStream mryStmObj = new MemoryStream(Encoding.UTF8.GetBytes(objEmployees.ToString()));
DataContractJsonSerializer serialiser = new DataContractJsonSerializer(typeof(List<SLDuplexDependency.SqlServiceReference.Employee>));
return (List<SqlServiceReference.Employee>)serialiser.ReadObject(mryStmObj);
}
catch (Exception) {return null;}
}
}
}
|
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.