C#Self Hosted Web - 另一个应用程序使用的TCP端口(win10)

时间:2017-07-13 14:34:05

标签: c# wpf netsh self-host-webapi

尝试在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 delete urlacl url = http://+:50231/
  • netsh http add 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

1 个答案:

答案 0 :(得分:2)

“你重启了吗?”这样做了。对我来说没有意义,因为没有列出任何使用该端口的东西......哦,好吧。