DotNet 学习笔记 Servers

Servers

ASP.NET Core ships with two different HTTP servers:

•Microsoft.AspNetCore.Server.Kestrel (AKA Kestrel, cross-platform)
•Microsoft.AspNetCore.Server.WebListener (AKA WebListener, Windows-only, preview)

{
  "webroot": "wwwroot",
  "version": "1.0.0-*",

  "dependencies": {
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-rc1-final"
  },

  "commands": {
    "run": "run server.urls=http://localhost:5003",
    "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.Kestrel --server.urls http://localhost:5000",
    "weblistener": "Microsoft.AspNet.Hosting --server WebListener --server.urls http://localhost:5004"
  },

  "frameworks": {
    "dnx451": { },


using System;
using System.Threading.Tasks;
using Microsoft.AspNet.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNet.Builder;
using Microsoft.Extensions.Logging;
using Microsoft.AspNet.Server.Kestrel;

namespace ServersDemo
{
    /// <summary>
    /// This demonstrates how the application can be launched in a console application. 
    /// Executing the "dnx run" command in the application folder will run this app.
    /// </summary>
    public class Program
    {
        private readonly IServiceProvider _serviceProvider;

        public Program(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        public Task<int> Main(string[] args)
        {
            //Add command line configuration source to read command line parameters.
            var builder = new ConfigurationBuilder();
            builder.AddCommandLine(args);
            var config = builder.Build();

            using (new WebHostBuilder(config)
                .UseServer("Microsoft.AspNet.Server.Kestrel")
                .Build()
                .Start())
            {
                Console.WriteLine("Started the server..");
                Console.WriteLine("Press any key to stop the server");
                Console.ReadLine();
            }
            return Task.FromResult(0);
        }
    }
}
------------------------------------------------------------------------------
Programmatic configuration

public void Configure(IApplicationBuilder app, IApplicationLifetime lifetime, ILoggerFactory loggerFactory)
{
    var webListenerInfo = app.ServerFeatures.Get<WebListener>();
    if (webListenerInfo != null)
    {
        webListenerInfo.AuthenticationManager.AuthenticationSchemes =
            AuthenticationSchemes.AllowAnonymous;
    }

    var serverAddress = app.ServerFeatures.Get<IServerAddressesFeature>()?.Addresses.FirstOrDefault();

    app.Run(async (context) =>
    {
        var message = String.Format("Hello World from {0}",
                                serverAddress);
        await context.Response.WriteAsync(message);
    });
}

"web": "Microsoft.AspNetCore.Hosting --server Microsoft.AspNetCore.Server.WebListener --server.urls http://localhost:5000"

 

posted @ 2016-06-30 10:42  自然去留  阅读(295)  评论(0)    收藏  举报