Norm.MongoException: Connection timeout trying to get connection from connection pool - asp.net-mvc

I'm using Rob's mvc startesite http://mvcstarter.codeplex.com/ with ASP.Net MVC 2, Ninject2, NoRM (http://github.com/atheken/NoRM) and MongoDB. It works so fast and the developpement is even faster but I'm facing a big problem, I at some points, get connection timeout. I can't figure out what I'm doing wrong.
I already asked a question here : I get this error that I don't understand why, using NoRM and Mongo in my MVC project and here http://groups.google.com/group/norm-mongodb/browse_thread/thread/7882be16f030eb29 but I still in the dark.
Thanks a lot for the help!
EDITED*
Here's my MongoSession object :
public class MongoSession : ISession{
private readonly Mongo _server;
public MongoSession()
{
//this looks for a connection string in your Web.config - you can override this if you want
_server = Mongo.Create("MongoDB");
}
public T Single<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) where T : class {
return _server.GetCollection<T>().AsQueryable().Where(expression).SingleOrDefault();
}
public IQueryable<T> All<T>() where T : class {
return _server.GetCollection<T>().AsQueryable();
}
public void Save<T>(IEnumerable<T> items) where T : class {
foreach (T item in items) {
Save(item);
}
}
public void Save<T>(T item) where T : class {
var errors = DataAnnotationsValidationRunner.GetErrors(item);
if (errors.Count() > 0)
{
throw new RulesException(errors);
}
_server.Database.GetCollection<T>().Save(item);
}
public void Delete<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) where T : class
{
var items = All<T>().Where(expression);
foreach (T item in items)
{
Delete(item);
}
}
public void Delete<T>(T item) where T : class
{
_server.GetCollection<T>().Delete(item);
}
public void Drop<T>() where T : class
{
_server.Database.DropCollection(typeof(T).Name);
}
public void Dispose() {
_server.Dispose();
}
}
And now my MongoRepositoryBase
public abstract class MongoRepositoryBase<T> : ISession<T> where T : MongoObject
{
protected ISession _session;
protected MongoRepositoryBase(ISession session)
{
_session = session;
}
public T Single(ObjectId id)
{
return _session.All<T>().Where(x => x.Id == id).FirstOrDefault();
}
public T Single(Expression<Func<T, bool>> expression)
{
return _session.Single(expression);
}
public IQueryable<T> All()
{
return _session.All<T>();
}
public void Save(IEnumerable<T> items)
{
foreach (T item in items)
{
Save(item);
}
}
public void Save(T item)
{
_session.Save(item);
}
public void Delete(System.Linq.Expressions.Expression<Func<T, bool>> expression)
{
var items = _session.All<T>().Where(expression);
foreach (T item in items)
{
Delete(item);
}
}
public void DeleteAll()
{
var items = _session.All<T>();
foreach (T item in items)
{
Delete(item);
}
}
public void Delete(T item)
{
_session.Delete(item);
}
public void Drop()
{
_session.Drop<T>();
}
public void Dispose()
{
_session.Dispose();
}
}
And an exemple of an other Repository implemantation :
public class PlaceRepository : MongoRepositoryBase<Place>, IPlaceRepository
{
public PlaceRepository(ISession session) : base(session)
{
}
public List<Place> GetByCategory(PlaceCategory category, bool publishedOnly)
{
var query = _session.All<Place>()
.OrderBy(x => x.Name)
.Where(x => x.Category == category);
if (publishedOnly) query = query.Where(x => x.Published);
if (publishedOnly) query = query.Where(x => x.ShowOnMap);
return query.ToList();
}
public Place FindByName(string name)
{
var query = _session.All<Place>()
.Where(x => x.Name.ToLower().Contains(name.ToLower()))
.Where(x => x.Published);
return query.FirstOrDefault();
}
public string[] FindSuggestionsByName(string name)
{
var query = _session.All<Place>()
.OrderBy(x => x.Name)
.Where(x => x.Name.ToLower().StartsWith(name.ToLower()))
.Where(x => x.Published);
var places = query.ToList();
var names = new string[places.Count];
var i = 0;
foreach (var place in places)
{
names[i++] = place.Name;
}
return names;
}
}

