我创建了一个简单的 Web Api selfHost 作为 Windows 服务,它侦听从数据库动态加载的地址,通常它包括端口号(例如:http://localhost:1900)当我更改地址时(例如端口号,像 http://localhost:1901 之类的服务可以捕获新端口上的请求,但旧端口( http:localhost:1900 )上的请求会导致服务崩溃并且它将被停止。我只能调试我的服务,只看到 NullReference 错误,而没有更多关于它的信息。我什至不知道这个错误发生在哪里,而且我的日志都不能帮助我。你怎么看这个错误?你以前见过这种错误吗?
有关更多信息,我应该说我可以在事件查看器窗口中看到的一些错误:
应用程序:{Service.exe} 框架版本:v4.0.30319 描述:进程因未处理的异常而终止。异常信息:System.Web.Http.SelfHost.HttpSelfHostServer+d__35.MoveNext() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task) 在 System.Runtime.CompilerServices.TaskAwaiter 的 System.NullReferenceException。 HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task) 在 System.Web.Http.SelfHost.HttpSelfHostServer+d__34.MoveNext() 在 System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__6_1(System.Object) 在 System.Threading.QueueUserWorkItemCallback .WaitCallback_Context(System.Object) 在 System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) 在 System.Threading。
错误应用程序名称:{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 错误包全名:错误包相对应用程序编号
有关更多详细信息,我将我的服务类正文带到下面:
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) { } } } }
问问题
60 次