Exemplo n.º 1
0
 public void Publish <TServiceType>(PublishServiceCallback <TServiceType> callback)
 {
     if (callback == null)
     {
         throw new ArgumentNullException("callback");
     }
     this.Publish(typeof(TServiceType), new ServiceManager.PublishProxy <TServiceType>(callback).Callback);
 }
Exemplo n.º 2
0
 public override void Publish(Type serviceType, PublishServiceCallback callback)
 {
     if (serviceType == null)
     {
         throw new ArgumentNullException("serviceType");
     }
     if (callback == null)
     {
         throw new ArgumentNullException("callback");
     }
     this.Publish(serviceType, (object)callback);
 }
        public void Publish <TServiceType>(PublishServiceCallback <TServiceType> callback)
        {
            if (callback == null)
            {
                throw FxTrace.Exception.ArgumentNull("callback");
            }

            // Call the standard Subscribe method and use a generic proxy
            PublishProxy <TServiceType> proxy = new PublishProxy <TServiceType>(callback);

            Publish(typeof(TServiceType), proxy.Callback);
        }
Exemplo n.º 4
0
            // <summary>
            // Calls back on the provided callback when someone has published the requested service.
            // If the service was already available, this method invokes the callback immediately.
            //
            // A generic version of this method is provided for convience, and calls the non-generic
            // method with appropriate casts.
            // </summary>
            // <param name="serviceType"></param>
            // <param name="callback"></param>
            public override void Publish(Type serviceType, PublishServiceCallback callback)
            {
                if (serviceType == null)
                {
                    throw FxTrace.Exception.ArgumentNull("serviceType");
                }
                if (callback == null)
                {
                    throw FxTrace.Exception.ArgumentNull("callback");
                }

                Publish(serviceType, (object)callback);
            }
Exemplo n.º 5
0
            public override object GetService(Type serviceType)
            {
                object o = (object)null;

                if (serviceType == null)
                {
                    throw new ArgumentNullException("serviceType");
                }
                if (this._services != null && this._services.TryGetValue(serviceType, out o))
                {
                    if (o == EditingContext.DefaultServiceManager._recursionSentinel)
                    {
                        throw new InvalidOperationException(string.Format((IFormatProvider)CultureInfo.CurrentCulture, Resources.Error_RecursionResolvingService, new object[1]
                        {
                            (object)serviceType.FullName
                        }));
                    }
                    PublishServiceCallback publishServiceCallback = o as PublishServiceCallback;
                    if (publishServiceCallback != null)
                    {
                        this._services[serviceType] = EditingContext.DefaultServiceManager._recursionSentinel;
                        try
                        {
                            o = publishServiceCallback(serviceType);
                            if (o == null)
                            {
                                throw new InvalidOperationException(string.Format((IFormatProvider)CultureInfo.CurrentCulture, Resources.Error_NullService, new object[2]
                                {
                                    (object)publishServiceCallback.Method.DeclaringType.FullName,
                                    (object)serviceType.FullName
                                }));
                            }
                            if (!serviceType.IsInstanceOfType(o))
                            {
                                throw new InvalidOperationException(string.Format((IFormatProvider)CultureInfo.CurrentCulture, Resources.Error_IncorrectServiceType, (object)publishServiceCallback.Method.DeclaringType.FullName, (object)serviceType.FullName, (object)o.GetType().FullName));
                            }
                        }
                        finally
                        {
                            this._services[serviceType] = o;
                        }
                    }
                }
                return(o);
            }
            // <summary>
            // Calls back on the provided callback when someone has published the requested service.
            // If the service was already available, this method invokes the callback immediately.
            //
            // A generic version of this method is provided for convience, and calls the non-generic
            // method with appropriate casts.
            // </summary>
            // <param name="serviceType"></param>
            // <param name="callback"></param>
            public override void Publish(Type serviceType, PublishServiceCallback callback) 
            {
                if (serviceType == null) 
                {
                    throw FxTrace.Exception.ArgumentNull("serviceType");
                }
                if (callback == null) 
                {
                    throw FxTrace.Exception.ArgumentNull("callback");
                }

                Publish(serviceType, (object)callback);
            }
 internal PublishProxy(PublishServiceCallback <TServiceType> callback)
 {
     _genericCallback = callback;
 }
 /// <summary>
 /// Publishes the given service type, but does not declare an instance yet.  When someone
 /// requests the service the PublishServiceCallback will be invoked to create the instance.
 /// The callback is only invoked once and after that the instance it returned is cached.
 ///
 /// A generic version of this method is provided for convenience, and calls the non-generic
 /// method with appropriate casts.
 /// </summary>
 /// <param name="serviceType">The type of service to publish.</param>
 /// <param name="callback">A callback that will be invoked when an instance of the service is needed.</param>
 /// <exception cref="ArgumentNullException">If serviceType or callback is null.</exception>
 /// <exception cref="ArgumentException">If serviceType has already been published.</exception>
 public abstract void Publish(Type serviceType, PublishServiceCallback callback);
Exemplo n.º 9
0
            object GetPublishedService(Type serviceType)
            {
                object service = null;

                if (serviceType == null)
                {
                    throw FxTrace.Exception.ArgumentNull("serviceType");
                }

                if (_services != null && _services.TryGetValue(serviceType, out service))
                {
                    // If this service is our recursion sentinel, it means that someone is recursing
                    // while resolving a service callback.  Throw to break out of the recursion
                    // cycle.
                    if (service == _recursionSentinel)
                    {
                        throw FxTrace.Exception.AsError(new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.Error_RecursionResolvingService, serviceType.FullName)));
                    }

                    // See if this service is a callback.  If it is, invoke it and store
                    // the resulting service back in the dictionary.
                    PublishServiceCallback callback = service as PublishServiceCallback;
                    if (callback != null)
                    {
                        // Store a recursion sentinel in the dictionary so we can easily
                        // tell if someone is recursing
                        _services[serviceType] = _recursionSentinel;
                        try
                        {
                            service = callback(serviceType);
                            if (service == null)
                            {
                                throw FxTrace.Exception.AsError(new InvalidOperationException(
                                                                    string.Format(CultureInfo.CurrentCulture,
                                                                                  Resources.Error_NullService,
                                                                                  callback.Method.DeclaringType.FullName,
                                                                                  serviceType.FullName)));
                            }

                            if (!serviceType.IsInstanceOfType(service))
                            {
                                throw FxTrace.Exception.AsError(new InvalidOperationException(
                                                                    string.Format(CultureInfo.CurrentCulture,
                                                                                  Resources.Error_IncorrectServiceType,
                                                                                  callback.Method.DeclaringType.FullName,
                                                                                  serviceType.FullName,
                                                                                  service.GetType().FullName)));
                            }
                        }
                        finally
                        {
                            // Note, this puts the callback back in place if it threw.
                            _services[serviceType] = service;
                        }
                    }
                }

                // If the service is not found locally, do not walk up the parent chain.
                // This was a major source of unreliability with the component model
                // design.  For a service to be accessible from the editing context, it
                // must be added.

                return(service);
            }