Vinny,
I've never used Ninject, so I could be way off with this suggestion. But it seems possible that having a static MongoSession instance might be holding connections open. Have you tried TransientBehavior instead of SingletonBehavior? Or maybe change your code to call Dispose (or use using) after you convert your ShortcutLinks to a List? All
var shortcutLionks = _session.All<ShortcutLinks>().ToList();
_session.Dispose();
A better approach might be to use some sort of repository or DAO where the session details are hidden from the controller. I have a RepositoryBase sample at http://www.codevoyeur.com/Articles/20/A-NoRM-MongoDB-Repository-Base-Class.aspx.
Stuart Harris has a similar, arguably more complete implementation at http://red-badger.com/Blog/post/A-simple-IRepository3cT3e-implementation-for-MongoDB-and-NoRM.aspx
Pooled MongoDB connections are relatively cheap to create, so it's probably best to make sure the data access methods are disposing after your done getting/saving data.

If I add throw new NotImplementedException(); in the Dispose() method of my MongoRepositoryBase class it does not get call so I guess Ninject does not handle this for me, If I had
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
_recipeRepo.Dispose();
base.OnActionExecuted(filterContext);
}
In my controller it does get call. It seems to be fine, thx!

Related

Interface with more implementation and Dependency Injection

I created a project with .net Core 2.
Now I have a List of classes from the same interface which I needed at runtime.
My problem is, I can't add this classes to the servicecollection (only one interface). So I don't have access to the other services in those classes. Also I think it wouldn't solve it.
I could create a singleton/static class with my servicecollection and use the IServiceProvider to get those other services from there, but I think that isn't the best practice.
Here is an example of my problem:
public class Manager : IManager
{
private IList<IMyService> _myService;
public Manager()
{
IList<Type> types = GetIMyServiceTypes();
foreach (Type type in types)
{
var instance = (IMyService)Activator.CreateInstance(type);
_myService.Add(instance)
}
}
public IList<bool> IsTrue()
{
return _myService
.Select(se => se.IsTrue())
.ToList();
}
public IList<Type> GetIMyServiceTypes()
{
var type = typeof(IMyService);
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p))
.ToList();
return types;
}
}
public class ServiceType1: IMyService
{
public bool IsTrue()
{
//Need Access to IServiceCollection Services
}
}
public interface IMyService
{
bool IsTrue();
}
public class MyController : Controller
{
private IManager _amanager;
public MyController(IManager manager)
{
_manager = manager
}
public IActionResult IsTrue()
{
IList<bool> isTrue =_manager.IsTrue();
return new ObjectResult(isTrue);
}
}
Is there a pattern, which I could use to solve my problem? Is there a best practice to have access to the services without using them in the constructor?
I found the solution on another post in stackoverflow https://stackoverflow.com/a/44177920/5835745
But I will post my changes for other people with the same problem. I loaded the list of classes from the configuration, but it's also possible to add all classes.
public class Manager : IManager
{
private IList<IMyService> _myService;
private readonly Func<string, IService> _serviceAccessor;
public Manager (Func<string, IService> serviceAccessor)
{
IList<string> authentications = new List<string> {"value1", "value2"}
foreach (string authentication in authentications)
{
AddAuthentication(_serviceAccessor(authentication));
}
}
public IList<bool> IsTrue()
{
return _myService
.Select(se => se.IsTrue())
.ToList();
}
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<Value1>();
services.AddTransient<Value2>();
services.AddTransient(factory =>
{
Func<string, IService> accesor = key =>
{
switch (key)
{
case "value1":
return factory.GetService<Value1>();
case "value2":
return factory.GetService<Value2>();
default:
throw new KeyNotFoundException();
}
};
return accesor;
});
}
}

Refactor IUnitOfWork

