/// <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 object ProxyConnectingTo(EndpointId endpoint, Type interfaceType) { { Lokad.Enforce.Argument(() => interfaceType); Lokad.Enforce.With <ArgumentException>( typeof(ICommandSet).IsAssignableFrom(interfaceType), Resources.Exceptions_Messages_ACommandSetTypeMustDeriveFromICommandSet); Lokad.Enforce.Argument(() => endpoint); } // We assume that the interface lives up to the demands we placed on it, i.e.: // - Derives from ICommandSet // - Has only methods, no properties and no events, other than those defined by // ICommandSet // - Every method either returns nothing (Task) or returns a Task<T> object. // All these checks should have been done when the interface was registered // at the remote endpoint. var selfReference = new ProxySelfReferenceInterceptor(); var methodWithoutResult = new CommandSetMethodWithoutResultInterceptor( CommandSendingFunctionFor(endpoint), m_Configuration, m_Diagnostics); var methodWithResult = new CommandSetMethodWithResultInterceptor( CommandSendingFunctionFor(endpoint), m_Configuration, m_Diagnostics); var options = new ProxyGenerationOptions { Selector = new CommandSetInterceptorSelector(), BaseTypeForInterfaceProxy = typeof(CommandSetProxy), }; try { var proxy = m_Generator.CreateInterfaceProxyWithoutTarget( interfaceType, options, new IInterceptor[] { selfReference, methodWithoutResult, methodWithResult }); return(proxy); } catch (GeneratorException e) { throw new UnableToGenerateProxyException( Resources.Exceptions_Messages_UnableToGenerateProxy, e); } }
/// <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); }