Exemple #1
0
        public static void Test_wcf_01()
        {
            Trace.WriteLine("Test_wcf_01");
            ServiceDescription serviceDescription = ServiceDescription.GetService(typeof(Service1));

            TraceServiceDescription(serviceDescription);
        }
Exemple #2
0
        protected override ServiceDescription CreateDescription(
            out IDictionary <string, ContractDescription> implementedContracts)
        {
            contracts            = new Dictionary <string, ContractDescription> ();
            implementedContracts = contracts;
            ServiceDescription sd;
            IEnumerable <ContractDescription> contractDescriptions = GetServiceContractDescriptions();

            foreach (ContractDescription cd in contractDescriptions)
            {
                contracts.Add(cd.ContractType.FullName, cd);
            }

            if (SingletonInstance != null)
            {
                sd = ServiceDescription.GetService(instance);
            }
            else
            {
                sd = ServiceDescription.GetService(service_type);
            }

            ServiceBehaviorAttribute sba = PopulateAttribute <ServiceBehaviorAttribute> ();

            if (SingletonInstance != null)
            {
                sba.SetWellKnownSingleton(SingletonInstance);
            }
            sd.Behaviors.Add(sba);

            return(sd);
        }
Exemple #3
0
            protected override ServiceDescription CreateDescription(out IDictionary <string, ContractDescription> implementedContracts)
            {
                var description = ServiceDescription.GetService(_serviceInstance);
                var cd          = ContractDescription.GetContract <SimpleService>(typeof(ISimpleService));

                implementedContracts = new Dictionary <string, ContractDescription>();
                implementedContracts[cd.ConfigurationName] = cd;
                return(description);
            }
Exemple #4
0
        public void Snippet10()
        {
            // <Snippet10>
            ServiceDescription d = ServiceDescription.GetService(new CalculatorService());

            foreach (IServiceBehavior isb in d.Behaviors)
            {
                Console.WriteLine(isb.GetType());
            }
            Console.WriteLine();
            // </Snippet10>
        }
Exemple #5
0
 public HostedWcfService(IServiceProvider serviceProvider)
 {
     if (ServiceDescription.GetService(typeof(TService)).IsInstanceContextModeSingle())
     {
         host = new ServiceHost(serviceProvider.GetRequiredService <TContract>());
     }
     else
     {
         host = new ServiceHost(typeof(TService));
         host.AddDependencyInjectionBehavior <TContract>(serviceProvider);
     }
 }
Exemple #6
0
        /// <summary>
        /// Creates a description of the service hosted.
        /// </summary>
        /// <param name="implementedContracts">
        /// The <see cref="IDictionary&lt;TKey,TValue&gt;"/> with key pairs of
        /// type (string, <see cref="ContractDescription"/>) that contains the
        /// keyed-contracts of the hosted service that have been implemented.
        /// </param>
        /// <returns>A <see cref="ServiceDescription"/> of the hosted service.</returns>
        protected override ServiceDescription CreateDescription(out IDictionary <string, ContractDescription> implementedContracts)
        {
            try
            {
                Type domainServiceType         = this._domainServiceDescription.DomainServiceType;
                ServiceDescription serviceDesc = ServiceDescription.GetService(domainServiceType);

                implementedContracts = new Dictionary <string, ContractDescription>();

                DomainServicesSection config = (DomainServicesSection)WebConfigurationManager.GetSection("system.serviceModel/domainServices");
                if (config == null)
                {
                    // Make sure we have a config instance, as that's where we put our default configuration. If we don't do this, our
                    // binary endpoint won't be used when someone doesn't have a <domainServices/> section in their web.config.
                    config = new DomainServicesSection();
                    config.InitializeDefaultInternal();
                }

                foreach (ProviderSettings provider in config.Endpoints)
                {
                    DomainServiceEndpointFactory endpointFactory = DomainServiceHost.CreateEndpointFactoryInstance(provider);
                    foreach (ServiceEndpoint endpoint in endpointFactory.CreateEndpoints(this._domainServiceDescription, this))
                    {
                        string contractName = endpoint.Contract.ConfigurationName;

                        ContractDescription contract;
                        if (implementedContracts.TryGetValue(contractName, out contract) && contract != endpoint.Contract)
                        {
                            throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.DomainServiceHost_DuplicateContractName, contract.ConfigurationName));
                        }

                        // Register the contract.
                        implementedContracts[endpoint.Contract.ConfigurationName] = endpoint.Contract;

                        // Register the endpoint.
                        serviceDesc.Endpoints.Add(endpoint);
                    }
                }

                return(serviceDesc);
            }
            catch (Exception ex)
            {
                DiagnosticUtility.ServiceException(ex);
                throw;
            }
        }
