刚开始尝试在 Windows 10 1703 (15063.483) 中使用自托管 webapi 应用程序时出现异常
VS2017:版本 15.2 (26430.15) 发布 VisualStudio.15.Release/15.2.0+26430.15
我尝试在管理员模式下运行 VS,结果与非管理员模式相同。我的应用程序正在尝试使用http://192.168.12.118:50231 该项目是一个 .NET 4.5 WPF 应用程序。
在应用程序第一次失败后,我做了:
- netsh http 删除 urlacl url= http://+:50231/
netsh http 添加 urlacl url= http://+:50231/ user=AD\USER
netsh http show url => 显示注册成功
- netstat -o -n -a => 显示该端口上没有其他应用程序在任何 IP 上运行
这是我正在使用的代码:
HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(new Uri("http://" + MyIP + ":" + MyPort));
config.MaxReceivedMessageSize = Int32.MaxValue - 1000;
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// Startup server
server = new HttpSelfHostServer(config);
server.OpenAsync().Wait();
这是我得到的异常,当执行命中 OpenAsync 时:
.Inner Type: AddressAlreadyInUseException
.Inner Message: HTTP could not register URL http://+:50231/ because TCP port 50231 is being used by another application.
.Inner Stack: at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
at System.ServiceModel.Channels.CommunicationObject.EndOpen(IAsyncResult result)
at System.Web.Http.SelfHost.HttpSelfHostServer.OpenListenerComplete(IAsyncResult result)
..Inner Type: HttpListenerException
..Inner Message: The process cannot access the file because it is being used by another process
..Inner Stack: at System.Runtime.AsyncResult.End[TAsyncResult] (IAsyncResult result)
at System.ServiceModel.Channels.CommunicationObject.EndOpen(IAsyncResult result)
at System.Web.Http.SelfHost.HttpSelfHostServer.OpenListenerComplete(IAsyncResult result)`
TIA