I Use Autofac and asp.net MVC 5, and Generic Repository pattern
i create Custom RoleProvider
Custom RoleProvider
namespace Web.Utility.Classes
{
class GoldenBodyProvider : RoleProvider
{
private readonly IUnitOfWork _unitOfWork;
private readonly IService _service;
public GoldenBodyProvider()
{
}
public GoldenBodyProvider(IUnitOfWork unitOfWork, IService service)
{
_unitOfWork = unitOfWork;
_service = service;
}
public override bool IsUserInRole(string username, string roleName)
{
throw new NotImplementedException();
}
public override string[] GetRolesForUser(string username)
{
var role = (from u in _service.UserRepository.GetAll()
join r in _service.RoleRepository.GetAll() on u.RoleId equals r.Id
where u.MobileNumber == username
select r.RoleInSystem).ToArray();
return role;
}
public override void CreateRole(string roleName)
{
throw new NotImplementedException();
}
public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
{
throw new NotImplementedException();
}
public override bool RoleExists(string roleName)
{
throw new NotImplementedException();
}
public override void AddUsersToRoles(string[] usernames, string[] roleNames)
{
throw new NotImplementedException();
}
public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
{
throw new NotImplementedException();
}
public override string[] GetUsersInRole(string roleName)
{
throw new NotImplementedException();
}
public override string[] GetAllRoles()
{
throw new NotImplementedException();
}
public override string[] FindUsersInRole(string roleName, string usernameToMatch)
{
throw new NotImplementedException();
}
public override string ApplicationName { get; set; }
}
}
In one of the Views called User.IsInRole("Coach")
(The GetRolesForUser
method is called in RoleProvider) . after that , in GetRolesForUser
method in RoleProvider Gives me an error:
Object reference not set to an instance of an object.
The error pointed to _service.UserRepository.GetAll()
,this method is through autofact inject to RoleProvider
Source Error:
Line 31: public override string[] GetRolesForUser(string username)
Line 32: {
Line 33: var role = (from u in _service.UserRepository.GetAll()
Line 34: join r in _service.RoleRepository.GetAll() on u.RoleId equals r.Id
Line 35: where u.MobileNumber == username
Source File: I:\GBWork\GoldenBody\Web\Utility\Classes\GoldenBodyProvider.cs Line: 33
GetAll()
method in GenericRepository pattern is :
public virtual List<T> GetAll()
{
return _t.AsParallel().ToList();
}
I think the injections do not work properly.
autofac configuration:
#region Autofac
var builder = new ContainerBuilder();
#region Registery
#region MVC Register
//This allows you to add properties to your filter attributes and any matching dependencies that are registered in the container will be injected into the properties.
builder.RegisterFilterProvider();
// You can register controllers all at once using assembly scanning...
builder.RegisterControllers(typeof(Global).Assembly);
builder.RegisterModule<AutofacWebTypesModule>();
builder.RegisterAssemblyTypes(Assembly.Load("ServiceLayer"))
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces()
.InstancePerRequest();
builder.RegisterType(typeof(GoldenBodyDb)).As(typeof(IUnitOfWork)).InstancePerRequest();
#endregion
#region WebApi
builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); //Register WebApi Controllers
//زیرا کنتلر های وب ای پی آی در همین اسمبلی می باشد
#endregion
#endregion
var container = builder.Build();
// Set the dependency resolver for Web API.
var webApiResolver = new AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = webApiResolver;
// Set the dependency resolver for MVC.
var mvcResolver = new AutofacDependencyResolver(container);
DependencyResolver.SetResolver(mvcResolver);
#endregion
Where have I made a mistake? Where is my problem? what's the solution ?