예제 #1
0
        private static Binding FindBindingForContractType(Type contractType)
        {
            IBackendServiceLocation serviceLocation = _serviceLocationCache.FirstOrDefault(item => item.ContractType == contractType);

            if (serviceLocation == null)
            {
                throw new InvalidOperationException(string.Format(Properties.Resources.NoAttributeForServiceContractTypeFound, contractType));
            }

            Binding binding = null;

            switch (serviceLocation.Binding)
            {
            case SupportedBinding.NetTcp:
                binding = new NetTcpBinding()
                {
                    MaxReceivedMessageSize = int.MaxValue,
                    ReaderQuotas           = XmlDictionaryReaderQuotas.Max,
                };
                break;

            default:
                throw new InvalidOperationException(string.Format(Properties.Resources.InvalidSupportedBindingValue, serviceLocation.Binding));
            }

            return(binding);
        }
        private void HostExposedService(IBackendServiceLocation serviceLocation)
        {
            // Configure the ServiceHost to be a Per-Session service.
            ServiceHost host = new ServiceHost(serviceLocation.ServiceType);

            Binding         binding  = ServiceBindingCache.GetBindingForContractType(serviceLocation.ContractType);
            EndpointAddress address  = ServiceFactory.GetEndpointAddress(serviceLocation.ContractType, binding);
            ServiceEndpoint endpoint = host.AddServiceEndpoint(serviceLocation.ContractType, binding, address.Uri);

            _hostedExposedServices.Add(host);

            host.Open();
        }
        private void HostInternalService(IBackendServiceLocation serviceLocation)
        {
            object           serviceInstanceRaw = Activator.CreateInstance(serviceLocation.ServiceType);
            IInternalService serviceInstance    = serviceInstanceRaw as IInternalService;

            if (serviceInstance == null)
            {
                Logger.Instance.LogFormat(LogType.Error, this, Properties.Resources.SvcMgrServiceInstanceIllegalType, typeof(IInternalService));
                return;
            }

            serviceInstance.Initialize(this);

            _hostedInternalServices.Add(serviceInstance);
            ServiceInternalProvider.Instance.AddService(serviceLocation.ContractType, serviceInstance);
        }
 private void HostService(IBackendServiceLocation serviceLocation)
 {
     // Decide which kind of service to host.
     Type[] interfaces = serviceLocation.ContractType.GetInterfaces();
     if (interfaces.Any(f => f.Equals(typeof(IInternalService))))
     {
         HostInternalService(serviceLocation);
     }
     else if (interfaces.Any(f => f.Equals(typeof(IExposedService))))
     {
         HostExposedService(serviceLocation);
     }
     else
     {
         // TODO: Details!
         throw new InvalidOperationException("Invalid service detected!");
     }
 }
