예제 #1
0
        protected virtual ClientProxy <I> CreateInProcWrapper <I>(Type serviceType)
            where I : class
        {
            // We are using WCF, create host.
            // InProcHost.CreateHost will cache the host.
            var url = InProcessHost.CreateHost(serviceType, typeof(I));

            var binding = new NetNamedPipeBinding()
            {
                MaxBufferPoolSize      = int.MaxValue,
                MaxBufferSize          = int.MaxValue,
                MaxReceivedMessageSize = int.MaxValue,
            };

            MaxSetter.SetMaxes(binding);

            return(new ClientProxy <I>(
                       binding,
                       new EndpointAddress(url)));
        }
예제 #2
0
        public void ConfigureService(System.ServiceModel.Description.ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {
            var host = (serviceHostBase as ServiceHost);

            if (host != null)
            {
                // Don't use this crappy binding
                var address  = serviceHostBase.Description.Endpoints.First().Address;
                var contract = serviceHostBase.Description.Endpoints.First().Contract;

                // clear existing
                host.Description.Endpoints.Clear();

                var binding = new WS2007HttpBinding();
                binding.Security.Mode = SecurityMode.None;

                MaxSetter.SetMaxes(binding);

                host.AddServiceEndpoint(contract.ContractType, binding, address.Uri);
            }

            DisableErrorMasking.Disable(serviceHostBase);
        }
예제 #3
0
        /// <summary>
        /// Configure an endpoint from a setting
        /// </summary>
        protected virtual IChannelWrapper <I> ConfigureEndpointFromSetting <I>(ClientEndpoint endpoint)
            where I : class
        {
            IChannelWrapper <I> result = null;

            if (!string.IsNullOrWhiteSpace(endpoint.Implementation))
            {
                Type t = Type.GetType(endpoint.Implementation);
                if (t == null)
                {
                    throw new InvalidOperationException(
                              string.Format(
                                  "Unable to create service type, your configuration for endpoint.Implementation is probably wrong or you need to add a reference to an assembly. Configured as {0}.",
                                  endpoint.Implementation));
                }

                if (endpoint.UseWcf)
                {
                    result = CreateInProcWrapper <I>(t);
                }
                else
                {
                    result = new MockChannelWrapper <I>()
                    {
                        Instance = Activator.CreateInstance(t) as I
                    };
                }
            }
            else if (!string.IsNullOrWhiteSpace(endpoint.Address))
            {
                if (string.IsNullOrWhiteSpace(endpoint.ChannelFactory))
                {
                    IServiceHelperAttribute configureHelper = new DefaultServiceHelperAttribute();

                    Binding          binding  = null;
                    ClientProxy <I>  proxy    = null;
                    EndpointIdentity identity = null;

                    var attributes = typeof(I).GetCustomAttributes(false);
                    foreach (var attribute in attributes)
                    {
                        if (attribute is IServiceHelperAttribute)
                        {
                            configureHelper = attribute as IServiceHelperAttribute;
                            identity        = configureHelper.CreateIdentity();
                        }
                    }

                    if (endpoint.Address.StartsWith("net.tcp://"))
                    {
                        binding = new NetTcpBinding()
                        {
                            MaxBufferPoolSize      = int.MaxValue,
                            MaxBufferSize          = int.MaxValue,
                            MaxReceivedMessageSize = int.MaxValue,
                        };
                        MaxSetter.SetMaxes(binding);
                    }
                    else if (endpoint.Address.StartsWith("http://") || endpoint.Address.StartsWith("https://"))
                    {
                        binding = new WS2007HttpBinding(SecurityMode.None)
                        {
                            MaxBufferPoolSize      = int.MaxValue,
                            MaxReceivedMessageSize = int.MaxValue,
                        };
                        MaxSetter.SetMaxes(binding);
                        MaxSetter.SetTimeouts <I>(binding);
                    }
                    else if (endpoint.Address.StartsWith("net.msmq://"))
                    {
                        binding = new NetMsmqBinding()
                        {
                            MaxBufferPoolSize      = int.MaxValue,
                            MaxReceivedMessageSize = int.MaxValue,
                        };
                    }
                    else if (endpoint.Address.StartsWith("net.pipe://"))
                    {
                        binding = new NetNamedPipeBinding()
                        {
                            MaxBufferPoolSize      = int.MaxValue,
                            MaxReceivedMessageSize = int.MaxValue,
                        };
                    }
                    configureHelper.ConfigureClientBinding(binding, typeof(I));

                    proxy = new ClientProxy <I>(
                        binding,
                        new EndpointAddress(new Uri(endpoint.Address), configureHelper.CreateIdentity()));

                    configureHelper.ConfigureClientCredentials(proxy.ClientCredentials, typeof(I));

                    result = proxy;
                }
                else
                {
                    var cfType = Type.GetType(endpoint.ChannelFactory);
                    if (cfType == null)
                    {
                        throw new ConfigurationErrorsException("ChannelFactory is invalid");
                    }

                    var cf = Activator.CreateInstance(cfType) as ICustomChannelFactory;

                    if (cf == null)
                    {
                        throw new ConfigurationErrorsException("Could not create custom channel factory.");
                    }

                    result = cf.CreateChannel <I>(endpoint);
                }
            }

            if (result != null)
            {
                result.CacheCount = endpoint.CacheCount;
            }

            return(result);
        }
        public void ConfigureService(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            if (ConfigHelper.IsSecurityEnabled(serviceHostBase))
            {
                // Get certificate location from the cert store.
                StoreLocation location = StoreLocation.LocalMachine;
                CertHelper.TryGetCertLocation(ServiceCert, out location, true);

                // Set certificate

                serviceHostBase.Credentials.ServiceCertificate.SetCertificate(
                    location,
                    System.Security.Cryptography.X509Certificates.StoreName.My,
                    X509FindType.FindBySubjectName,
                    ServiceCert);

                // Set certificate validation mode (defaults to peer trust).
                serviceHostBase.Credentials.ClientCertificate.Authentication.CertificateValidationMode = ValidationMode;

                if (serviceHostBase.Description.Endpoints != null)
                {
                    bool reConfigure = false;
                    foreach (var endpoint in serviceHostBase.Description.Endpoints)
                    {
                        if (endpoint.Binding is WS2007HttpBinding)
                        {
                            // Setup each endpoint to use Message security.
                            var binding = endpoint.Binding as WS2007HttpBinding;
                            binding.Security.Mode = SecurityMode.Message;
                            binding.Security.Message.ClientCredentialType       = MessageCredentialType.Certificate;
                            binding.Security.Message.EstablishSecurityContext   = false;
                            binding.Security.Message.NegotiateServiceCredential = false;
                            MaxSetter.SetMaxes(binding);
                        }
                        if (endpoint.Binding is NetTcpBinding)
                        {
                            // Setup each endpoint to use Message security.
                            var binding = endpoint.Binding as NetTcpBinding;
                            binding.Security.Mode = SecurityMode.Message;
                            binding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
                            MaxSetter.SetMaxes(binding);
                        }
                        if (endpoint.Binding is BasicHttpBinding)
                        {
                            reConfigure = true;
                        }
                    }

                    // reconfigure host
                    if (reConfigure && serviceHostBase.Description.Endpoints.Count() > 0)
                    {
                        var host = (serviceHostBase as ServiceHost);
                        if (host != null)
                        {
                            // Don't use this crappy binding
                            var address  = serviceHostBase.Description.Endpoints.First().Address;
                            var contract = serviceHostBase.Description.Endpoints.First().Contract;

                            // clear existing
                            host.Description.Endpoints.Clear();

                            var binding = new WS2007HttpBinding();
                            binding.Security.Mode = SecurityMode.Message;
                            binding.Security.Message.ClientCredentialType     = MessageCredentialType.Certificate;
                            binding.Security.Message.EstablishSecurityContext = false;

                            MaxSetter.SetMaxes(binding);

                            var endpoint = host.AddServiceEndpoint(contract.ContractType, binding, address.Uri);
                        }
                    }

                    DisableErrorMasking.Disable(serviceHostBase);
                }
            }
        }