private void Configure(AzureServiceBusOwinServiceConfiguration config, Action<IAppBuilder> startup)
        {
            if (startup == null)
            {
                throw new ArgumentNullException("startup");
            }

            var options = new StartOptions();
            if (string.IsNullOrWhiteSpace(options.AppStartup))
            {
                // Populate AppStartup for use in host.AppName
                options.AppStartup = startup.Method.ReflectedType.FullName;
            }

            var testServerFactory = new AzureServiceBusOwinServerFactory(config);
            var services = ServicesFactory.Create();
            var engine = services.GetService<IHostingEngine>();
            var context = new StartContext(options)
            {
                ServerFactory = new ServerFactoryAdapter(testServerFactory),
                Startup = startup
            };
            _started = engine.Start(context);
            _next = testServerFactory.Invoke;
        }
        static void Main(string[] args)
        {
            var sbConfig = new AzureServiceBusOwinServiceConfiguration(
                issuerName: "owner",
                issuerSecret: SecretCredentials.Secret,
                address: SecretCredentials.ServiceBusAddress);

            using (AzureServiceBusOwinServer.Create(sbConfig, app =>
                {
                    var config = new HttpConfiguration();
                    config.Routes.MapHttpRoute("ApiDefault", "webapi/{controller}/{id}", new {id = RouteParameter.Optional});
                    config.MessageHandlers.Add(new TraceMessageHandler());

                    app.Use(async (ctx, next) =>
                    {
                        Trace.TraceInformation(ctx.Request.Uri.ToString());
                        await next();
                        Trace.TraceInformation(ctx.Response.StatusCode.ToString());
                    });

                    app.UseErrorPage();

                    app.UseBasicAuthentication(new BasicAuthenticationOptions("ndc", (user, pw) =>
                    {
                        var ticket = user != pw
                            ? null
                            : new AuthenticationTicket
                            (
                                new ClaimsIdentity(new GenericIdentity(user, "Basic")),
                                new AuthenticationProperties()
                            );
                        return Task.FromResult(ticket);
                    }));

                    app.UseWebApi(config);
                }))
            {

                Console.WriteLine("Server is opened at {0}", sbConfig.Address);
                Process.Start(sbConfig.Address);
                Console.ReadKey();
            }
        }
 public static AzureServiceBusOwinServer Create(AzureServiceBusOwinServiceConfiguration config, Action<IAppBuilder> startup)
 {
     var server = new AzureServiceBusOwinServer();
     server.Configure(config, startup);
     return server;
 }
 public AzureServiceBusOwinServerFactory(AzureServiceBusOwinServiceConfiguration config)
 {
     _config = config;
 }