/// <summary> /// Listen indefinitely for network connections on the specified port. /// </summary> private async Task ListenForConnectionsAsync() { INetworkListener listener = null; try { var noDelay = Options?.TcpNoDelay ?? DicomServiceOptions.Default.TcpNoDelay; listener = NetworkManager.CreateNetworkListener(IPAddress, Port); await listener.StartAsync().ConfigureAwait(false); IsListening = true; while (!_cancellationSource.IsCancellationRequested) { await _hasNonMaxServicesFlag.WaitAsync().ConfigureAwait(false); var networkStream = await listener .AcceptNetworkStreamAsync(_certificateName, noDelay, _cancellationSource.Token) .ConfigureAwait(false); if (networkStream != null) { var scp = CreateScp(networkStream); if (Options != null) { scp.Options = Options; } var serviceTask = scp.RunAsync(); lock (_services) { _services.Add(new RunningDicomService(scp, serviceTask)); } _hasServicesFlag.Set(); if (IsServicesAtMax) { _hasNonMaxServicesFlag.Reset(); } } } } catch (OperationCanceledException) { } catch (Exception e) { Logger.Error("Exception listening for DICOM services, {@error}", e); Stop(); Exception = e; } finally { listener?.Stop(); IsListening = false; } }
/// <summary> /// Listen indefinitely for network connections on the specified port. /// </summary> private async Task ListenAsync() { try { var noDelay = this.Options?.TcpNoDelay ?? DicomServiceOptions.Default.TcpNoDelay; var listener = NetworkManager.CreateNetworkListener(this.Port); await listener.StartAsync().ConfigureAwait(false); this.IsListening = true; while (!this.cancellationSource.IsCancellationRequested) { var networkStream = await listener.AcceptNetworkStreamAsync( this.certificateName, noDelay, this.cancellationSource.Token).ConfigureAwait(false); if (networkStream != null) { var scp = this.CreateScp(networkStream); if (this.Options != null) { scp.Options = this.Options; } this.clients.Add(scp); } } listener.Stop(); this.IsListening = false; this.Exception = null; } catch (OperationCanceledException) { this.Logger.Info("Listening manually terminated"); this.IsListening = false; this.Exception = null; } catch (Exception e) { this.Logger.Error("Exception listening for clients, {@error}", e); this.Stop(); this.IsListening = false; this.Exception = e; } }
/// <summary> /// Listen indefinitely for network connections on the specified port. /// </summary> private async void ListenAsync() { try { var noDelay = this.Options != null ? this.Options.TcpNoDelay : DicomServiceOptions.Default.TcpNoDelay; var listener = NetworkManager.CreateNetworkListener(this.port); ///--- //----The process was terminated due to an unhandled exception. Exception Info: System.NullReferenceException Stack: //at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__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) //at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() //at System.Threading.ThreadPoolWorkQueue.Dispatch() //at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() //------------http://stackoverflow.com/questions/16056016/nullreferenceexception-in-system-threading-tasks-calling-httpclient-getasyncurl //---原始代码 BEGIN //await listener.StartAsync().ConfigureAwait(false); //---原始代码 END //----Bug Fixed BEGIN #pragma warning disable 4014 // Fire and forget. var task = Task.Run(async() => { await listener.StartAsync().ConfigureAwait(false); }); Task.WaitAll(new Task[] { task }); //---Bug Fixed END this.IsListening = true; while (!this.cancellationSource.IsCancellationRequested) { var networkStream = await listener.AcceptNetworkStreamAsync(this.certificateName, noDelay, this.cancellationSource.Token) .ConfigureAwait(false); if (networkStream != null) { var scp = this.CreateScp(networkStream.AsStream()); if (this.Options != null) { scp.Options = this.Options; } this.clients.Add(scp); } } listener.Stop(); this.IsListening = false; this.Exception = null; } catch (OperationCanceledException) { this.Logger.Info("Listening manually terminated"); this.IsListening = false; this.Exception = null; } catch (Exception e) { this.Logger.Error("Exception listening for clients, {@error}", e); this.Stop(); this.IsListening = false; this.Exception = e; } }