public void Invoke()
        {
            EndpointId     processedId  = null;
            NotificationId registration = null;
            var            sink         = new Mock <ISendNotifications>();
            {
                sink.Setup(s => s.RegisterForNotification(It.IsAny <EndpointId>(), It.IsAny <NotificationId>()))
                .Callback <EndpointId, NotificationId>((e, s) =>
                {
                    processedId  = e;
                    registration = s;
                });
            }

            var action = new RegisterForNotificationProcessAction(sink.Object);

            var id  = new EndpointId("id");
            var reg = NotificationId.Create(typeof(InteractionExtensionsTest.IMockNotificationSetWithTypedEventHandler).GetEvent("OnMyEvent"));
            var msg = new RegisterForNotificationMessage(id, reg);

            action.Invoke(msg);

            Assert.AreEqual(id, processedId);
            Assert.AreEqual(reg, registration);
        }
Beispiel #2
0
        public void FromMessage()
        {
            var translator = new NotificationRegistrationConverter();

            var id  = NotificationId.Create(typeof(InteractionExtensionsTest.IMockNotificationSetWithTypedEventHandler).GetEvent("OnMyEvent"));
            var msg = new RegisterForNotificationMessage(new EndpointId("a"), id);

            var data = translator.FromMessage(msg);

            Assert.IsInstanceOf(typeof(NotificationRegistrationData), data);
            Assert.AreSame(msg.Id, data.Id);
            Assert.AreSame(msg.Sender, data.Sender);
            Assert.AreSame(msg.InResponseTo, data.InResponseTo);
            Assert.AreEqual(NotificationIdExtensions.Serialize(id), ((NotificationRegistrationData)data).NotificationId);
        }
        /// <summary>
        /// Generates a proxy object for the given command set and the specified endpoint.
        /// </summary>
        /// <param name="endpoint">The endpoint for which a proxy must be made.</param>
        /// <param name="interfaceType">The interface of the command set for which a proxy must be made.</param>
        /// <returns>
        /// The interfaced proxy.
        /// </returns>
        public INotificationSet ProxyConnectingTo(EndpointId endpoint, Type interfaceType)
        {
            {
                Lokad.Enforce.Argument(() => interfaceType);
                Lokad.Enforce.With <ArgumentException>(
                    typeof(INotificationSet).IsAssignableFrom(interfaceType),
                    Resources.Exceptions_Messages_ANotificationSetTypeMustDeriveFromINotificationSet);
            }

            // We assume that the interface lives up to the demands we placed on it, i.e.:
            // - Derives from INotificationSet
            // - Has only events, no properties and no methods
            // - Every event is based on either the EventHandler or the EventHandler<T> delegate.
            // All these checks should have been done when the interface was registered
            // at the remote endpoint.
            var selfReference   = new ProxySelfReferenceInterceptor();
            var addEventHandler = new NotificationEventAddMethodInterceptor(
                interfaceType,
                eventInfo =>
            {
                var msg = new RegisterForNotificationMessage(m_Local, eventInfo);
                m_SendWithoutResponse(endpoint, msg, CommunicationConstants.DefaultMaximuNumberOfRetriesForMessageSending);
            },
                m_Diagnostics);
            var removeEventHandler = new NotificationEventRemoveMethodInterceptor(
                interfaceType,
                eventInfo =>
            {
                var msg = new UnregisterFromNotificationMessage(m_Local, eventInfo);
                m_SendWithoutResponse(endpoint, msg, CommunicationConstants.DefaultMaximuNumberOfRetriesForMessageSending);
            },
                m_Diagnostics);

            var options = new ProxyGenerationOptions
            {
                Selector = new NotificationSetInterceptorSelector(),
                BaseTypeForInterfaceProxy = typeof(NotificationSetProxy),
            };

            var proxy = m_Generator.CreateInterfaceProxyWithoutTarget(
                interfaceType,
                options,
                new IInterceptor[] { selfReference, addEventHandler, removeEventHandler });

            return((INotificationSet)proxy);
        }
        public void ProxyConnectingToEventWithNormalEventHandler()
        {
            var local = new EndpointId("local");
            RegisterForNotificationMessage intermediateMsg = null;
            SendMessage messageSender = (e, m, r) =>
            {
                intermediateMsg = m as RegisterForNotificationMessage;
            };

            var systemDiagnostics = new SystemDiagnostics((p, s) => { }, null);

            var builder = new NotificationProxyBuilder(local, messageSender, systemDiagnostics);

            var remoteEndpoint = new EndpointId("other");
            var proxy          = builder.ProxyConnectingTo <InteractionExtensionsTest.IMockNotificationSetWithEventHandler>(remoteEndpoint);

            object    sender       = null;
            EventArgs receivedArgs = null;

            proxy.OnMyEvent +=
                (s, e) =>
            {
                sender       = s;
                receivedArgs = e;
            };

            var id = NotificationId.Create(typeof(InteractionExtensionsTest.IMockNotificationSetWithEventHandler).GetEvent("OnMyEvent"));

            Assert.AreEqual(id, intermediateMsg.Notification);

            var notificationObj = proxy as NotificationSetProxy;

            Assert.IsNotNull(notificationObj);

            var args = new EventArgs();

            notificationObj.RaiseEvent(id, args);

            Assert.AreSame(proxy, sender);
            Assert.AreSame(args, receivedArgs);
        }