Exemplo n.º 1
0
        protected override ServiceDescription CreateDescription(out IDictionary <string, ContractDescription> implementedContracts)
        {
            ServiceDescription description = _serviceProvider.GetService <ServiceDescription <TService> >();

            ServiceBehaviorAttribute serviceBehavior = description.Behaviors.Find <ServiceBehaviorAttribute>();
            TService serviceInstanceUsedAsABehavior  = (TService)serviceBehavior.GetWellKnownSingleton();

            if (serviceInstanceUsedAsABehavior == null)
            {
                serviceInstanceUsedAsABehavior = (TService)serviceBehavior.GetHiddenSingleton();
                _disposableInstance            = serviceInstanceUsedAsABehavior as IDisposable;
            }

            // serviceInstanceUsedAsBehavior will be null when InstanceContextMode != Single
            // In this case, we need to check if the service type is a behavior and if it is, create an instance to apply to behaviors
            if ((typeof(IServiceBehavior).IsAssignableFrom(typeof(TService)) || typeof(IContractBehavior).IsAssignableFrom(typeof(TService))) &&
                serviceInstanceUsedAsABehavior == null)
            {
                serviceInstanceUsedAsABehavior = _serviceProvider.GetService <TService>(); // First try DI to get an instance
                if (serviceInstanceUsedAsABehavior == null)                                // Not in DI so create the old WCF way using reflection
                {
                    serviceInstanceUsedAsABehavior = ServiceDescription.CreateImplementation <TService>();
                }

                _disposableInstance = serviceInstanceUsedAsABehavior as IDisposable;
            }

            if (serviceBehavior.InstanceContextMode == InstanceContextMode.Single)
            {
                serviceBehavior.ServicePovider = _serviceProvider;

                // If using Single, then ServiceBehavior fetched/created the instance and need to set on SingletonInstance
                Debug.Assert(serviceInstanceUsedAsABehavior != null, "Service behavior should have created a singleton instance");
                SingletonInstance = serviceInstanceUsedAsABehavior;
            }
            else
            {
                serviceBehavior.InstanceProvider = new DependencyInjectionWithLegacyFallbackInstanceProvider(_serviceProvider, typeof(TService));
            }

            if (serviceInstanceUsedAsABehavior is IServiceBehavior behavior)
            {
                description.Behaviors.Add(behavior);
            }

            ReflectedContractCollection reflectedContracts = new ReflectedContractCollection();
            List <Type> interfaces = ServiceReflector.GetInterfaces <TService>();

            for (int i = 0; i < interfaces.Count; i++)
            {
                Type contractType = interfaces[i];
                if (!reflectedContracts.Contains(contractType))
                {
                    ContractDescription contract;
                    if (serviceInstanceUsedAsABehavior != null)
                    {
                        contract = ContractDescription.GetContract <TService>(contractType, serviceInstanceUsedAsABehavior);
                    }
                    else
                    {
                        contract = ContractDescription.GetContract <TService>(contractType);
                    }

                    reflectedContracts.Add(contract);
                    Collection <ContractDescription> inheritedContracts = contract.GetInheritedContracts();
                    for (int j = 0; j < inheritedContracts.Count; j++)
                    {
                        ContractDescription inheritedContract = inheritedContracts[j];
                        if (!reflectedContracts.Contains(inheritedContract.ContractType))
                        {
                            reflectedContracts.Add(inheritedContract);
                        }
                    }
                }
            }
            ReflectedContracts = reflectedContracts;

            implementedContracts = reflectedContracts.ToImplementedContracts();
            return(description);
        }
