Exemple #1
0
        public static void Main()
        {
            // <Snippet0>
            Uri         baseAddress = new Uri("http://localhost:8001/Simple");
            ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress);

            serviceHost.AddServiceEndpoint(
                typeof(ICalculator),
                new WSHttpBinding(),
                "CalculatorServiceObject");

            // Enable Mex
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();

            smb.HttpGetEnabled = true;
            serviceHost.Description.Behaviors.Add(smb);

            serviceHost.Open();

            // <Snippet1>
            ContractDescription cd0 = new ContractDescription("ICalculator");
            // </Snippet1>
            // <Snippet2>
            ContractDescription cd1 = new ContractDescription("ICalculator", "http://www.tempuri.org");
            // </Snippet2>
            // <Snippet13>
            ContractDescription cd2 = ContractDescription.GetContract(typeof(ICalculator));
            // </Snippet13>
            // <Snippet14>
            CalculatorService   calcSvc = new CalculatorService();
            ContractDescription cd3     = ContractDescription.GetContract(typeof(ICalculator), calcSvc);
            // </Snippet14>
            // <Snippet15>
            ContractDescription cd4 = ContractDescription.GetContract(typeof(ICalculator), typeof(CalculatorService));
            // </Snippet15>
            ContractDescription cd = serviceHost.Description.Endpoints[0].Contract;

            Console.WriteLine("Displaying information for contract: {0}", cd.Name.ToString());

            // <Snippet3>
            KeyedByTypeCollection <IContractBehavior> behaviors = cd.Behaviors;

            Console.WriteLine("\tDisplay all behaviors:");
            foreach (IContractBehavior behavior in behaviors)
            {
                Console.WriteLine("\t\t" + behavior.ToString());
            }
            // </Snippet3>

            // <Snippet4>
            Type type = cd.CallbackContractType;
            // </Snippet4>

            // <Snippet5>
            string configName = cd.ConfigurationName;

            Console.WriteLine("\tConfiguration name: {0}", configName);
            // </Snippet5>

            // <Snippet6>
            Type contractType = cd.ContractType;

            Console.WriteLine("\tContract type: {0}", contractType.ToString());
            // </Snippet6>

            // <Snippet7>
            bool hasProtectionLevel = cd.HasProtectionLevel;

            if (hasProtectionLevel)
            {
                // <Snippet11>
                ProtectionLevel protectionLevel = cd.ProtectionLevel;
                Console.WriteLine("\tProtection Level: {0}", protectionLevel.ToString());
                // </Snippet11>
            }
            // </Snippet7>


            // <Snippet8>
            string name = cd.Name;

            Console.WriteLine("\tName: {0}", name);
            // </Snippet8>

            // <Snippet9>
            string namespc = cd.Namespace;

            Console.WriteLine("\tNamespace: {0}", namespc);
            // </Snippet9>

            // <Snippet10>
            OperationDescriptionCollection odc = cd.Operations;

            Console.WriteLine("\tDisplay Operations:");
            foreach (OperationDescription od in odc)
            {
                Console.WriteLine("\t\t" + od.Name);
            }
            // </Snippet10>

            // <Snippet12>
            SessionMode sm = cd.SessionMode;

            Console.WriteLine("\tSessionMode: {0}", sm.ToString());
            // </Snippet12>

            // <Snippet16>
            Collection <ContractDescription> inheretedContracts = cd.GetInheritedContracts();

            Console.WriteLine("\tInherited Contracts:");
            foreach (ContractDescription contractdescription in inheretedContracts)
            {
                Console.WriteLine("\t\t" + contractdescription.Name);
            }
            // </Snippet16>

            Console.WriteLine("The service is ready.");
            Console.WriteLine("Press <ENTER> to terminate service.");
            Console.WriteLine();
            Console.ReadLine();

            // Close the ServiceHostBase to shutdown the service.
            serviceHost.Close();
            // </Snippet0>
        }
        protected override System.ServiceModel.Description.ServiceDescription CreateDescription(out IDictionary <string, ContractDescription> implementedContracts)
        {
            System.ServiceModel.Description.ServiceDescription service;
            if (this.serviceType == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("SFxServiceHostCannotCreateDescriptionWithoutServiceType")));
            }
            if (this.SingletonInstance != null)
            {
                service = System.ServiceModel.Description.ServiceDescription.GetService(this.SingletonInstance);
            }
            else
            {
                service = System.ServiceModel.Description.ServiceDescription.GetService(this.serviceType);
            }
            ServiceBehaviorAttribute attribute = service.Behaviors.Find <ServiceBehaviorAttribute>();
            object wellKnownSingleton          = attribute.GetWellKnownSingleton();

            if (wellKnownSingleton == null)
            {
                wellKnownSingleton      = attribute.GetHiddenSingleton();
                this.disposableInstance = wellKnownSingleton as IDisposable;
            }
            if ((typeof(IServiceBehavior).IsAssignableFrom(this.serviceType) || typeof(IContractBehavior).IsAssignableFrom(this.serviceType)) && (wellKnownSingleton == null))
            {
                wellKnownSingleton      = System.ServiceModel.Description.ServiceDescription.CreateImplementation(this.serviceType);
                this.disposableInstance = wellKnownSingleton as IDisposable;
            }
            if ((this.SingletonInstance == null) && (wellKnownSingleton is IServiceBehavior))
            {
                service.Behaviors.Add((IServiceBehavior)wellKnownSingleton);
            }
            ReflectedContractCollection contracts  = new ReflectedContractCollection();
            List <System.Type>          interfaces = ServiceReflector.GetInterfaces(this.serviceType);

            for (int i = 0; i < interfaces.Count; i++)
            {
                System.Type key = interfaces[i];
                if (!contracts.Contains(key))
                {
                    ContractDescription item = null;
                    if (wellKnownSingleton != null)
                    {
                        item = ContractDescription.GetContract(key, wellKnownSingleton);
                    }
                    else
                    {
                        item = ContractDescription.GetContract(key, this.serviceType);
                    }
                    contracts.Add(item);
                    Collection <ContractDescription> inheritedContracts = item.GetInheritedContracts();
                    for (int j = 0; j < inheritedContracts.Count; j++)
                    {
                        ContractDescription description3 = inheritedContracts[j];
                        if (!contracts.Contains(description3.ContractType))
                        {
                            contracts.Add(description3);
                        }
                    }
                }
            }
            this.reflectedContracts = contracts;
            implementedContracts    = contracts.ToImplementedContracts();
            return(service);
        }
        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);
        }
        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);
        }