![]() ![]() ![]() Once we have our scoped ITenant service, register a pooling context factory as a Singleton service, as usual: ( Return tenantIdString != StringValues.Empty & int.TryParse(tenantIdString, out var tenantId)Īs written above, pay special attention to where you get the tenant ID from - this is an important aspect of your application's security. Var tenantIdString = sp.GetRequiredService(). In a real application, the tenant ID would be set based on secure authentication data.ī() In this sample, we simply accept the tenant ID as a request query, which means that a client can impersonate any ![]() Let's assume that your application registers a scoped ITenant service, which wraps the tenant ID and any other tenant-related information: // Below is a minimal tenant resolution strategy, which registers a scoped ITenant service in DI. Since the tenant ID needs to change with each web request, we need to go through some extra steps to make it all work with context pooling. a tenant ID).Ī typical scenario involving context state would be a multi-tenant ASP.NET Core application, where the context instance has a tenant ID which is taken into account by queries (see Global Query Filters for more details). Crucially, the context's OnConfiguring is only invoked once - when the instance context is first created - and so cannot be used to set state which needs to vary (e.g. This means that special care must be taken when the context involves any state that may change between requests. MethodĬontext pooling works by reusing the same context instance across requests this means that it's effectively registered as a Singleton, and the same instance is reused across multiple requests (or DI scopes). The source code is available here, feel free to use it as a basis for your own measurements. Importantly, this benchmarks single-threaded pooling performance, while a real-world contended scenario may have different results benchmark on your platform before making any decisions. As always, results will change with the number of rows, the latency to your database server and other factors. Benchmarksįollowing are the benchmark results for fetching a single row from a SQL Server database running locally on the same machine, with and without context pooling. Once poolSize is exceeded, new context instances are not cached and EF falls back to the non-pooling behavior of creating instances on demand. The poolSize parameter of the PooledDbContextFactory constructor sets the maximum number of instances retained by the pool (defaults to 1024). Using (var context = factory.CreateDbContext()) To use context pooling without dependency injection, initialize a PooledDbContextFactory and request context instances from it: var options = new factory = new PooledDbContextFactory(options) The poolSize parameter of AddDbContextPool sets the maximum number of instances retained by the pool (defaults to 1024). O => o.UseSqlServer(("WeatherForecastContext"))) To enable context pooling, simply replace AddDbContext with AddDbContextPool: ( Then, instances of that type are obtained through constructor parameters in controllers or Razor Pages. ![]() You may find it helpful to keep the default timeout for most queries and catch the error to give you insight into database problems.The typical pattern in an ASP.NET Core app using EF Core involves registering a custom DbContext type into the dependency injection container via AddDbContext. To keep from having to put this code in every query, you could call it when you instantiate the dbContext. If you are using ObjectContext, you can do without the adapter, like this: I’m not sure why Microsoft didn’t keep the simpler syntax from the older version of ObjectContext for dbContext. This line of code moves you down from dbContext to the underlying ObjectContext to make the change. Of course, you can use any number of seconds you need. ((IObjectContextAdapter)Context).ObjectContext.CommandTimeout = 180 If you are using dbContext, the solution to this is to add the following code before your query to set the timeout to a longer value: If your query runs longer than that, you are going to get the error above. The timeout period elapsed prior to completion of the operation or the server is not responding.”Īs near as I can find, the default timeout for EF queries using the SQL Server 2008 connection provider is 30 seconds. You have a long running Entity Framework query that often creates a “” with the message “Timeout expired. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |