当前位置: 首页>>代码示例>>C#>>正文


C# WebHostBuilder.Start方法代码示例

本文整理汇总了C#中Microsoft.AspNetCore.Hosting.WebHostBuilder.Start方法的典型用法代码示例。如果您正苦于以下问题:C# WebHostBuilder.Start方法的具体用法?C# WebHostBuilder.Start怎么用?C# WebHostBuilder.Start使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.AspNetCore.Hosting.WebHostBuilder的用法示例。


在下文中一共展示了WebHostBuilder.Start方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

        public static void Main(string[] args)
        {
            // TODO AppDomains are no longer a thing - what to do about Razor?
            // Initialize in separate AppDomain for Razor
            //if (AppDomain.CurrentDomain.IsDefaultAppDomain())
            //{
            //    var setup = new AppDomainSetup
            //    {
            //        ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase
            //    };
            //    var strongNames = new StrongName[0];

            //    var domain = AppDomain.CreateDomain(
            //        "MyMainDomain",
            //        null,
            //        setup,
            //        new PermissionSet(PermissionState.Unrestricted),
            //        strongNames);

            //    domain.ExecuteAssembly(Assembly.GetEntryAssembly().Location);
            //    AppDomain.Unload(domain);
            //    return;
            //}

            using (var host = new WebHostBuilder()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseKestrel()
                .UseStartup<Startup>()
                .UseUrls("http://localhost:1234")
                .Build())
            {
                Log.Info("Starting server...");
                host.Start();
                Log.Info("Running on port 1234.");

                if (IsRunningOnLinux())
                {
                    // On linux (i.e. Travis CI), we only terminate when killed with -SIGUSR1
                    // TODO Listening to arbitrary unix signals is unsupported on .NET Core so far. Reimplement when available.
                    //UnixSignal.WaitAny(new[] { new UnixSignal(Signum.SIGUSR1) });
                }
                else
                {
                    // During local debugging, we quit on any input.
                    Console.ReadLine();
                }

                Log.Info("Shutting down server...");
            }
        }
开发者ID:ThePadawan,项目名称:the_diary,代码行数:50,代码来源:Program.cs

示例2: Main

        // Entry point for the application.
        public static void Main(string[] args)
        {
            var config = new ConfigurationBuilder().AddCommandLine(args).Build();

            var host = new WebHostBuilder()
                .UseConfiguration(config)
                .UseStartup<StartupBlockingOnStart>()
                .Build();

            using (host)
            {
                host.Start();
                Console.ReadLine();
            }
        }
开发者ID:akrisiun,项目名称:Hosting,代码行数:16,代码来源:StartupBlockingOnStart.cs

示例3: StartWebSocketServer

        private IWebHost StartWebSocketServer(Func<HttpContext, Task> app)
        {
            Action<IApplicationBuilder> startup = builder =>
            {
                builder.Use(async (ct, next) =>
                {
                    try
                    {
                        // Kestrel does not return proper error responses:
                        // https://github.com/aspnet/KestrelHttpServer/issues/43
                        await next();
                    }
                    catch (Exception ex)
                    {
                        if (ct.Response.HasStarted)
                        {
                            throw;
                        }

                        ct.Response.StatusCode = 500;
                        ct.Response.Headers.Clear();
                        await ct.Response.WriteAsync(ex.ToString());
                    }
                });

                builder.UseWebSockets();
                builder.Run(c => app(c));
            };

            var configBuilder = new ConfigurationBuilder();
            configBuilder.AddInMemoryCollection();
            var config = configBuilder.Build();
            config["server.urls"] = "http://localhost:54321";

            var host = new WebHostBuilder()
                .UseConfiguration(config)
                .UseKestrel()
                .Configure(startup)
                .Build();

            host.Start();

            return host;
        }
开发者ID:kerryjiang,项目名称:WebSocket4Net,代码行数:44,代码来源:Tests.cs

