From b2eb7b9093be012ecf70d7500199afaf8b2ece17 Mon Sep 17 00:00:00 2001 From: Jeff Handley Date: Tue, 11 Sep 2012 17:55:12 -0700 Subject: [PATCH 1/3] Fixing StyleCop errors in Razor. --- .../Editor/BackgroundParser.cs | 50 ++++++++++--------- src/System.Web.Razor/RazorEditorParser.cs | 6 ++- src/System.Web.Razor/Text/SourceLocation.cs | 4 +- 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/src/System.Web.Razor/Editor/BackgroundParser.cs b/src/System.Web.Razor/Editor/BackgroundParser.cs index 84700f19..5be3f6f0 100644 --- a/src/System.Web.Razor/Editor/BackgroundParser.cs +++ b/src/System.Web.Razor/Editor/BackgroundParser.cs @@ -17,9 +17,12 @@ internal class BackgroundParser : IDisposable private MainThreadState _main; private BackgroundThread _bg; - public bool IsIdle + public BackgroundParser(RazorEngineHost host, string fileName) { - get { return _main.IsIdle; } + _main = new MainThreadState(fileName); + _bg = new BackgroundThread(_main, host, fileName); + + _main.ResultsReady += (sender, args) => OnResultsReady(args); } /// @@ -27,12 +30,9 @@ public bool IsIdle /// public event EventHandler ResultsReady; - public BackgroundParser(RazorEngineHost host, string fileName) + public bool IsIdle { - _main = new MainThreadState(fileName); - _bg = new BackgroundThread(_main, host, fileName); - - _main.ResultsReady += (sender, args) => OnResultsReady(args); + get { return _main.IsIdle; } } public void Start() @@ -144,13 +144,24 @@ private class MainThreadState : ThreadStateBase, IDisposable private object _stateLock = new object(); private IList _changes = new List(); - public CancellationToken CancelToken { get { return _cancelSource.Token; } } + public MainThreadState(string fileName) + { + _fileName = fileName; + + SetThreadId(Thread.CurrentThread.ManagedThreadId); + } public event EventHandler ResultsReady; + public CancellationToken CancelToken + { + get { return _cancelSource.Token; } + } + public bool IsIdle { - get { + get + { lock (_stateLock) { return _currentParcelCancelSource == null; @@ -158,13 +169,6 @@ public bool IsIdle } } - public MainThreadState(string fileName) - { - _fileName = fileName; - - SetThreadId(Thread.CurrentThread.ManagedThreadId); - } - public void Cancel() { EnsureOnThread(); @@ -253,7 +257,7 @@ protected virtual void Dispose(bool disposing) private class BackgroundThread : ThreadStateBase { private MainThreadState _main; - private Thread _bgThread; + private Thread _backgroundThread; private CancellationToken _shutdownToken; private RazorEngineHost _host; private string _fileName; @@ -264,18 +268,18 @@ public BackgroundThread(MainThreadState main, RazorEngineHost host, string fileN { // Run on MAIN thread! _main = main; - _bgThread = new Thread(WorkerLoop); + _backgroundThread = new Thread(WorkerLoop); _shutdownToken = _main.CancelToken; _host = host; _fileName = fileName; - SetThreadId(_bgThread.ManagedThreadId); + SetThreadId(_backgroundThread.ManagedThreadId); } // **** ANY THREAD **** public void Start() { - _bgThread.Start(); + _backgroundThread.Start(); } // **** BACKGROUND THREAD **** @@ -444,14 +448,14 @@ private GeneratorResults ParseChange(ITextBuffer buffer, CancellationToken token private class WorkParcel { - public CancellationToken CancelToken { get; private set; } - public IList Changes { get; private set; } - public WorkParcel(IList changes, CancellationToken cancelToken) { Changes = changes; CancelToken = cancelToken; } + + public CancellationToken CancelToken { get; private set; } + public IList Changes { get; private set; } } } } diff --git a/src/System.Web.Razor/RazorEditorParser.cs b/src/System.Web.Razor/RazorEditorParser.cs index 8660a9a1..1dd07035 100644 --- a/src/System.Web.Razor/RazorEditorParser.cs +++ b/src/System.Web.Razor/RazorEditorParser.cs @@ -98,7 +98,11 @@ public RazorEditorParser(RazorEngineHost host, string sourceFileName) public RazorEngineHost Host { get; private set; } public string FileName { get; private set; } public bool LastResultProvisional { get; private set; } - public Block CurrentParseTree { get { return _currentParseTree; } } + + public Block CurrentParseTree + { + get { return _currentParseTree; } + } [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Since this method is heavily affected by side-effects, particularly calls to CheckForStructureChanges, it should not be made into a property")] public virtual string GetAutoCompleteString() diff --git a/src/System.Web.Razor/Text/SourceLocation.cs b/src/System.Web.Razor/Text/SourceLocation.cs index cc40c1f5..822b2648 100644 --- a/src/System.Web.Razor/Text/SourceLocation.cs +++ b/src/System.Web.Razor/Text/SourceLocation.cs @@ -26,7 +26,9 @@ public int AbsoluteIndex get { return _absoluteIndex; } } - // THIS IS 1-based!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + /// + /// THIS IS 1-based!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + /// /// /// Gets the 1-based index of the line referred to by this Source Location. /// From 3c9ae825ac2fabc8a2bee021a7e1ee21d6d153ab Mon Sep 17 00:00:00 2001 From: Jeff Handley Date: Tue, 11 Sep 2012 18:05:21 -0700 Subject: [PATCH 2/3] Moving remarks below summary in XML comments --- src/System.Web.Razor/Text/SourceLocation.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/System.Web.Razor/Text/SourceLocation.cs b/src/System.Web.Razor/Text/SourceLocation.cs index 822b2648..3830e92a 100644 --- a/src/System.Web.Razor/Text/SourceLocation.cs +++ b/src/System.Web.Razor/Text/SourceLocation.cs @@ -26,12 +26,12 @@ public int AbsoluteIndex get { return _absoluteIndex; } } - /// - /// THIS IS 1-based!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - /// /// /// Gets the 1-based index of the line referred to by this Source Location. /// + /// + /// THIS IS 1-based!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + /// public int LineIndex { get { return _lineIndex; } From 75b5e7ea58d2a120242ff0d840600e33e9b65882 Mon Sep 17 00:00:00 2001 From: youssefm Date: Tue, 11 Sep 2012 11:11:17 -0700 Subject: [PATCH 3/3] Resolving client disconnection issues for self host server --- .../HttpSelfHostServer.cs | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/System.Web.Http.SelfHost/HttpSelfHostServer.cs b/src/System.Web.Http.SelfHost/HttpSelfHostServer.cs index 891f3499..90b66a09 100644 --- a/src/System.Web.Http.SelfHost/HttpSelfHostServer.cs +++ b/src/System.Web.Http.SelfHost/HttpSelfHostServer.cs @@ -1040,14 +1040,32 @@ public void Dispose() /// Releases unmanaged and - optionally - managed resources /// /// true to release both managed and unmanaged resources; false to release only unmanaged SRResources. + [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "We never want to fail here so we have to catch all exceptions.")] protected virtual void Dispose(bool disposing) { if (!_disposed) { if (disposing) { - RequestContext.Close(); - Reply.Close(); + // RequestContext.Close can throw if the client disconnects before it finishes receiving the response + // Catch here to avoid throwing in a Dispose method + try + { + RequestContext.Close(); + } + catch + { + } + + // HttpMessage.Close can throw if the request message throws in its Dispose implementation + // Catch here to avoid throwing in a Dispose method + try + { + Reply.Close(); + } + catch + { + } } _disposed = true;