Exemple #1
0
        public void Start <TContext>(IHttpApplication <TContext> application)
        {
            // Note that this example does not take into account of Nowin's "server.OnSendingHeaders" callback.
            // Ideally we should ensure this method is fired before disposing the context.
            _callback = async features =>
            {
                var context = application.CreateContext(features);
                try
                {
                    await application.ProcessRequestAsync(context);
                }
                catch (Exception ex)
                {
                    application.DisposeContext(context, ex);
                    throw;
                }
                application.DisposeContext(context, null);
            };

            var address       = Features.Get <IServerAddressesFeature>().Addresses.First();
            var port          = new Uri(address).Port;
            var serverBuilder = ServerBuilder.New().SetAddress(IPAddress.Loopback).SetPort(port);

            _nowinServer = serverBuilder.SetOwinApp(OwinWebSocketAcceptAdapter.AdaptWebSockets(HandleRequest)).Build();
            _nowinServer.Start();
        }
        public void Start()
        {
            DefaultHttpContextFactory = new DefaultHttpContextFactory(new DefaultServiceProviderFactory().CreateServiceProvider(SerCollection));
            if (PipeEntityes.Count > 1)
            {
                for (int i = 0; i < PipeEntityes.Count; i++)
                {
                    if (i != PipeEntityes.Count - 1)
                    {
                        PipeEntityes[i].Next = PipeEntityes[i + 1];
                    }
                    else
                    {
                        PipeEntityes[i].Next = new PipeEntity {
                            Action = (c, p) => { return(Task.CompletedTask); }
                        }
                    };
                }
            }

            Server = ServerBuilder.SetOwinApp(OwinApp).Build();
            Server.Start();

            Console.TreatControlCAsInput = false;
            while (true)
            {
                Console.ReadKey(true);
            }
        }

        ~CustomServer()
        {
            Server?.Dispose();
        }
    }
 public IDisposable Start(IServerInformation serverInformation, Func<object, Task> application)
 {
     var information = (NowinServerInformation)serverInformation;
     _callback = application;
     INowinServer server = information.Builder.Build();
     server.Start();
     return server;
 }
Exemple #4
0
        public IDisposable Start(IFeatureCollection serverFeatures, Func <IFeatureCollection, Task> application)
        {
            var information = (NowinServerInformation)serverFeatures.Get <NowinServerInformation>();

            _callback = application;
            INowinServer server = information.Builder.Build();

            server.Start();
            return(server);
        }
Exemple #5
0
        public Task StartAsync <TContext>(IHttpApplication <TContext> application, CancellationToken cancellationToken)
        {
            // Note that this example does not take into account of Nowin's "server.OnSendingHeaders" callback.
            // Ideally we should ensure this method is fired before disposing the context.
            Func <IDictionary <string, object>, Task> appFunc = async env =>
            {
                // The reason for 2 level of wrapping is because the OwinFeatureCollection isn't mutable
                // so features can't be added
                var features = new FeatureCollection(new OwinFeatureCollection(env));

                var context = application.CreateContext(features);
                try
                {
                    await application.ProcessRequestAsync(context);
                }
                catch (Exception ex)
                {
                    application.DisposeContext(context, ex);
                    throw;
                }

                application.DisposeContext(context, null);
            };

            // Add the web socket adapter so we can turn OWIN websockets into ASP.NET Core compatible web sockets.
            // The calling pattern is a bit different
            appFunc = OwinWebSocketAcceptAdapter.AdaptWebSockets(appFunc);

            // Get the server addresses
            var address = Features.Get <IServerAddressesFeature>().Addresses.First();

            var       uri  = new Uri(address);
            var       port = uri.Port;
            IPAddress ip;

            if (!IPAddress.TryParse(uri.Host, out ip))
            {
                ip = IPAddress.Loopback;
            }

            _nowinServer = _builder.SetAddress(ip)
                           .SetPort(port)
                           .SetOwinApp(appFunc)
                           .Build();
            _nowinServer.Start();

            return(Task.CompletedTask);
        }