static void Main(string[] args)
 {
     var config = new AzureServiceBusConfiguration();
     config.Routes.MapHttpRoute("default", "{controller}/{message}", new { id = RouteParameter.Optional });
     config.MessageHandlers.Add(new LoggingActivityHandler());
     config.Formatters.Insert(0, new CustomHtmlFormatter());
     //config.Formatters.Insert(0, new CustomFormatter());
     var host = new AzureServiceBusHttpServer(config);
     host.OpenAsync().Wait();
     System.Console.WriteLine("Server is opened at '{0}'", config.BaseAddress);
     System.Console.ReadKey();
     host.CloseAsync();
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ChannelContext"/> class.
 /// </summary>
 /// <param name="server">The host to associate with this context.</param>
 /// <param name="channel">The channel to associate with this channel.</param>
 public ChannelContext(AzureServiceBusHttpServer server, IReplyChannel channel)
 {
     Contract.Assert(server != null);
     Contract.Assert(channel != null);
     Server = server;
     Channel = channel;
 }
        private void BeginOpenListener(AzureServiceBusHttpServer server)
        {
            Contract.Assert(server != null);

            try
            {
                // Create WCF HTTP transport channel
                var binding = new WebHttpRelayBinding(EndToEndWebHttpSecurityMode.None, RelayClientAuthenticationType.None);

                // Get it configured
                BindingParameterCollection bindingParameters = server._configuration.ConfigureBinding(binding);
                if (bindingParameters == null)
                {
                    bindingParameters = new BindingParameterCollection();
                }

                // Build channel listener
                server._listener = binding.BuildChannelListener<IReplyChannel>(server._configuration.BaseAddress, bindingParameters);

                if (server._listener == null)
                {
                    throw new InvalidOperationException(string.Format("Error creating '{0}' instance using '{1}'.", typeof(IChannelListener).Name, typeof(IReplyChannel).Name));
                }

                IAsyncResult result = server._listener.BeginOpen(_onOpenListenerComplete, server);

                if (result.CompletedSynchronously)
                {
                    OpenListenerComplete(result);
                }
            }
            catch (Exception e)
            {
                FaultTask(server._openTaskCompletionSource, e);
            }
        }
        private void BeginAcceptChannel(AzureServiceBusHttpServer server)
        {
            Contract.Assert(server != null);
            Contract.Assert(server._listener != null);

            IAsyncResult result = BeginTryAcceptChannel(server, _onAcceptChannelComplete);
            if (result.CompletedSynchronously)
            {
                AcceptChannelComplete(result);
            }
        }
        private static void CloseNextChannel(AzureServiceBusHttpServer server)
        {
            Contract.Assert(server != null);

            IReplyChannel channel;
            if (server._channels.TryTake(out channel))
            {
                BeginCloseChannel(new ChannelContext(server, channel));
            }
            else
            {
                CompleteTask(server._closeTaskCompletionSource);
            }
        }
        private static IAsyncResult BeginTryAcceptChannel(AzureServiceBusHttpServer server, AsyncCallback callback)
        {
            Contract.Assert(server != null);
            Contract.Assert(server._listener != null);
            Contract.Assert(callback != null);

            try
            {
                return server._listener.BeginAcceptChannel(_acceptTimeout, callback, server);
            }
            catch (CommunicationObjectAbortedException)
            {
                return new CompletedAsyncResult<bool>(true, callback, server);
            }
            catch (CommunicationObjectFaultedException)
            {
                return new CompletedAsyncResult<bool>(true, callback, server);
            }
            catch (TimeoutException)
            {
                return new CompletedAsyncResult<bool>(false, callback, server);
            }
            catch (CommunicationException)
            {
                return new CompletedAsyncResult<bool>(false, callback, server);
            }
            catch
            {
                return new CompletedAsyncResult<bool>(false, callback, server);
            }
        }
        private static void BeginCloseListener(AzureServiceBusHttpServer server)
        {
            Contract.Assert(server != null);

            try
            {
                if (server._listener != null)
                {
                    IAsyncResult result = server._listener.BeginClose(_onCloseListenerComplete, server);
                    if (result.CompletedSynchronously)
                    {
                        CloseListenerComplete(result);
                    }
                }
            }
            catch
            {
                CloseNextChannel(server);
            }
        }