예제 #5
0
        private static Binding FindBindingForContractType(Type contractType)
        {
            IBackendServiceLocation serviceLocation = _serviceLocationCache.FirstOrDefault(item => item.ContractType == contractType);

            if (serviceLocation == null)
            {
                throw new InvalidOperationException(string.Format(Properties.Resources.NoAttributeForServiceContractTypeFound, contractType));
            }

            Binding binding = null;

            switch (serviceLocation.Binding)
            {
            case SupportedBinding.NetTcp:
                binding = new NetTcpBinding()
                {
                    MaxReceivedMessageSize = int.MaxValue,
                    ReaderQuotas           = XmlDictionaryReaderQuotas.Max,
                };

                if (File.Exists(ServiceFactory.BackendConfigurator.Get(ConfigKeyCertificate)))
                {
                    (binding as NetTcpBinding).Security = new NetTcpSecurity()
                    {
                        Message = new MessageSecurityOverTcp()
                        {
                            ClientCredentialType = MessageCredentialType.Certificate
                        },
                        Transport = new TcpTransportSecurity()
                        {
                            ClientCredentialType = TcpClientCredentialType.Certificate
                        }
                    };
                }
                break;

            default:
                throw new InvalidOperationException(string.Format(Properties.Resources.InvalidSupportedBindingValue, serviceLocation.Binding));
            }

            return(binding);
        }
        private void HostExposedService(IBackendServiceLocation serviceLocation)
        {
            string path     = ServiceFactory.BackendConfigurator.Get("Certificate");
            string password = ServiceFactory.BackendConfigurator.Get("Certificate.Password");
            // Configure the ServiceHost to be a Per-Session service.
            ServiceHost host = new ServiceHost(serviceLocation.ServiceType);

            if (File.Exists(path))
            {
                X509Certificate2 certificate = new X509Certificate2(path, password);
                host.Credentials.ServiceCertificate.Certificate = certificate;
                host.Credentials.ClientCertificate.Authentication.CertificateValidationMode  = X509CertificateValidationMode.Custom;
                host.Credentials.ClientCertificate.Authentication.CustomCertificateValidator = new CertificateValidator(certificate.Thumbprint);
            }

            Binding         binding  = ServiceBindingCache.GetBindingForContractType(serviceLocation.ContractType);
            EndpointAddress address  = ServiceFactory.GetEndpointAddress(serviceLocation.ContractType, binding);
            ServiceEndpoint endpoint = host.AddServiceEndpoint(serviceLocation.ContractType, binding, address.Uri);

            _hostedExposedServices.Add(host);

            host.Open();
        }
 private void HostService(IBackendServiceLocation serviceLocation)
 {
     // Decide which kind of service to host.
     Type[] interfaces = serviceLocation.ContractType.GetInterfaces();
     if (interfaces.Any(f => f.Equals(typeof(IInternalService))))
     {
         HostInternalService(serviceLocation);
     }
     else if (interfaces.Any(f => f.Equals(typeof(IExposedService))))
     {
         HostExposedService(serviceLocation);
     }
     else
     {
         // TODO: Details!
         throw new InvalidOperationException("Invalid service detected!");
     }
 }
        private void HostInternalService(IBackendServiceLocation serviceLocation)
        {
            object serviceInstanceRaw = Activator.CreateInstance(serviceLocation.ServiceType);
            IInternalService serviceInstance = serviceInstanceRaw as IInternalService;
            if (serviceInstance == null)
            {
                Logger.Instance.LogFormat(LogType.Error, this, Properties.Resources.SvcMgrServiceInstanceIllegalType, typeof(IInternalService));
                return;
            }

            serviceInstance.Initialize(this);

            _hostedInternalServices.Add(serviceInstance);
            ServiceInternalProvider.Instance.AddService(serviceLocation.ContractType, serviceInstance);
        }
        private void HostExposedService(IBackendServiceLocation serviceLocation)
        {
            string path = ServiceFactory.BackendConfigurator.Get("Certificate");
            string password = ServiceFactory.BackendConfigurator.Get("Certificate.Password");
            // Configure the ServiceHost to be a Per-Session service.
            ServiceHost host = new ServiceHost(serviceLocation.ServiceType);

            if (File.Exists(path))
            {
                X509Certificate2 certificate = new X509Certificate2(path, password);
                host.Credentials.ServiceCertificate.Certificate = certificate;
                host.Credentials.ClientCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.Custom;
                host.Credentials.ClientCertificate.Authentication.CustomCertificateValidator = new CertificateValidator(certificate.Thumbprint);
            }

            Binding binding = ServiceBindingCache.GetBindingForContractType(serviceLocation.ContractType);
            EndpointAddress address = ServiceFactory.GetEndpointAddress(serviceLocation.ContractType, binding);
            ServiceEndpoint endpoint = host.AddServiceEndpoint(serviceLocation.ContractType, binding, address.Uri);

            _hostedExposedServices.Add(host);

            host.Open();
        }
        private void HostExposedService(IBackendServiceLocation serviceLocation)
        {
            // Configure the ServiceHost to be a Per-Session service.
            ServiceHost host = new ServiceHost(serviceLocation.ServiceType);

            Binding binding = ServiceBindingCache.GetBindingForContractType(serviceLocation.ContractType);
            EndpointAddress address = ServiceFactory.GetEndpointAddress(serviceLocation.ContractType, binding);
            ServiceEndpoint endpoint = host.AddServiceEndpoint(serviceLocation.ContractType, binding, address.Uri);

            _hostedExposedServices.Add(host);

            host.Open();
        }