0
  • I created a simple Web Api selfHost as windows service which listens to an address which is dynamically loads from the database and normally it includes port number( like : http://localhost:1900) When I change the address( for example port number, something like http://localhost:1901) the service can catch the requests on the new port but the requests on old port ( http:localhost:1900) leads to crashing the service and it will be stopped. I just could debug my service and saw just NullReference Error and not any more info about it. I don't know even where this error happened and non of my logs could help me. what do you think about this error? Have you ever seen this kind of error before?

  • For more info I should say just some errors I can see in Event Viewer window :

  • Application: {Service.exe} Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.NullReferenceException at System.Web.Http.SelfHost.HttpSelfHostServer+d__35.MoveNext() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task) at System.Web.Http.SelfHost.HttpSelfHostServer+d__34.MoveNext() at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__6_1(System.Object) at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(System.Object) at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)

  • Faulting application name: {Service.exe}, version: 1.0.0.0, time stamp: 0xc594704b Faulting module name: KERNELBASE.dll, version: 10.0.14393.3383, time stamp: 0x5ddcb9ff Exception code: 0xe0434352 Fault offset: 0x000dc232 Faulting process id: 0x7370 Faulting application start time: 0x01d72886545b1d41 Faulting application path: {Service PhysicalAddress} Faulting module path: C:\Windows\System32\KERNELBASE.dll Report Id: 305c75f4-8c83-484a-b673-565abfc2b7d6 Faulting package full name: Faulting package-relative application ID

  • For more details I bring my Service Class Body below :

    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)
              {
              }
    
          }
    
      }
    }
    
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Peter Bons Apr 03 '21 at 15:20
  • *Description: The process was terminated due to an unhandled exception. Exception Info: System.NullReferenceException at System.Web.Http.SelfHost.HttpSelfHostServer.* Can yoi post some code at how the server is set up? – Peter Bons Apr 03 '21 at 15:21
  • thank you @(Peter Bons) for your replying but no! that wasn't my answer, I know what does NullReference mean but my question was that how can I find out where does it come from? because I had set varous log in my code and didn't get anything – Ehsan Shirvan Apr 04 '21 at 03:38
  • @PeterBons I edited my question and added my class – Ehsan Shirvan Apr 04 '21 at 05:17

0 Answers0