本文整理汇总了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...");
}
}
示例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();
}
}
示例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;
}
示例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()));
}
示例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();
}
示例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;
}