示例4: Start

        /// <summary>
        /// Starts listening at the specified port.
        /// </summary>
        public void Start()
        {
            Startup.Listener = this;
           
            m_host = new WebHostBuilder();

            HttpsConnectionFilterOptions httpsOptions = new HttpsConnectionFilterOptions();
            httpsOptions.CheckCertificateRevocation = false;
            httpsOptions.ClientCertificateMode = ClientCertificateMode.NoCertificate;
            httpsOptions.ServerCertificate = m_serverCert;
            httpsOptions.SslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12;

            m_host.UseKestrel(options =>
            {
                options.NoDelay = true;
                options.UseHttps(httpsOptions);
            });

            m_host.UseContentRoot(Directory.GetCurrentDirectory());
            m_host.UseStartup<Startup>();
            m_host.Build();

            m_host.Start(Utils.ReplaceLocalhost(m_uri.ToString()));
        }
开发者ID:OPCFoundation,项目名称:UA-.NETStandardLibrary,代码行数:27,代码来源:HttpsListener.cs

示例5: startWebServer

        private void startWebServer(int port, WebSocketsHandler webSockets)
        {
            var baseDirectory = AppContext.BaseDirectory;
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(baseDirectory)
                .UseUrls($"http://localhost:{port}")
                .Configure(app =>
                {
                    app.UseWebSockets();

                    app.Use(async (http, next) =>
                    {
                        if (http.WebSockets.IsWebSocketRequest)
                            await webSockets.HandleSocket(http).ConfigureAwait(false);
                        else
                            await next().ConfigureAwait(false);
                    });

#if DEBUG
                    configureStaticFiles(app);
#endif

                    app.Run(async http =>
                    {
                        if (http.Request.Path == "/favicon.ico")
                        {
                            var stream =
                                GetType()
                                    .GetTypeInfo()
                                    .Assembly.GetManifestResourceStream("StorytellerRunner.favicon.ico");

                            http.Response.ContentType = "image/x-icon";
                            await stream.CopyToAsync(http.Response.Body).ConfigureAwait(false);

                            return;
                        }

                        try
                        {
                            string html;
                            if (http.Request.Path.HasValue && http.Request.Path.Value == "/preview")
                            {
                                html = ExportWriter.BuildPage(_application);
                            }
                            else
                            {
                                html = HomeEndpoint.BuildPage(_application, _input).ToString();
                            }


                            http.Response.ContentType = "text/html";
                            await http.Response.WriteAsync(html).ConfigureAwait(false);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.ToString());
                            throw;
                        }



                    });
                });

            _server = host.Start();
        }
开发者ID:storyteller,项目名称:Storyteller,代码行数:67,代码来源:WebApplicationRunner.cs

示例6: startHost

        private IWebHost startHost(int port, WebSocketsHandler webSockets, TopicMiddleware middleware)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseUrls($"http://localhost:{port}")
                .Configure(app =>
                {
                    app.UseWebSockets();

                    app.Use(async (http, next) =>
                    {
                        if (http.WebSockets.IsWebSocketRequest)
                            await webSockets.HandleSocket(http).ConfigureAwait(false);
                        else
                            await next().ConfigureAwait(false);
                    });

                    app.UseStaticFiles(new StaticFileOptions
                    {
                        ServeUnknownFileTypes = true,
                        FileProvider = new PhysicalFileProvider(_settings.Root)
                    });

                    app.Use(async (http, next) =>
                    {
                        if (http.Request.Method.EqualsIgnoreCase("POST"))
                            switch (http.Request.Path)
                            {
                                case "/refresh":
                                Console.WriteLine("Hey, I got the order to do the hard refresh!");
                                    await HardRefresh().ConfigureAwait(false);

                                    _refresher.RefreshPage();
                                    break;

                                case "/open":
                                
                                    var url = new Uri(http.Request.Headers["referer"]);

                                    Console.WriteLine("Open requested to " + url);

                                    var topic = FindTopicByUrl(url.AbsolutePath.TrimStart('/'));
                                    if (topic != null)
                                    {
                                        try {
                                            var start = new ProcessStartInfo();
                                            start.UseShellExecute = true;
                                            start.FileName = "open";
                                            start.Arguments = topic.File;
                                            Process.Start(start);
                                            
                                        }
                                        catch (Exception ex){
                                            Process.Start(topic.File);
                                        }


                                        
                                    }
                                        

                                    break;
                            }
                        else
                            await next().ConfigureAwait(false);
                    });

                    app.Run(middleware.Invoke);
                })
                .Build();

            host.Start();
            return host;
        }
开发者ID:storyteller,项目名称:Storyteller,代码行数:75,代码来源:DocProject.cs


注:本文中的Microsoft.AspNetCore.Hosting.WebHostBuilder.Start方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。