Premisse:
I am folowing IUnitOfWork Patterns I created a Base class with my methods to persist data.
My Problem:
I found a problem to write a method Next Number of table from SQLServer, because of this, I am repeating this method in every class .
Classes:
BaseContext Class:
public class BaseContext<T> : DbContext where T : class
{
public DbSet<T> DbSet
{
get;
set;
}
public BaseContext() : base("DefaultConnection")
{
Database.SetInitializer<BaseContext<T>>(null);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
// Class Mapping from IMapping
var typesToMapping = (from x in Assembly.GetExecutingAssembly().GetTypes()
where x.IsClass && typeof(IMapping).IsAssignableFrom(x)
select x).ToList();
foreach (var mapping in typesToMapping)
{
dynamic mappingClass = Activator.CreateInstance(mapping);
modelBuilder.Configurations.Add(mappingClass);
}
}
public virtual void ChangeObjectState(object model, EntityState state)
{
((IObjectContextAdapter)this)
.ObjectContext
.ObjectStateManager
.ChangeObjectState(model, state);
}
// Implement IUnitOfWork
public virtual int Save(T model)
{
this.DbSet.Add(model);
this.ChangeObjectState(model, EntityState.Added);
return this.SaveChanges();
}
public virtual int Update(T model, int id)
{
var entity = DbSet.Find(id);
this.Entry(entity).CurrentValues.SetValues(model);
return this.SaveChanges();
}
public virtual void Delete(T model)
{
var entry = this.Entry(model);
if (entry.State == EntityState.Detached)
this.DbSet.Attach(model);
this.ChangeObjectState(model, EntityState.Deleted);
this.SaveChanges();
}
public virtual IEnumerable<T> GetAll()
{
return this.DbSet.ToList();
}
public virtual T GetById(object id)
{
return this.DbSet.Find(id);
}
public virtual IEnumerable<T> Where(Expression<Func<T, bool>> expression)
{
return this.DbSet.Where(expression);
}
public IEnumerable<T> OrderBy(Expression<Func<T, bool>> expression)
{
return this.DbSet.OrderBy(expression);
}
}
My method NextNumber in every class:
public class VersionDao : BaseContext<Version>, IUnitOfWork<Version>
{
public int Next() => DbSet.Max(x => x.VersionId) + 1;
}
public class TicketDao : BaseContext<ViewModelTicket> , IUnitOfWork<ViewModelTicket>
{
public int Next() => DbSet.Max(x => x.TicketId) + 1;
}
public class CompanyDao : BaseContext<Company>, IUnitOfWork<Company>
{
public int Next() => DbSet.Max(x => x.CompanyId) + 1;
}
Solicitation:
I need a suggestions to stop repeatition on Next method in every class.
Thank you
There is no need to implement Unit of Work pattern if you are using Entity Framework. If you click F12/Go To Definition on DbContext you'll see the following summary
A DbContext instance represents a combination of the Unit Of Work and
Repository patterns such that it can be used to query from a database
and group together changes that will then be written back to the store
as a unit. DbContext is conceptually similar to ObjectContext.

Issue in Connecting Web App with localdb using DataContext file and webConfig file