Exemple #7
0
        public AgsGeoDataService(AgsHost host, string service)
        {
            ServiceDescription serviceDescription = host.ServiceDescriptions.FirstOrDefault(sd => sd.Type == "GeoDataServer" && String.Compare(sd.Name, service, true) == 0);

            if (serviceDescription == null)
            {
                throw new AgsException(String.Format("The geodata service \"{0}\" does not exist on the ArcGIS Server", service));
            }

            Host = host;
            Name = serviceDescription.Name;

            _geoDataServer                 = serviceDescription.GetService() as GeoDataServer;
            _geoDataServer.Credentials     = host.Credentials;
            _geoDataServer.PreAuthenticate = _geoDataServer.Credentials != null;

            Reload();
        }
        /// <summary>
        /// Creates a contract from the specified description.
        /// </summary>
        /// <param name="description">The description to create a contract from.</param>
        /// <returns>A <see cref="ContractDescription"/>.</returns>
        private ContractDescription CreateContract(DomainServiceDescription description)
        {
            Type domainServiceType = description.DomainServiceType;

            // PERF: We should consider just looking at [ServiceDescription] directly.
            ServiceDescription serviceDesc = ServiceDescription.GetService(domainServiceType);

            // Use names from [ServiceContract], if specified.
            if (TypeDescriptor.GetAttributes(domainServiceType)[typeof(ServiceContractAttribute)]
                is ServiceContractAttribute sca)
            {
                if (!string.IsNullOrEmpty(sca.Name))
                {
                    serviceDesc.Name = sca.Name;
                }
                if (!string.IsNullOrEmpty(sca.Namespace))
                {
                    serviceDesc.Namespace = sca.Namespace;
                }
            }

            ContractDescription contractDesc = new ContractDescription(serviceDesc.Name, serviceDesc.Namespace)
            {
                ConfigurationName = serviceDesc.ConfigurationName,
                ContractType      = domainServiceType
            };

            // Add domain service behavior which takes care of instantiating DomainServices.
            ServiceUtility.EnsureBehavior <DomainServiceBehavior>(contractDesc);
            // Disable metadata generation by default
            contractDesc.Behaviors.Add(new ServiceMetadataContractBehavior()
            {
                MetadataGenerationDisabled = true
            });

            // Load the ContractDescription from the DomainServiceDescription.
            ServiceUtility.PopulateContractDescription(contractDesc, description);

            return(contractDesc);
        }
        /// <summary>
        /// Creates a description of the service hosted.
        /// </summary>
        /// <param name="implementedContracts">
        /// The <see cref="IDictionary&lt;TKey,TValue&gt;"/> with key pairs of
        /// type (string, <see cref="ContractDescription"/>) that contains the
        /// keyed-contracts of the hosted service that have been implemented.
        /// </param>
        /// <returns>A <see cref="ServiceDescription"/> of the hosted service.</returns>
        protected override ServiceDescription CreateDescription(out IDictionary <string, ContractDescription> implementedContracts)
        {
            try
            {
                Type domainServiceType         = this._domainServiceDescription.DomainServiceType;
                ServiceDescription serviceDesc = ServiceDescription.GetService(domainServiceType);

                implementedContracts = new Dictionary <string, ContractDescription>();

                DomainServicesSection config = DomainServicesSection.Current;
                foreach (ProviderSettings provider in config.Endpoints)
                {
                    DomainServiceEndpointFactory endpointFactory = DomainServiceHost.CreateEndpointFactoryInstance(provider);
                    foreach (ServiceEndpoint endpoint in endpointFactory.CreateEndpoints(this._domainServiceDescription, this))
                    {
                        string contractName = endpoint.Contract.ConfigurationName;

                        ContractDescription contract;
                        if (implementedContracts.TryGetValue(contractName, out contract) && contract != endpoint.Contract)
                        {
                            throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.DomainServiceHost_DuplicateContractName, contract.ConfigurationName));
                        }

                        // Register the contract.
                        implementedContracts[endpoint.Contract.ConfigurationName] = endpoint.Contract;

                        // Register the endpoint.
                        serviceDesc.Endpoints.Add(endpoint);
                    }
                }

                return(serviceDesc);
            }
            catch (Exception ex)
            {
                DiagnosticUtility.ServiceException(ex);
                throw;
            }
        }
        /// <summary>
        /// Creates a description of the service hosted.
        /// </summary>
        /// <param name="implementedContracts">
        /// The <see cref="IDictionary&lt;TKey,TValue&gt;"/> with key pairs of
        /// type (string, <see cref="ContractDescription"/>) that contains the
        /// keyed-contracts of the hosted service that have been implemented.
        /// </param>
        /// <returns>A <see cref="ServiceDescription"/> of the hosted service.</returns>
        protected override ServiceDescription CreateDescription(out IDictionary <string, ContractDescription> implementedContracts)
        {
            try
            {
                Type domainServiceType         = this._domainServiceDescription.DomainServiceType;
                ServiceDescription serviceDesc = ServiceDescription.GetService(domainServiceType);
                implementedContracts = new Dictionary <string, ContractDescription>();

                var config   = DomainServieHostingConfiguration.Current;
                var contract = CreateContract(this._domainServiceDescription);

                foreach (DomainServiceEndpointFactory endpointFactory in config.EndpointFactories)
                {
                    foreach (ServiceEndpoint endpoint in endpointFactory.CreateEndpoints(this._domainServiceDescription, this, contract))
                    {
                        string contractName = endpoint.Contract.ConfigurationName;

                        if (implementedContracts.TryGetValue(contractName, out var oldContract) && !object.ReferenceEquals(oldContract, endpoint.Contract))
                        {
                            throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.DomainServiceHost_DuplicateContractName, contract.ConfigurationName));
                        }

                        // Register the contract.
                        implementedContracts[contractName] = endpoint.Contract;

                        // Register the endpoint.
                        serviceDesc.Endpoints.Add(endpoint);
                    }
                }

                return(serviceDesc);
            }
            catch (Exception ex)
            {
                DiagnosticUtility.ServiceException(ex, OperationContext.Current);
                throw;
            }
        }
