2

I have a WCF Service with a static data context, but no concurrency. It has been live for a year with no issues, but we saw some strange behavior the other day where users were passing data in correctly, but getting unexpected and incorrect data back. This does pass through an F5, so we're looking at issues there as well.

Would there be any negative impacts to having Single Concurrency Per Session as set-up below with a Static Context?

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContextMode = InstanceContextMode.PerSession)]
public class AgencyUserManagementService : IAgencyUserManagement, IDisposable
{
    private static UserDataContext _dataContext;

    protected static UserDataContext Db
    {
        get { return _dataContext ?? (_dataContext = new UserDataContext()); }
    }
}

public class UserDataContext : DbContext
{
    public UserDataContext()
        : base("Name=UserDataContext")
    {
        Database.SetInitializer<UserDataContext>(null);
    }

    public DbSet<TblAuthor> TblAuthors { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new TblAuthorMap());
    }
}
  • What is UserDataContext? Does it derive from EF DbContext? Can you post source please? – tom redfern Nov 02 '16 at 16:31
  • @Tom - It does. **public class UserDataContext : DbContext { public UserDataContext() : base("Name=UserDataContext") { Database.SetInitializer(null); } public DbSet TblAuthors { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new TblAuthorMap()); } }** – AilingRichMan Nov 02 '16 at 16:37
  • Thank you. I have posted an answer. Yes you are going to run into problems. To be honest I'm surprised that it has taken a year for these problems to emerge – tom redfern Nov 02 '16 at 16:57

1 Answers1

2

Would there be any negative impacts to having Single Concurrency Per Session

The behaviour you will see for a service configured as such will be:

  1. A service instance created per unique caller, and
  2. Each service instance will queue concurrent requests (from the single caller) and process them one by one.

Negative impacts to having a Static Context

It follows then that a static variable defined in such a service will be accessed by the potentially multiple instances of the service.

If you have two concurrent users, then the single static DbContext is being used by both users at the same time.

This could very well lead to some weird problems, such as those you are seeing.

It's not recommended to use DbContext in the manner it is being used in your application for this reason (and others).

You should initialise a new DbContext for each service instance, which will mean the context will no longer be shared across callers. Many IOC containers provide plugins for WCF sessions which may help with this.

Sources:

Community
  • 1
  • 1
tom redfern
  • 30,562
  • 14
  • 91
  • 126