I have created MVC web application using Repository & DI approach. I have used Code First approach too.
Here is my DataContext file:
namespace EfRepPatTest.Data
{
public class DataContext : DbContext, IDbContext
{
public new IDbSet<TEntity> Set<TEntity>() where TEntity: class
{
return base.Set<TEntity>();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
.Where(type => !String.IsNullOrEmpty(type.Namespace))
.Where(type => type.BaseType != null && type.BaseType.IsGenericType &&
type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
foreach (var type in typesToRegister)
{
dynamic configurationInstance = Activator.CreateInstance(type);
modelBuilder.Configurations.Add(configurationInstance);
}
base.OnModelCreating(modelBuilder);
}
}
}
I have defined connecting string in Web.Config file like below:
<add name="DataContext"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\eCommerce.mdf;Integrated Security=True"
providerName="System.Data.SqlClient"
/>
Please note here I have mentioned same name to the Connection string and my context file.
Here is my post method:
[HttpPost]
public ActionResult Create(CategoryModel model)//FormCollection collection
{
try
{
// TODO: Add insert logic here
if (model == null)
return View(model);
var category = new Category();
category.Name = model.Name;
categoryService.Insert(category);
return RedirectToAction("Index");
}
catch
{
return View(model);
}
}
CategoryService:
public class CategoryService : ICategoryService
{
private IRepository<Category> _categoryRepository;
public CategoryService(IRepository<Category> categoryRepository)
{
this._categoryRepository = categoryRepository;
}
public void Insert(Category category)
{
if (category == null)
throw new ArgumentNullException("Category");
_categoryRepository.Insert(category);
}
}
RepositoryService:
public class RepositoryService<TEntity> : IRepository<TEntity> where TEntity: class
{
private IDbContext _context;
private IDbSet<TEntity> Entities
{
get { return this._context.Set<TEntity>(); }
}
public RepositoryService(IDbContext context)
{
this._context = context;
}
public void Insert(TEntity entity)
{
Entities.Add(entity);
}
}
When I run application on the first time it will create local db. But when I going to insert data, I did not get any error from the application and it does not insert my data to the DB.
What cause this? What I have done wrong here?
Any help is appreciated!
You should call SaveChanges() on _context after all changes like this for ex.:
public void Insert(TEntity entity)
{
Entities.Add(entity);
_context.SaveChanges();
}

How to identify what Session Ninject should bind

Using for the first time: Asp.NET MVC, NHibernate(FNH), DI using Ninject. I was able to get everything working with one database, but now I'm trying to use two databases (DB1 and DB2 for sake of the eg). I have a dictionary of SessionFactory(s), that is keyed by a database identifier.
I can't figure out how to select the correct Session based on what is being requested from constructor injection. I have seen this How to inject different NHibernate Sessions (multi-db) to same repository with Controller controling which sesions with Ninject, but I didn't get it to work.
public ProductController(DB1.Model.IRepository<Product> prodRepo, DB2.Model.IRepository<Account> acctRepo)
{
[...]
}
NinjectWebCommon.cs snippet
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<ISession>().ToMethod(ctx => NHibernateSessionModule.Provider.GetCurrentSession()).InRequestScope();
kernel.Bind(typeof(DB1.Model.IRepository<>)).To(typeof(NHibernateRepository<>));
kernel.Bind(typeof(DB2.Model.IRepository<>)).To(typeof(NHibernateRepository<>));
}
NHibernateSessionModule.cs: does UOW via Begin/End request
public class NHibernateSessionModule : IHttpModule
{
public static ISessionFactoryProvider Provider = new MultipleSessionFactoryProvider();
public void Dispose() { }
public void Init(HttpApplication context)
{
context.BeginRequest += BeginRequest;
context.EndRequest += EndRequest;
}
public void BeginRequest(object sender, EventArgs e)
{
Provider.BindNew();
}
public void EndRequest(object sender, EventArgs e)
{
Provider.Unbind();
}
}
MultipleSessionFactoryProvider.cs: Not quite sure I'm doing everything here correctly.
public class MultipleSessionFactoryProvider : ISessionFactoryProvider
{
public Dictionary<string, ISessionFactory> SessionFactories { get; private set; }
public static Func<Dictionary<string,ISessionFactory>> InitSessionFactories = GetFactories;
public MultipleSessionFactoryProvider() : this(InitSessionFactories())
{
}
public MultipleSessionFactoryProvider(Dictionary<string, ISessionFactory> factories)
{
SessionFactories = factories;
}
public static Dictionary<string, ISessionFactory> GetFactories()
{
Dictionary<string, ISessionFactory> ret = new Dictionary<string, ISessionFactory>();
Dictionary<string, string> connectionStrings = new Dictionary<string, string>();
connectionStrings.Add(ConfigurationManager.ConnectionStrings["DB1"].Name, ConfigurationManager.ConnectionStrings["DB1"].ConnectionString.ToString());
connectionStrings.Add(ConfigurationManager.ConnectionStrings["DB2"].Name, ConfigurationManager.ConnectionStrings["DB2"].ConnectionString.ToString());
foreach (KeyValuePair<string, string> pair in connectionStrings)
{
//Better way to do the mapping?
ISessionFactory factory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(pair.Value))
.Mappings(cfg => cfg.FluentMappings.Conventions.Setup(x => x.Add(AutoImport.Never()))
.AddFromAssemblyOf<ProductMap>())
.BuildConfiguration()
.CurrentSessionContext<WebSessionContext>().BuildSessionFactory();
ret.Add(pair.Key, factory);
}
return ret;
}
public void BindNew()
{
foreach (KeyValuePair<string, ISessionFactory> factory in SessionFactories)
{
Bind(OpenSession(factory.Key));
}
}
public void Bind(ISession session)
{
CurrentSessionContext.Bind(session);
}
public void Unbind()
{
foreach (KeyValuePair<string, ISessionFactory> factory in SessionFactories)
{
if (CurrentSessionContext.HasBind(factory.Value))
{
var sess = CurrentSessionContext.Unbind(factory.Value);
sess.Dispose();
}
}
}
public ISession OpenSession(string factoryId)
{
return SessionFactories[factoryId].OpenSession();
}
public ISession GetCurrentSession()
{
string factoryId = GetIdentifier(); //<--- How to implement this
return SessionFactories[factoryId].GetCurrentSession();
}
public String GetIdentifier()
{
return "DB1"; //Hardcoded for example
}
So, how can I implement GetIdentifier(), or alter my Ninject binding. I did take a look at Ninject Named binding, but didn't understand how to use that when I bind the Session.
public ProductController([Name("DB1")]DB1.Model.IRepository<Product> prodRepo, [Name("DB2")]DB2.Model.IRepository<Account> acctRepo)
Since this is the first go around with these technologies for me, please let me know if I'm doing anything out of practice, or that might be of concern; I'd like to stay with the HttpModule.
You need two bindings for session, one for each database
kernel.Bind<ISession>().ToMethod(ctx => GetSessionForDB1()).WhenInjectedInto(typeof(DB1.Model.IRepository<>)).InRequestScope();
kernel.Bind<ISession>().ToMethod(ctx => GetSessionForDB2()).WhenInjectedInto(typeof(DB2.Model.IRepository<>)).InRequestScope();