Exemple #11
0
        /// <summary>
        /// Creates a contract description for the domain data service endpoint based on the domain service description.
        /// </summary>
        /// <param name="description">Domain data service description.</param>
        /// <returns>Contract description for the domain data service endpoint.</returns>
        private ContractDescription CreateContract(DomainServiceDescription description)
        {
            Type domainServiceType = description.DomainServiceType;

            ServiceDescription serviceDesc = ServiceDescription.GetService(domainServiceType);

            // Use names from [ServiceContract], if specified.
            ServiceContractAttribute sca = TypeDescriptor.GetAttributes(domainServiceType)[typeof(ServiceContractAttribute)] as ServiceContractAttribute;

            if (sca != null)
            {
                if (!String.IsNullOrEmpty(sca.Name))
                {
                    serviceDesc.Name = sca.Name;
                }
                if (!String.IsNullOrEmpty(sca.Namespace))
                {
                    serviceDesc.Name = sca.Namespace;
                }
            }

            ContractDescription contractDesc = new ContractDescription(serviceDesc.Name + this.Name, serviceDesc.Namespace)
            {
                ConfigurationName = serviceDesc.ConfigurationName + this.Name,
                ContractType      = domainServiceType
            };

            // Disable metadata generation for the domain data service contract.
            contractDesc.Behaviors.Add(new ServiceMetadataContractBehavior(true));

            // Add domain service behavior which takes care of instantiating DomainServices.
            ServiceUtils.EnsureBehavior <DomainDataServiceContractBehavior>(contractDesc);

            // Load the ContractDescription from the DomainServiceDescription.
            this.LoadContractDescription(contractDesc, description);

            return(contractDesc);
        }
