/// <summary> /// Lookups the service references. /// </summary> /// <param name="context">The context.</param> /// <returns>IServiceReference[].</returns> /// Performs a lookup for ServiceReferences that are bound to this /// ServiceRegistry using the specified BundleContext. /// @param context The BundleContext to lookup the ServiceReferences on. /// @return An array of all matching ServiceReferences or null if none /// exist. public IServiceReference[] LookupServiceReferences(IBundleContext context) { int size; List <IServiceReference> references; List <IServiceRegistration> serviceRegs = (List <IServiceRegistration>)PublishedServicesByContext[context]; if (serviceRegs == null) { return(null); } size = serviceRegs.Count; if (size == 0) { return(null); } references = new List <IServiceReference>(); for (int i = 0; i < size; i++) { IServiceRegistration registration = (IServiceRegistration)serviceRegs[i]; IServiceReference reference = registration.GetReference(); references.Add(reference); } if (references.Count == 0) { return(null); } return((IServiceReference[])references.ToArray()); }
/// <summary> /// Lookups the service references. /// </summary> /// <param name="clazz">The clazz.</param> /// <param name="filter">The filter.</param> /// <returns>IServiceReference[].</returns> /// Performs a lookup for ServiceReferences that are bound to this /// ServiceRegistry. If both clazz and filter are null then all bound /// ServiceReferences are returned. /// @param clazz A fully qualified class name. All ServiceReferences that /// reference an object that implement this class are returned. May be /// null. /// @param filter Used to match against published Services. All /// ServiceReferences that match the filter are returned. If a clazz is /// specified then all ServiceReferences that match the clazz and the /// filter parameter are returned. May be null. /// @return An array of all matching ServiceReferences or null /// if none exist. public IServiceReference[] LookupServiceReferences(string clazz, IFilter filter) { int size; List <IServiceReference> references = null; List <IServiceRegistration> serviceRegs = null; if (clazz == null) /* all services */ { serviceRegs = AllPublishedServices; } else { /* services registered under the class name */ if (PublishedServicesByClass.ContainsKey(clazz)) { serviceRegs = (List <IServiceRegistration>)PublishedServicesByClass[clazz]; } } if (serviceRegs == null) { return(null); } size = serviceRegs.Count; if (size == 0) { return(null); } references = new List <IServiceReference>(); for (int i = 0; i < size; i++) { IServiceRegistration registration = (IServiceRegistration)serviceRegs[i]; IServiceReference reference = registration.GetReference(); if ((filter == null) || filter.Match(reference)) { references.Add(reference); } } if (references.Count == 0) { return(null); } return((IServiceReference[])references.ToArray()); }
/// <summary> /// 实现了多个服务接口的服务对象注册 /// </summary> /// <param name="contracts">服务约束数组</param> /// <param name="service">服务对象</param> /// <param name="properties">服务属性,在查找服务时用于过滤服务</param> /// <returns>服务注册信息</returns> public IServiceRegistration RegisterService(string[] contracts, object service, IDictionary <string, object> properties = null) { IServiceRegistration sr = ((IFrameworkService)framework).RegisterService(this, contracts, service, properties); serviceReferenceList.Add(sr.GetReference()); //触发服变更事件 foreach (var bundleObj in this.framework.GetBundles()) { var fireContext = (IContextFireEvent)bundleObj.GetBundleContext(); fireContext.FireServiceChanged(this.bundle, new ServiceEventArgs(ServiceEventArgs.REGISTERED, contracts, sr.GetReference())); } return(sr); }