【发布时间】:2017-12-18 10:07:25
【问题描述】:
刚开始尝试在 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
【问题讨论】:
-
我怀疑你的应用打开了两次端口。
-
我不这么认为,但为了测试,我在它到达 OpenAsync() 之前设置了一个断点。当它命中时,我运行了 netstat,但端口上什么也没有。抛出异常后(在我的处理程序中),我运行 netstat 仍然没有。谢谢
-
FWIW:netstat -o -n -a | findstr /r /c:".50231."
-
尝试删除ACL分配......
netsh http delete urlacl url=http://+:50231/ -
如果我删除它,然后运行(不重新添加,我会收到相同的异常消息。
标签: c# wpf netsh self-host-webapi