This definitely appears to be a different issue than the Multithreading issue.
Using Unity with Dependency Injection(DI) for all my services and using an entity framework context which appears to be initialized and working correctly.
Currently using .net 4.5 with mvc and Unity.WebApi and entity framework 6.0.0.0.
I want to know if its possible to persist a service in this model or use outside of DI and be able to close and re-initialize entity framework in the persisted service if possible.
Currently getting this error when I try to persist a service through a stack class so its performance will be very fast b/c I'm using more frequently.
The Error message I am getting is: Error Message: The underlying provider failed on Open. - The connection was not closed. The connection's current state is connecting.( at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
Purpose is to retrieve only a single entity and make it load very fast compared to the dependency injected version and expose the results through a service. The entity is tied to a sql table for logging purposes.
Here is code snippet of why I'm trying to do to persist service:
using System;
using System.Linq;
using Davinci.Models;
namespace Services.Services
{
public interface ILogService
{
void SaveLog(UserLog log);
UserLog RetreiveLog(DateTime dateTime);
}
public class LogService : ILogService
{
private DavinciEntities _DavinciEntities;
public LogService(DavinciEntities context)
{
_DavinciEntities = context;
}
private void SaveLog(UserLog log)
{
_DavinciEntities.UserLogs.Add(log);
_DavinciEntities.SaveChanges();
}
public UserLog RetreiveLog(DateTime dateTime)
{
return _DavinciEntities.UserLogs.Where(m => m.LogTime.ToShortDateString() == dateTime.ToShortDateString()).FirstOrDefault();
}
}
public static class PersistService
{
public static UserLogService userLogService {get; set;}
public static void PersistUserLog(UserLogService service)
{
IUserLogService UserLogService;
if (UserAccessLog == null)
{
UserLogService = (UserLogService)context.Configuration.DependencyResolver.GetService(typeof(UserLogService));
}
}
}
}
Since I had stored a copy of my service with my entity's DBContext it was failing because it was being used by multiple threads. The fix was to new it up each time because it had originally came from a Unity DI resolver method and the entity doesn't get newed up each time the stored service and its methods are called.
Its unknown if the current context can be cleared up a different or better way to make it perform faster when finished with a specific use case scenario in a particular web api thread or end point instead of newing it up in each method call of the saved service.
Solution every time its stored in a static variable and reused on each method:
// some service
public class SomeSerivce : ISomeService
{
...
// when the service is stored must use new context liberally
public void SomeStaticMethod() {
_DavinciEntities = new DavinciEntities();
... // work
}
}
An example of where this might be needed would be filter or global filter.