Vývoj moderních aplikací na platformě Microsoft .NET

Je vyžadována podpora jazyka JavaScript

Některé stránky na tomto webu vyžadují podporu jazyka JavaScript. Váš webový prohlížeč jazyk JavaScript nepodporuje nebo jazyk JavaScript není povolen.

Chcete-li zjistit, zda webový prohlížeč podporuje jazyk JavaScript nebo jazyk JavaScript chcete povolit, přečtěte si nápovědu k vašemu webovému prohlížeči.


proxy.cs

Download file

Toto je zdrojový kód souboru proxy.cs

using System;
using SilverlightDuplexSample.NotificationService;

namespace SilverlightDuplexSample
{
    internal sealed class UpdateReceivedEventArgs : EventArgs
    {
        #region member varible and default property initialization
        public string Message { get; private set; }
        #endregion

        #region constructors and destructors
        public UpdateReceivedEventArgs(string message)
        {
            this.Message = message;
        }
        #endregion
    }

    internal static class Proxy
    {
        #region delegate and events
        public static event EventHandler<UpdateReceivedEventArgs> UpdateReceived;
        #endregion

        #region member varible and default property initialization
        private static NotificationServiceClient s_NotificationService;
        private static System.Threading.Timer s_NotificationServiceStayAliveTimer = new System.Threading.Timer(InitializeNotificationService, null, TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(5));
        #endregion

        #region action methods
        public static void Initialize()
        {
            InitializeNotificationService(null);
        }

        public static void Publish(string message)
        {
            bool reconnected = EnsureProxy();
            var service = s_NotificationService;
            Guid token = Guid.NewGuid();
            EventHandler<System.ComponentModel.AsyncCompletedEventArgs> handler = null;
            handler = (s, e) =>
                {
                    if ((Guid)e.UserState != token)
                    {
                        return;
                    }
                    service.PublishCompleted -= handler;

                    if (e.Error != null)
                    {
                        CloseProxy();
                        if (reconnected)
                        {
                            Publish(message);
                            return;
                        }
                        throw e.Error;
                    }
                };
            service.PublishCompleted += handler;
            service.PublishAsync(message, token);
        }
        #endregion

        #region private member functions
        /// <summary>
        /// Register NotificationService client or reset inactivity timeout
        /// </summary>
        /// <param name="state">State</param>
        private static void InitializeNotificationService(object state)
        {
            bool reconnected = EnsureProxy();
            var service = s_NotificationService;
            Guid token = Guid.NewGuid();
            EventHandler<System.ComponentModel.AsyncCompletedEventArgs> handler = null;
            handler = (s, e) =>
                {
                    if ((Guid)e.UserState != token)
                    {
                        return;
                    }
                    service.RegisterClientCompleted -= handler;

                    if (e.Error != null)
                    {
                        CloseProxy();
                        if (!reconnected)
                        {
                            InitializeNotificationService(null);
                        }
                    }
                };
            service.RegisterClientCompleted += handler;
            service.RegisterClientAsync(token);
        }

        private static void NotificationService_UpdateReceived(object sender, SilverlightDuplexSample.NotificationService.UpdateReceivedEventArgs e)
        {
            if (UpdateReceived != null)
            {
                UpdateReceived(null, new UpdateReceivedEventArgs(e.Message));
            }
        }

        private static bool EnsureProxy()
        {
            if (s_NotificationService != null)
            {
                return false;
            }

            var serviceUri = new Uri(System.Windows.Browser.HtmlPage.Document.DocumentUri, "NotificationService.svc");

            var address = new System.ServiceModel.EndpointAddress(serviceUri);
            var binding = new System.ServiceModel.PollingDuplexHttpBinding(System.ServiceModel.Channels.PollingDuplexMode.MultipleMessagesPerPoll);
            s_NotificationService = new NotificationServiceClient(binding, address);
            s_NotificationService.UpdateReceived += new EventHandler<SilverlightDuplexSample.NotificationService.UpdateReceivedEventArgs>(NotificationService_UpdateReceived);
            return true;
        }

        private static void CloseProxy()
        {
            if (s_NotificationService == null)
            {
                return;
            }

            s_NotificationService.UpdateReceived -= new EventHandler<SilverlightDuplexSample.NotificationService.UpdateReceivedEventArgs>(NotificationService_UpdateReceived);
            s_NotificationService.Abort();
            s_NotificationService = null;
        }
        #endregion
    }
}