How to execute function in MVC with Entity Framework?

how to use following function
Generic Function:
public T GetSingle(Expression<Func<T, bool>> whereCondition)
{
return this.ObjectSet.Where(whereCondition).FirstOrDefault<>();
}
Business logic wise:
//Now in the following function i would like to call Generic function.
public TabMasterViewModel GetSingle(Expression<Func<TabMasterViewModel, bool>> whereCondition)
{
_tabmasterRepository.GetSingle( .. what should be here.. );
}
//Calling function from Controller level.
public ActionResult Details(int id)
{
return View(_tabmasterService.GetSingle(x => x.colID == id));
}
I could not able to use the function, please suggest.
_tabmasterRepository.GetSingle( .. what should be here.. );
Thanks,
Imdadhusen
Either you modify your first generic function as
public T GetSingle(Expression<Func<T, bool>> whereCondition)
{
return context.CreateObjectSet<T>().Where(whereCondition).FirstOrDefault();
}
or create a genetic repository
public class RepositoryGeneric<TEntity>
{
public RepositoryGeneric(Context context)
{
Context = context;
}
protected ObjectContext Context { get; private set; }
protected virtual ObjectSet<TEntity> ObjectSet
{
get { return Context.CreateObjectSet<TEntity>(); }
}
public virtual TEntity GetByKey(params object[] keys)
{
return DbSet.Find(keys);
}
public TEntity GetSingle(Expression<Func<TEntity, bool>> whereCondition)
{
return ObjectSet.Where(whereCondition).FirstOrDefault();
}
}
Edit:
using the generic function
TabMasterViewModel model = _tabmasterService.GetSingle(x => x.colID == id);
or using generic repository
var tabmasterRepository = new RepositoryGeneric<TabMasterViewModel>(new Context());
var model = tabmasterRepository.GetSingle(x => x.colID == id);

Resources