Пример #1
0
        public static ServiceHost CreateHost <T>(bool asynchronousSendEnabled, bool customBinding, bool faultDetail, bool streamed, HttpMessageHandlerFactory httpMessageHandlerFactory) where T : TestServiceBase
        {
            var webHost = new WebServiceHost(typeof(T), TestServiceCommon.ServiceAddress);

            if (faultDetail && webHost.Description.Behaviors.Contains(typeof(ServiceDebugBehavior)))
            {
                var debug = webHost.Description.Behaviors[typeof(ServiceDebugBehavior)] as ServiceDebugBehavior;
                debug.IncludeExceptionDetailInFaults = true;
            }

            Binding httpBinding = null;

            if (customBinding)
            {
                var bindingElement = new HttpMessageHandlerBindingElement();
                bindingElement.MessageHandlerFactory = httpMessageHandlerFactory;

                var httpMsgBinding = new HttpBinding();
                if (streamed)
                {
                    httpMsgBinding.TransferMode = TransferMode.Streamed;
                }

                var bindingElements = httpMsgBinding.CreateBindingElements();
                bindingElements.Insert(0, bindingElement);

                httpBinding = new CustomBinding(bindingElements);
            }
            else
            {
                var httpMsgBinding = new HttpBinding()
                {
                    MessageHandlerFactory = httpMessageHandlerFactory
                };

                if (streamed)
                {
                    httpMsgBinding.TransferMode = TransferMode.Streamed;
                }

                httpBinding = httpMsgBinding;
            }

            var endpoint = webHost.AddServiceEndpoint(typeof(ITestServiceContract), httpBinding, "");

            ServiceDebugBehavior debugBehavior = webHost.Description.Behaviors.Find <ServiceDebugBehavior>();
            DispatcherSynchronizationBehavior synchronizationBehavior = new DispatcherSynchronizationBehavior()
            {
                AsynchronousSendEnabled = asynchronousSendEnabled
            };

            endpoint.Behaviors.Add(synchronizationBehavior);
            endpoint.Behaviors.Add(new TestHttpBindingParameterBehavior(debugBehavior, synchronizationBehavior));

            webHost.Open();
            return(webHost);
        }
Пример #2
0
        /// <summary>
        /// Invoked during the transition of a communication object into the opening state.
        /// </summary>
        protected override void OnOpening()
        {
            if (this.Description == null)
            {
                return;
            }

            DisableServiceDebugAndMetadataBehaviors(this.Description);
            ServiceDebugBehavior debugBehavior = this.Description.Behaviors.Find <ServiceDebugBehavior>();

            foreach (ServiceEndpoint serviceEndpoint in this.Description.Endpoints)
            {
                if (serviceEndpoint.Binding != null)
                {
                    if (serviceEndpoint.Binding.CreateBindingElements().Find <HttpMessageHandlerBindingElement>() != null)
                    {
                        DispatcherSynchronizationBehavior synchronizationBehavior = serviceEndpoint.Behaviors.Find <DispatcherSynchronizationBehavior>();
                        if (synchronizationBehavior == null)
                        {
                            synchronizationBehavior = new DispatcherSynchronizationBehavior()
                            {
                                AsynchronousSendEnabled = true
                            };
                            serviceEndpoint.Behaviors.Add(synchronizationBehavior);
                        }

                        if (serviceEndpoint.Behaviors.Find <HttpBindingParameterBehavior>() == null)
                        {
                            serviceEndpoint.Behaviors.Add(new HttpBindingParameterBehavior(debugBehavior, synchronizationBehavior));
                        }
                    }

                    if (serviceEndpoint.Binding.CreateBindingElements().Find <HttpMessageEncodingBindingElement>() != null)
                    {
                        if (serviceEndpoint.Behaviors.Find <HttpBehavior>() == null)
                        {
                            if (serviceEndpoint.Behaviors.Find <HttpBehavior>() == null)
                            {
                                serviceEndpoint.Behaviors.Add(new HttpBehavior());
                            }
                        }
                    }
                }
            }

            base.OnOpening();
        }
        public override IChannelListener <TChannel> BuildChannelListener <TChannel>(BindingContext context)
        {
            if (context == null)
            {
                throw Fx.Exception.ArgumentNull("context");
            }

            if (!this.CanBuildChannelListener <TChannel>(context))
            {
                throw Fx.Exception.AsError(
                          new NotSupportedException(
                              SR.HttpMessageHandlerChannelInvalidChannelShape(typeof(TChannel).Name, typeof(HttpMessageHandlerBindingElement).Name, typeof(IReplyChannel).Name)));
            }

            // Check whether we should use asynchronous reply path and whether we should include exceptions details in error responses
            bool asynchronousSendEnabled        = false;
            bool includeExceptionDetailInFaults = false;

            if (context.BindingParameters != null)
            {
                DispatcherSynchronizationBehavior dispatcherSynchronizationBehavior = context.BindingParameters.Find <DispatcherSynchronizationBehavior>();
                if (dispatcherSynchronizationBehavior != null)
                {
                    asynchronousSendEnabled = dispatcherSynchronizationBehavior.AsynchronousSendEnabled;
                }

                ServiceDebugBehavior serviceDebugBehavior = context.BindingParameters.Find <ServiceDebugBehavior>();
                if (serviceDebugBehavior != null)
                {
                    includeExceptionDetailInFaults = serviceDebugBehavior.IncludeExceptionDetailInFaults;
                }
            }

            IChannelListener <IReplyChannel> innerListener = context.BuildInnerChannelListener <IReplyChannel>();

            if (innerListener == null)
            {
                return(null);
            }

            return((IChannelListener <TChannel>) new HttpMessageHandlerChannelListener(context.Binding, innerListener, this.MessageHandlerFactory, asynchronousSendEnabled, includeExceptionDetailInFaults));
        }
Пример #4
0
        /// <summary>
        /// Adds the binding parameters.
        /// </summary>
        /// <param name="serviceEndpoint">The service endpoint.</param>
        /// <param name="parameters">The parameters.</param>
        void IEndpointBehavior.AddBindingParameters(ServiceEndpoint serviceEndpoint, BindingParameterCollection parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            ServiceDebugBehavior debugBehavior = parameters.Find <ServiceDebugBehavior>();

            if (debugBehavior == null && this.serviceDebugBehavior != null)
            {
                parameters.Add(this.serviceDebugBehavior);
            }

            DispatcherSynchronizationBehavior synchronizationBehavior = parameters.Find <DispatcherSynchronizationBehavior>();

            if (synchronizationBehavior == null && this.dispatcherSynchronizationBehavior != null)
            {
                parameters.Add(this.dispatcherSynchronizationBehavior);
            }
        }
Пример #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HttpBindingParameterBehavior"/> class.
 /// </summary>
 /// <param name="serviceDebugBehavior">Optional service debug behavior (can be null).</param>
 /// <param name="dispatcherSynchronizationBehavior">Optional dispatcher synchronization behavior (can be null).</param>
 public TestHttpBindingParameterBehavior(ServiceDebugBehavior serviceDebugBehavior, DispatcherSynchronizationBehavior dispatcherSynchronizationBehavior)
 {
     this.serviceDebugBehavior = serviceDebugBehavior;
     this.dispatcherSynchronizationBehavior = dispatcherSynchronizationBehavior;
 }