Esempio n. 1
0
        /// <summary>
        /// Enregistre un service distant.
        /// </summary>
        /// <param name="contractType">Type de l'interface représentant le contrat.</param>
        public void RegisterRemoteService(Type contractType)
        {
            if (contractType == null)
            {
                throw new ArgumentNullException("contractType");
            }

            if (!contractType.IsInterface)
            {
                throw new ArgumentException("contractType " + contractType.FullName + "must define an interface.");
            }

            ILog log = LogManager.GetLogger("Kinetix.Application");

            if (log.IsDebugEnabled)
            {
                log.Debug("Enregistrement du service distant " + contractType.FullName);
            }

            /* REMARQUE : on ne peut pas appeler HasWcfClientEndPoint à l'enregistrement car web.config n'est pas accessible. */
            InterceptionPipelineEventArgs args = new InterceptionPipelineEventArgs(contractType, new List <InjectionMember> {
                new Interceptor <InterfaceInterceptor>(),
                new InterceptionBehavior <LogInterceptionBehavior>(),
                new InterceptionBehavior <AnalyticsInterceptionBehavior>()
            });

            if (this.RegisteringInterceptors != null)
            {
                this.RegisteringInterceptors(this, args);
            }

            var remoteServiceFactory = new InjectionFactory(container => {
                /* TODO VERSION TEMPORAIRE : il faut faire un dispose sur la factory quand le service n'est plus utilisé. */
                try {
                    ChannelFactory factory;
                    var instance = GetRemoteService(contractType, out factory);
                    return(instance);
                } catch (Exception ex) {
                    log.Error($"Erreur à l'instancation du service WCF distant {contractType}", ex);
                    throw ex;
                }
            });

            _container.RegisterType(contractType, remoteServiceFactory);
        }
Esempio n. 2
0
        /// <summary>
        /// Enregistre une instance de service.
        /// </summary>
        /// <param name="contractType">Type de l'interface représentant le contrat.</param>
        /// <param name="serviceType">Type du service.</param>
        public void RegisterLocalService(Type contractType, Type serviceType)
        {
            if (contractType == null)
            {
                throw new ArgumentNullException("contractType");
            }

            if (serviceType == null)
            {
                throw new ArgumentNullException("serviceType");
            }

            if (!contractType.IsInterface)
            {
                throw new ArgumentException("contractType " + contractType.FullName + "must define an interface.");
            }

            if (!contractType.IsAssignableFrom(serviceType))
            {
                throw new ArgumentException("Invalid serviceType " + serviceType.FullName);
            }

            if (_localServices.Contains(contractType))
            {
                throw new NotSupportedException("Contract already registered for contractType " + contractType.FullName);
            }

            ILog log = LogManager.GetLogger("Kinetix.Application");

            if (log.IsDebugEnabled)
            {
                log.Debug("Enregistrement du service " + contractType.FullName);
            }

            List <Accessor> referenceAccessors    = new List <Accessor>();
            List <Accessor> primaryKeyAccessors   = new List <Accessor>();
            List <Accessor> autoCompleteAccessors = new List <Accessor>();
            List <Accessor> fileAccessors         = new List <Accessor>();
            List <string>   disableLogList        = new List <string>();

            ParseAccessors(contractType, referenceAccessors, primaryKeyAccessors, autoCompleteAccessors, fileAccessors, disableLogList);

            if (!HasWcfClientEndPoint(contractType) || HttpContext.Current == null)
            {
                ServiceEndpointElement sep = HasWcfServerEndPoint(serviceType, contractType);
                if (sep != null && HttpContext.Current == null && !DisableServiceHosts)
                {
                    if (log.IsInfoEnabled)
                    {
                        log.Info("Inscription du service " + sep.Contract + " à l'adresse " + sep.Address + ".");
                    }

                    _container.RegisterType(
                        serviceType,
                        new Interceptor <InterfaceInterceptor>(),
                        new InterceptionBehavior <LogInterceptionBehavior>());
                    UnityServiceHost host = new UnityServiceHost(serviceType);
                    host.Container = _container;
                    host.Open();
                    _hostList.Add(host);
                }
                else
                {
                    InterceptionPipelineEventArgs args = new InterceptionPipelineEventArgs(contractType, new List <InjectionMember> {
                        new Interceptor <InterfaceInterceptor>(),
                        //// new InterceptionBehavior<HighAvailabilityInterceptionBehavior>(),
                        new InterceptionBehavior <LogInterceptionBehavior>(),
                        new InterceptionBehavior <TransactionInterceptionBehavior>(),
                        new InterceptionBehavior <AnalyticsInterceptionBehavior>()
                    });
                    if (this.RegisteringInterceptors != null)
                    {
                        this.RegisteringInterceptors(this, args);
                    }

                    _container.RegisterType(
                        contractType,
                        serviceType,
                        args.Interceptors.ToArray());
                    _localServices.Add(contractType);
                }
            }

            this.RegisterReferenceAccessors(referenceAccessors);
            this.RegisterPrimaryKeyAccessors(primaryKeyAccessors);
            this.RegisterAutoCompleteAccessors(autoCompleteAccessors);
            this.RegisterFileAccessors(fileAccessors);
        }