-
-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issue with concurrent logging (#63)
* Fix issue with concurrent logging * Fix namespace + code format/cleanup
- Loading branch information
1 parent
6c38400
commit a15e674
Showing
7 changed files
with
174 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json; | ||
#if !NETSTANDARD | ||
using Microsoft.Owin; | ||
#else | ||
using Microsoft.AspNetCore.Http; | ||
#endif | ||
|
||
namespace WireMock.Owin | ||
{ | ||
#if !NETSTANDARD | ||
internal class GlobalExceptionMiddleware : OwinMiddleware | ||
#else | ||
internal class GlobalExceptionMiddleware | ||
#endif | ||
{ | ||
#if !NETSTANDARD | ||
public GlobalExceptionMiddleware(OwinMiddleware next) : base(next) { } | ||
#else | ||
public GlobalExceptionMiddleware(RequestDelegate next) | ||
{ | ||
Next = next; | ||
} | ||
#endif | ||
|
||
#if NETSTANDARD | ||
public RequestDelegate Next { get; private set; } | ||
#endif | ||
|
||
private readonly OwinResponseMapper _responseMapper = new OwinResponseMapper(); | ||
|
||
#if !NETSTANDARD | ||
public override async Task Invoke(IOwinContext ctx) | ||
#else | ||
public async Task Invoke(HttpContext ctx) | ||
#endif | ||
{ | ||
try | ||
{ | ||
await Next?.Invoke(ctx); | ||
} | ||
catch (Exception ex) | ||
{ | ||
await _responseMapper.MapAsync(new ResponseMessage { StatusCode = 500, Body = JsonConvert.SerializeObject(ex) }, ctx.Response); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
|
||
namespace WireMock.Util | ||
{ | ||
/// <summary> | ||
/// A special Collection that overrides methods of <see cref="ObservableCollection{T}"/> to make them thread safe | ||
/// </summary> | ||
/// <typeparam name="T">The type of elements in the collection.</typeparam> | ||
/// <inheritdoc cref="ObservableCollection{T}" /> | ||
public class ConcurentObservableCollection<T> : ObservableCollection<T> | ||
{ | ||
private readonly object _lockObject = new object(); | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="T:WireMock.Util.ConcurentObservableCollection`1" /> class. | ||
/// </summary> | ||
public ConcurentObservableCollection() { } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ConcurentObservableCollection{T}"/> class that contains elements copied from the specified list. | ||
/// </summary> | ||
/// <param name="list">The list from which the elements are copied.</param> | ||
public ConcurentObservableCollection(List<T> list) : base(list) { } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ConcurentObservableCollection{T}"/> class that contains elements copied from the specified collection. | ||
/// </summary> | ||
/// <param name="collection">The collection from which the elements are copied.</param> | ||
public ConcurentObservableCollection(IEnumerable<T> collection) : base(collection) { } | ||
|
||
/// <inheritdoc cref="ObservableCollection{T}.ClearItems"/> | ||
protected override void ClearItems() | ||
{ | ||
lock (_lockObject) | ||
{ | ||
base.ClearItems(); | ||
} | ||
} | ||
|
||
/// <inheritdoc cref="ObservableCollection{T}.RemoveItem"/> | ||
protected override void RemoveItem(int index) | ||
{ | ||
lock (_lockObject) | ||
{ | ||
base.RemoveItem(index); | ||
} | ||
} | ||
|
||
/// <inheritdoc cref="ObservableCollection{T}.InsertItem"/> | ||
protected override void InsertItem(int index, T item) | ||
{ | ||
lock (_lockObject) | ||
{ | ||
base.InsertItem(index, item); | ||
} | ||
} | ||
|
||
/// <inheritdoc cref="ObservableCollection{T}.SetItem"/> | ||
protected override void SetItem(int index, T item) | ||
{ | ||
lock (_lockObject) | ||
{ | ||
base.SetItem(index, item); | ||
} | ||
} | ||
|
||
/// <inheritdoc cref="ObservableCollection{T}.MoveItem"/> | ||
protected override void MoveItem(int oldIndex, int newIndex) | ||
{ | ||
lock (_lockObject) | ||
{ | ||
base.MoveItem(oldIndex, newIndex); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters