Hello Friends,
I have one issue to access instance of service on static webmethod to call qjeruy ajax method. I have enitity code first approch and
generic repository pattern implemented in the project.
Following are the code structure.
Repository Class:
public partial class EfRepository<T> : IRepository<T> where T : BaseEntity, new()
{
private readonly IDbContext _context;
private IDbSet<T> _entities;
public EfRepository(IDbContext context)
{
this._context = context;
}
public T GetById(object id)
{
return this.Entities.Find(id);
}
}
Services Class:
public partial class UserService : IUserService
{
private IRepository<User> repositoryUser;
public UserService(IRepository<User> _repositoryUser)
{
repositoryUser = _repositoryUser;
}
}
Global Application:
void Application_Start(object sender, EventArgs e)
{
Autofac.ContainerBuilder builder = new Autofac.ContainerBuilder();
builder.Register<IDbContext>(c => new DBObjectContext(ConnectionString)).InstancePerHttpRequest();
builder.RegisterType<UserService>().As<IUserService>().InstancePerHttpRequest();
builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope();
_containerProvider = new ContainerProvider(builder.Build());
}
userList.aspx.cs class
public IUserService userService { get; set; }
Now My requirement is how to access this object in static webmethod which is called through jquery ajax method.
[WebMethod]
public static string MyMethod()
{
}
1. If I can mark object as static then object is not created on load,
I did following trick which is working fine but exception thrown as DbContext is disposed.
I created static object as well.
protected static IUserService userStaticService { get; set; }
and on page load event of page, I assign object to static object.
protected void Page_Load(object sender, EventArgs e)
{
userStaticService = userService;
}
and I can access all methods in static web method by using static service object but as I told DBContext is disposed exception is thrown.
Is there any work around to achive my goal? I thinking to create webservice class in class library and write all webmethod withing in that class
which are not static method so I can easily create an object of service interface.
Any guide or tutorial would be greatly appreciate.