Exemple #12
0
        /// <summary>
        /// Creates a contract from the specified description.
        /// </summary>
        /// <param name="description">The description to create a contract from.</param>
        /// <returns>A <see cref="ContractDescription"/>.</returns>
        private ContractDescription CreateContract(DomainServiceDescription description)
        {
            Type domainServiceType = description.DomainServiceType;

            // PERF: We should consider just looking at [ServiceDescription] directly.
            ServiceDescription serviceDesc = ServiceDescription.GetService(domainServiceType);

            // Use names from [ServiceContract], if specified.
            ServiceContractAttribute sca = TypeDescriptor.GetAttributes(domainServiceType)[typeof(ServiceContractAttribute)] as ServiceContractAttribute;

            if (sca != null)
            {
                if (!String.IsNullOrEmpty(sca.Name))
                {
                    serviceDesc.Name = sca.Name;
                }
                if (!String.IsNullOrEmpty(sca.Namespace))
                {
                    serviceDesc.Name = sca.Namespace;
                }
            }

            ContractDescription contractDesc = new ContractDescription(serviceDesc.Name + this.Name, serviceDesc.Namespace)
            {
                ConfigurationName = serviceDesc.ConfigurationName + this.Name,
                ContractType      = domainServiceType
            };

            // Add domain service behavior which takes care of instantiating DomainServices.
            ServiceUtility.EnsureBehavior <DomainServiceBehavior>(contractDesc);

            // Load the ContractDescription from the DomainServiceDescription.
            ServiceUtility.LoadContractDescription(contractDesc, description);

            return(contractDesc);
        }
        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);
        }
        /// <summary>
        /// Gets the service description.
        /// </summary>
        /// <param name="serviceType">The service type.</param>
        /// <returns>The service description</returns>
        public static ServiceDescription GetServiceDescription(Type serviceType)
        {
            ServiceDescription serviceDescription = ServiceDescription.GetService(serviceType);

            return(serviceDescription);
        }
Exemple #15
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>
            // Use Default constructor
            ServiceDescription sd = new ServiceDescription();
            // </Snippet1>

            // <Snippet2>
            // Create ServiceDescription from a collection of service endpoints
            List <ServiceEndpoint> endpoints       = new List <ServiceEndpoint>();
            ContractDescription    conDescr        = new ContractDescription("ICalculator");
            EndpointAddress        endpointAddress = new EndpointAddress("http://localhost:8001/Basic");
            ServiceEndpoint        ep = new ServiceEndpoint(conDescr, new BasicHttpBinding(), endpointAddress);

            endpoints.Add(ep);
            ServiceDescription sd2 = new ServiceDescription(endpoints);
            // </Snippet2>

            //// <Snippet3>
            //// Iterate through the list of behaviors in the ServiceDescription
            ServiceDescription svcDesc = serviceHost.Description;
            KeyedByTypeCollection <IServiceBehavior> sbCol = svcDesc.Behaviors;

            foreach (IServiceBehavior behavior in sbCol)
            {
                Console.WriteLine("Behavior: {0}", behavior.ToString());
            }
            //</Snippet3>

            // <Snippet4>
            // svcDesc is a ServiceDescription.
            svcDesc = serviceHost.Description;
            string configName = svcDesc.ConfigurationName;

            Console.WriteLine("Configuration name: {0}", configName);
            // </Snippet4>

            // <Snippet5>
            // Iterate through the endpoints contained in the ServiceDescription
            ServiceEndpointCollection sec = svcDesc.Endpoints;

            foreach (ServiceEndpoint se in sec)
            {
                Console.WriteLine("Endpoint:");
                Console.WriteLine("\tAddress: {0}", se.Address.ToString());
                Console.WriteLine("\tBinding: {0}", se.Binding.ToString());
                Console.WriteLine("\tContract: {0}", se.Contract.ToString());
                KeyedByTypeCollection <IEndpointBehavior> behaviors = se.Behaviors;
                foreach (IEndpointBehavior behavior in behaviors)
                {
                    Console.WriteLine("Behavior: {0}", behavior.ToString());
                }
            }
            // </Snippet5>

            // <Snippet6>
            string name = svcDesc.Name;

            Console.WriteLine("Service Description name: {0}", name);
            // </Snippet6>

            // <Snippet7>
            string namespc = svcDesc.Namespace;

            Console.WriteLine("Service Description namespace: {0}", namespc);
            // </Snippet7>

            // <Snippet8>
            Type serviceType = svcDesc.ServiceType;

            Console.WriteLine("Service Type: {0}", serviceType.ToString());
            // </Snippet8>

            // <Snippet9>
            // Instantiate a service description specifying a service object
            // Note: Endpoints collection and other properties will be null since
            // we have not specified them
            CalculatorService  svcObj      = new CalculatorService();
            ServiceDescription sd3         = ServiceDescription.GetService(svcObj);
            String             serviceName = sd3.Name;

            Console.WriteLine("Service name: {0}", serviceName);
            // </Snippet9>

            // </Snippet0>
        }
        /// <summary>
        /// Gets the service description.
        /// </summary>
        /// <param name="serviceImplementation">The service implementation instance.</param>
        /// <returns>The service description</returns>
        public static ServiceDescription GetServiceDescription(object serviceImplementation)
        {
            ServiceDescription serviceDescription = ServiceDescription.GetService(serviceImplementation);

            return(serviceDescription);
        }
        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);
        }