diff --git a/.gitignore b/.gitignore index dd0049e..4aa3348 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,19 @@ + +# Created by https://www.gitignore.io/api/aspnetcore,visualstudio + +### ASPNETCore ### ## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. # User-specific files *.suo *.user +*.userosscache *.sln.docstates +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + # Build results [Dd]ebug/ [Dd]ebugPublic/ @@ -13,19 +21,21 @@ [Rr]eleases/ x64/ x86/ -build/ bld/ [Bb]in/ [Oo]bj/ +[Ll]og/ -# Roslyn cache directories -*.ide/ +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ # MSTest test Results [Tt]est[Rr]esult*/ [Bb]uild[Ll]og.* -#NUNIT +# NUNIT *.VisualState.xml TestResult.xml @@ -34,6 +44,11 @@ TestResult.xml [Rr]eleasePS/ dlldata.c +# DNX +project.lock.json +project.fragment.lock.json +artifacts/ + *_i.c *_p.c *_i.h @@ -66,14 +81,18 @@ _Chutzpah* ipch/ *.aps *.ncb +*.opendb *.opensdf *.sdf *.cachefile +*.VC.db +*.VC.VC.opendb # Visual Studio profiler *.psess *.vsp *.vspx +*.sap # TFS 2012 Local Workspace $tf/ @@ -86,7 +105,7 @@ _ReSharper*/ *.[Rr]e[Ss]harper *.DotSettings.user -# JustCode is a .NET coding addin-in +# JustCode is a .NET coding add-in .JustCode # TeamCity is a build add-in @@ -95,9 +114,14 @@ _TeamCity* # DotCover is a Code Coverage Tool *.dotCover +# Visual Studio code coverage results +*.coverage +*.coveragexml + # NCrunch _NCrunch_* .*crunch*.local.xml +nCrunchTemp_* # MightyMoose *.mm.* @@ -125,39 +149,63 @@ publish/ # Publish Web Output *.[Pp]ublish.xml *.azurePubxml -# TODO: Comment the next line if you want to checkin your web deploy settings +# TODO: Comment the next line if you want to checkin your web deploy settings # but database connection strings (with potential passwords) will be unencrypted *.pubxml *.publishproj +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + # NuGet Packages *.nupkg # The packages folder can be ignored because of Package Restore **/packages/* # except build/, which is used as an MSBuild target. !**/packages/build/ -# If using the old MSBuild-Integrated Package Restore, uncomment this: +# Uncomment if necessary however generally it will be regenerated when needed #!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable files +*.nuget.props +*.nuget.targets -# Windows Azure Build Output +# Microsoft Azure Build Output csx/ *.build.csdef -# Windows Store app package directory +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ # Others -sql/ -*.Cache ClientBin/ -[Ss]tyle[Cc]op.* ~$* *~ *.dbmdl *.dbproj.schemaview +*.jfm *.pfx *.publishsettings node_modules/ +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ # RIA/Silverlight projects Generated_Code/ @@ -182,4 +230,194 @@ UpgradeLog*.htm # Microsoft Fakes FakesAssemblies/ -/.vs/DbContextScope/v15/sqlite3 +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/ + +### VisualStudio ### +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files + +# User-specific files (MonoDevelop/Xamarin Studio) + +# Build results + +# Visual Studio 2015 cache/options directory +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results + +# NUNIT + +# Build Results of an ATL Project + +# .NET Core +**/Properties/launchSettings.json + + +# Chutzpah Test files + +# Visual C++ cache files + +# Visual Studio profiler + +# TFS 2012 Local Workspace + +# Guidance Automation Toolkit + +# ReSharper is a .NET coding add-in + +# JustCode is a .NET coding add-in + +# TeamCity is a build add-in + +# DotCover is a Code Coverage Tool + +# Visual Studio code coverage results + +# NCrunch + +# MightyMoose + +# Web workbench (sass) + +# Installshield output folder + +# DocProject is a documentation generator add-in + +# Click-Once directory + +# Publish Web Output +# TODO: Uncomment the next line to ignore your web deploy settings. +# By default, sensitive information, such as encrypted password +# should be stored in the .pubxml.user file. +#*.pubxml +*.pubxml.user + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted + +# NuGet Packages +# The packages folder can be ignored because of Package Restore +# except build/, which is used as an MSBuild target. +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignorable files + +# Microsoft Azure Build Output + +# Microsoft Azure Emulator + +# Windows Store app package directories and files + +# Visual Studio cache files +# files ending in .cache can be ignored +# but keep track of directories ending in .cache + +# Others + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) + +# SQL Server files +*.ndf + +# Business Intelligence projects + +# Microsoft Fakes + +# GhostDoc plugin setting file + +# Node.js Tools for Visual Studio + +# Typescript v1 declaration files +typings/ + +# Visual Studio 6 build log + +# Visual Studio 6 workspace options file + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output + +# Paket dependency manager + +# FAKE - F# Make + +# JetBrains Rider + +# CodeRush + +# Python Tools for Visual Studio (PTVS) + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +### VisualStudio Patch ### +# By default, sensitive information, such as encrypted password +# should be stored in the .pubxml.user file. + + +# End of https://www.gitignore.io/api/aspnetcore,visualstudio diff --git a/DbContextScope/CallContext.cs b/DbContextScope/CallContext.cs new file mode 100644 index 0000000..24d321d --- /dev/null +++ b/DbContextScope/CallContext.cs @@ -0,0 +1,32 @@ +using System.Collections.Concurrent; +using System.Threading; + +namespace EntityFrameworkCore.DbContextScope +{ + + /// + /// Provides a way to set contextual data that flows with the call and + /// async context of a test or invocation. + /// http://www.cazzulino.com/callcontext-netstandard-netcore.html + /// + internal static class CallContext + { + static ConcurrentDictionary> state = new ConcurrentDictionary>(); + + /// + /// Stores a given object and associates it with the specified name. + /// + /// The name with which to associate the new item in the call context. + /// The object to store in the call context. + public static void SetData(string name, object data) => + state.GetOrAdd(name, _ => new AsyncLocal()).Value = data; + + /// + /// Retrieves an object with the specified name from the . + /// + /// The name of the item in the call context. + /// The object in the call context associated with the specified name, or if not found. + public static object GetData(string name) => + state.TryGetValue(name, out AsyncLocal data) ? data.Value : null; + } +} \ No newline at end of file diff --git a/DbContextScope/DbContextScope.csproj b/DbContextScope/DbContextScope.csproj index e87ad91..97f4e14 100644 --- a/DbContextScope/DbContextScope.csproj +++ b/DbContextScope/DbContextScope.csproj @@ -1,7 +1,7 @@ - net461 + netstandard2.0 EntityFrameworkCore.DbContextScope @@ -22,7 +22,7 @@ - + diff --git a/DbContextScope/Implementations/DbContextScope.cs b/DbContextScope/Implementations/DbContextScope.cs index c67f75f..b946918 100644 --- a/DbContextScope/Implementations/DbContextScope.cs +++ b/DbContextScope/Implementations/DbContextScope.cs @@ -12,7 +12,6 @@ using System.Data; using System.Linq; using System.Runtime.CompilerServices; -using System.Runtime.Remoting.Messaging; using System.Threading; using System.Threading.Tasks; @@ -400,13 +399,13 @@ internal static void SetAmbientScope(DbContextScope newAmbientScope) { throw new ArgumentNullException("newAmbientScope"); //return Thread.CurrentThread.GetExecutionContextReader().LogicalCallContext.GetData(name); - var current = CallContext.LogicalGetData(AmbientDbContextScopeKey) as InstanceIdentifier; + var current = CallContext.GetData(AmbientDbContextScopeKey) as InstanceIdentifier; if (current == newAmbientScope._instanceIdentifier) return; // Store the new scope's instance identifier in the CallContext, making it the ambient scope - CallContext.LogicalSetData(AmbientDbContextScopeKey, newAmbientScope._instanceIdentifier); + CallContext.SetData(AmbientDbContextScopeKey, newAmbientScope._instanceIdentifier); // Keep track of this instance (or do nothing if we're already tracking it) DbContextScopeInstances.GetValue(newAmbientScope._instanceIdentifier, key => newAmbientScope); @@ -417,8 +416,8 @@ internal static void SetAmbientScope(DbContextScope newAmbientScope) { /// Call this when a DbContextScope is being disposed. /// internal static void RemoveAmbientScope() { - var current = CallContext.LogicalGetData(AmbientDbContextScopeKey) as InstanceIdentifier; - CallContext.LogicalSetData(AmbientDbContextScopeKey, null); + var current = CallContext.GetData(AmbientDbContextScopeKey) as InstanceIdentifier; + CallContext.SetData(AmbientDbContextScopeKey, null); // If there was an ambient scope, we can stop tracking it now if (current != null) { @@ -431,7 +430,7 @@ internal static void RemoveAmbientScope() { /// hide the ambient context (e.g. to prevent it from being captured by parallel task). /// internal static void HideAmbientScope() { - CallContext.LogicalSetData(AmbientDbContextScopeKey, null); + CallContext.SetData(AmbientDbContextScopeKey, null); } /// @@ -439,7 +438,7 @@ internal static void HideAmbientScope() { /// internal static DbContextScope GetAmbientScope() { // Retrieve the identifier of the ambient scope (if any) - var instanceIdentifier = CallContext.LogicalGetData(AmbientDbContextScopeKey) as InstanceIdentifier; + var instanceIdentifier = CallContext.GetData(AmbientDbContextScopeKey) as InstanceIdentifier; if (instanceIdentifier == null) return null; // Either no ambient context has been set or we've crossed an app domain boundary and have (intentionally) lost the ambient context diff --git a/Demo/Demo.csproj b/Demo/Demo.csproj index fb56285..c5751f4 100644 --- a/Demo/Demo.csproj +++ b/Demo/Demo.csproj @@ -2,15 +2,15 @@ Exe - net461 + netstandard2.0 - + - + \ No newline at end of file diff --git a/Demo/Program.cs b/Demo/Program.cs index 9512646..b2de42f 100644 --- a/Demo/Program.cs +++ b/Demo/Program.cs @@ -1,6 +1,6 @@ using EntityFrameworkCore.DbContextScope; using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Diagnostics; using Numero3.EntityFramework.Demo.BusinessLogicServices; using Numero3.EntityFramework.Demo.CommandModel; using Numero3.EntityFramework.Demo.DatabaseContext;