Exemplo n.º 2
0
        protected override ServiceDescription CreateDescription(out IDictionary <string, ContractDescription> implementedContracts)
        {
            ServiceDescription description;
            TService           instance = _serviceProvider.GetService <TService>();

            if (instance != null)
            {
                description = ServiceDescription.GetService(instance);
            }
            else
            {
                description = ServiceDescription.GetService <TService>();
            }

            // Any user supplied IServiceBehaviors can be applied now
            var serviceBehaviors = _serviceProvider.GetServices <IServiceBehavior>();

            foreach (var behavior in serviceBehaviors)
            {
                description.Behaviors.Add(behavior);
            }

            ServiceBehaviorAttribute serviceBehavior = description.Behaviors.Find <ServiceBehaviorAttribute>();
            object serviceInstanceUsedAsABehavior    = serviceBehavior.GetWellKnownSingleton();

            if (serviceInstanceUsedAsABehavior == null)
            {
                serviceInstanceUsedAsABehavior = serviceBehavior.GetHiddenSingleton();
                _disposableInstance            = serviceInstanceUsedAsABehavior as IDisposable;
            }

            if ((typeof(IServiceBehavior).IsAssignableFrom(typeof(TService)) || typeof(IContractBehavior).IsAssignableFrom(typeof(TService))) &&
                serviceInstanceUsedAsABehavior == null)
            {
                if (instance == null)
                {
                    serviceInstanceUsedAsABehavior = ServiceDescription.CreateImplementation <TService>();
                }
                else
                {
                    serviceInstanceUsedAsABehavior = instance;
                }

                _disposableInstance = serviceInstanceUsedAsABehavior as IDisposable;
            }

            if (instance != null)
            {
                if (serviceBehavior.InstanceContextMode == InstanceContextMode.Single)
                {
                    SingletonInstance = instance;
                }
                else
                {
                    serviceBehavior.InstanceProvider = new DependencyInjectionInstanceProvider(_serviceProvider, typeof(TService));
                    if (serviceInstanceUsedAsABehavior == null && instance is IDisposable disposable)
                    {
                        disposable.Dispose();
                    }
                }
            }

            if (instance == null)
            {
                if (serviceInstanceUsedAsABehavior is IServiceBehavior)
                {
                    description.Behaviors.Add((IServiceBehavior)serviceInstanceUsedAsABehavior);
                }
            }

            ReflectedContractCollection reflectedContracts = new ReflectedContractCollection();
            List <Type> interfaces = ServiceReflector.GetInterfaces <TService>();

            for (int i = 0; i < interfaces.Count; i++)
            {
                Type contractType = interfaces[i];
                if (!reflectedContracts.Contains(contractType))
                {
                    ContractDescription contract = null;
                    if (serviceInstanceUsedAsABehavior != null)
                    {
                        contract = ContractDescription.GetContract <TService>(contractType, serviceInstanceUsedAsABehavior);
                    }
                    else
                    {
                        contract = ContractDescription.GetContract <TService>(contractType);
                    }

                    reflectedContracts.Add(contract);
                    Collection <ContractDescription> inheritedContracts = contract.GetInheritedContracts();
                    for (int j = 0; j < inheritedContracts.Count; j++)
                    {
                        ContractDescription inheritedContract = inheritedContracts[j];
                        if (!reflectedContracts.Contains(inheritedContract.ContractType))
                        {
                            reflectedContracts.Add(inheritedContract);
                        }
                    }
                }
            }
            ReflectedContracts = reflectedContracts;

            implementedContracts = reflectedContracts.ToImplementedContracts();
            return(description);
        }
Exemplo n.º 3
0
        protected override ServiceDescription CreateDescription(out IDictionary <string, ContractDescription> implementedContracts)
        {
            ServiceDescription description;

            if (SingletonInstance != null)
            {
                description = ServiceDescription.GetService(SingletonInstance);
            }
            else
            {
                description = ServiceDescription.GetService <TService>();
            }

            ServiceBehaviorAttribute serviceBehavior = description.Behaviors.Find <ServiceBehaviorAttribute>();
            TService serviceInstanceUsedAsABehavior  = (TService)serviceBehavior.GetWellKnownSingleton();

            if (serviceInstanceUsedAsABehavior == null)
            {
                serviceInstanceUsedAsABehavior = (TService)serviceBehavior.GetHiddenSingleton();
                _disposableInstance            = serviceInstanceUsedAsABehavior as IDisposable;
            }

            if ((typeof(IServiceBehavior).IsAssignableFrom(typeof(TService)) || typeof(IContractBehavior).IsAssignableFrom(typeof(TService))) &&
                serviceInstanceUsedAsABehavior == null)
            {
                serviceInstanceUsedAsABehavior = ServiceDescription.CreateImplementation <TService>();
                _disposableInstance            = serviceInstanceUsedAsABehavior as IDisposable;
            }

            if (SingletonInstance == null)
            {
                if (serviceInstanceUsedAsABehavior is IServiceBehavior)
                {
                    description.Behaviors.Add((IServiceBehavior)serviceInstanceUsedAsABehavior);
                }
            }

            ReflectedContractCollection reflectedContracts = new ReflectedContractCollection();
            List <Type> interfaces = ServiceReflector.GetInterfaces <TService>();

            for (int i = 0; i < interfaces.Count; i++)
            {
                Type contractType = interfaces[i];
                if (!reflectedContracts.Contains(contractType))
                {
                    ContractDescription contract = null;
                    if (serviceInstanceUsedAsABehavior != null)
                    {
                        contract = ContractDescription.GetContract(contractType, serviceInstanceUsedAsABehavior);
                    }
                    else
                    {
                        contract = ContractDescription.GetContract <TService>(contractType);
                    }

                    reflectedContracts.Add(contract);
                    Collection <ContractDescription> inheritedContracts = contract.GetInheritedContracts();
                    for (int j = 0; j < inheritedContracts.Count; j++)
                    {
                        ContractDescription inheritedContract = inheritedContracts[j];
                        if (!reflectedContracts.Contains(inheritedContract.ContractType))
                        {
                            reflectedContracts.Add(inheritedContract);
                        }
                    }
                }
            }
            ReflectedContracts = reflectedContracts;

            implementedContracts = reflectedContracts.ToImplementedContracts();
            return(description);
        }