您可以使用 Microsoft.Owin 包在控制台应用程序中托管 http 控制器
想法是在这种情况下您不需要安装 IIS。
如果需要,您可以将应用程序托管为 Windows 服务(使用 TopShelf)。
但是,您正在使 ASP 控制器正常工作,不应该再为 HttpListeners 烦恼。
有很多可用的操作指南,例如here
简而言之,您必须这样做
添加Microsoft.AspNet.WebApi.OwinSelfHost包
在代码中配置Owin就像
public class SelfHostStart
{
private readonly WebSettings config;
public SelfHostStart(WebSettings config)
{
this.config = config;
}
void Start<T>(string hostAddress, string message)
{
if (string.IsNullOrEmpty(hostAddress))
throw new ArgumentException("hostAddress", $"no value to start host {message}");
var oh = new OwinHost();
server = oh.Start<T>(hostAddress, message);
}
public void Start()
{
if (config?.Enabled == true)
{
Start<Startup>(config.HostAddress, "webServer");
}
}
}
public class WebSettings
{
public bool Enabled { get; set; }
public string HostAddress { get; set; }
}
然后您可以创建Startup.cs 来配置 http 托管,如下所示:
public class Startup
{
public void Configuration(IAppBuilder appBuilder)
{
HttpConfiguration config = GetHttpConfig(appBuilder);
ConfigureWebApi(appBuilder, config);
DEBUG
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.LocalOnly;
if
appBuilder.UseCors(CorsOptions.AllowAll);
appBuilder.UseWebApi(config);
}
public HttpConfiguration GetHttpConfig(IAppBuilder appBuilder)
{
var config = new HttpConfiguration();
// you may override some default behavior here
//config.Services.Replace(typeof(IHttpControllerActivator), new UnityControllerActivator());
//config.Services.Add(typeof(System.Web.Http.ExceptionHandling.IExceptionLogger), new ExceptionLog(appBuilder.CreateLogger<ExceptionLog>()));
//config.Services.Replace(typeof(System.Web.Http.ExceptionHandling.IExceptionHandler), new CustomExceptionHandler()); // to make ExceptionHandler works
//config.MessageHandlers.Add(new LoggingHandler(appBuilder.CreateLogger("rest")));
return config;
}
public void ConfigureWebApi(IAppBuilder app, HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.EnsureInitialized();
}
}
和控制器(具有可用于服务客户端的方法的类):
[AllowAnonymous]
public class StatusController : ApiController
{
public StatusController()
{
}
[Route("status")]
public StatusDto Get()
{
var res = new StatusDto
{
DbVersion = "123",
LocalCurrency = "USD",
SwiftCode = "12345", // just sample
};
return res;
}
}
public class StatusDto
{
public string DbVersion { get; set; }
public string LocalCurrency { get; set; }
public string SwiftCode { get; set; }
}
并在您的控制台中启动 http 服务器
var config = new WebSettings() { HostAddress = "http://localhost:8080" };
var start = new SelfHostStart(config);
start.Start();
您应该使用netsh http add urlacl 命令为您的控制台提供侦听 http url 的权限,例如
netsh http 添加 urlacl url=http://+:8080/ user=everyone
然后你可以浏览 http://localhost:8080/status 并且应该从 http 控制台服务器获取报告。
您可以通过使用方法创建新控制器来扩展您的服务