Asp.net mvc .Net Framework Web Api自主机服务突然停止

Asp.net mvc .Net Framework Web Api自主机服务突然停止,asp.net-mvc,asp.net-web-api,self-host-webapi,Asp.net Mvc,Asp.net Web Api,Self Host Webapi,我创建了一个简单的Web Api selfHost as windows服务,它侦听从数据库动态加载的地址,通常包括端口号(如:http://localhost:1900) 当我更改地址时(例如端口号,类似http://localhost:1901)服务可以捕获新端口上的请求,但旧端口(http:localhost:1900)上的请求会导致服务崩溃,服务将停止。 我可以调试我的服务,只看到null引用错误,没有更多的信息。 我甚至不知道这个错误发生在哪里,没有我的日志可以帮助我。 你对这个错误怎

我创建了一个简单的Web Api selfHost as windows服务,它侦听从数据库动态加载的地址,通常包括端口号(如:http://localhost:1900) 当我更改地址时(例如端口号,类似http://localhost:1901)服务可以捕获新端口上的请求,但旧端口(http:localhost:1900)上的请求会导致服务崩溃,服务将停止。 我可以调试我的服务,只看到null引用错误,没有更多的信息。 我甚至不知道这个错误发生在哪里,没有我的日志可以帮助我。 你对这个错误怎么看?你以前见过这种错误吗

  • 有关更多信息,我只想说我在事件查看器窗口中看到的一些错误:

  • 应用程序:{Service.exe} 框架版本:v4.0.30319 描述:由于未处理的异常,进程已终止。 异常信息:System.NullReferenceException 在System.Web.Http.SelfHost.HttpSelfHostServer+d_u35.MoveNext()中 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task) 在System.Web.Http.SelfHost.HttpSelfHostServer+d_u34.MoveNext()中 在System.Runtime.CompilerServices.AsyncMethodBuilderCore+c.b_uu6_u1(System.Object)上 在System.Threading.QueueUserWorkItemCallback.WaitCallback_上下文(System.Object)中 位于System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext,System.Threading.ContextCallback,System.Object,布尔值) 在System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,System.Threading.ContextCallback,System.Object,布尔值)

  • 出错的应用程序名称:{Service.exe},版本:1.0.0.0,时间戳:0xc594704b 故障模块名称:KERNELBASE.dll,版本:10.0.14393.3383,时间戳:0x5ddcb9ff 异常代码:0xe0434352 故障偏移量:0x000dc232 出错进程id:0x7370 故障应用程序启动时间:0x01d72886545b1d41 出错的应用程序路径:{Service PhysicalAddress} 故障模块路径:C:\Windows\System32\KERNELBASE.dll 报告Id:305c75f4-8c83-484a-b673-565abfc2b7d6 故障包全名: 错误的包相对应用程序ID

  • 有关更多详细信息,我将我的服务类主体带到下面:

    class service
     {
      HttpSelfHostConfiguration config;
      HttpSelfHostServer server;
      Timer _timer = new Timer();
    
      protected override void OnStart(string[] args)
      {
          _timer.Interval = 2000;
          _timer.Elapsed += _timer_Elapsed;
          _timer.Enabled = true;
      }
    
      private void _timer_Elapsed(object sender, ElapsedEventArgs e)
      {
          var listenToUrl = _getDestUrlFromDB();
          var configChanged = false;
          if (config != null && config.BaseAddress.AbsoluteUri != listenToUrl + "/")
          {
              configChanged = true;
              config.Dispose();
          }
          config = new HttpSelfHostConfiguration(uploadApiUrl.Data);
          config.Routes.MapHttpRoute("default",
                                      "api/{controller}/{id}",
                                      new { controller = "Home", id = RouteParameter.Optional });
          config.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows;
          if (server == null)
          {
              server = new HttpSelfHostServer(config);
              var task = server.OpenAsync();
              task.Wait();
    
          }
          else if (configChanged)
          {
    
              try
              {
                  Process.Start("cmd", $@"netsh http add urlacl url={listenToUrl} ");
                  Process.Start("cmd", $@"delete urlacl url={listenToUrl} ");
                  server.Dispose();
                  server = new HttpSelfHostServer(config);
                  var task = server.OpenAsync();
                  task.Wait();
    
              }
              catch (Exception ex)
              {
              }
    
          }
    
      }
    }
    

  • 这是否回答了您的问题?描述:由于未处理的异常,进程已终止。异常信息:System.Web.Http.SelfHost.HttpSelfHostServer上的System.NullReferenceException。你能在服务器的设置上发布一些代码吗?谢谢你@(Peter Bons)的回复,但是没有!这不是我的答案,我知道NullReference是什么意思,但我的问题是,我如何才能找到它来自哪里?因为我在代码中设置了varous log,但没有得到anything@PeterBons我编辑了我的问题并添加了我的类