User-1835425756 posted
hello, i am new to wcf and mvc and unityframework.
i want to call the method of the businesslayer from the wcf service.
but the irepository class is not initialized and gives null ref. error.
here is the code:
WCF service code:
public class GarageService : IGarageService
{
private readonly IGarageRepository GarageRepository;
public IEnumerable<ErrorMessage> AddGarage(Garage.Domain.ModelClass.Garage Garages)
{
return GarageRepository.AddGarage(Garages);
}
the interface of the wcf code:
[ServiceContract]
public interface IGarageService
{
[OperationContract]
IEnumerable<ErrorMessage> AddGarage(Garage.Domain.ModelClass.Garage Garage);
}
now my businesslayer code where the repository resides as follows:
public interface IGarageManagerService
{
IEnumerable<ErrorMessage> AddGarage(Garage.Domain.ModelClass.Garage Garage);
}
/// <summary>
///
/// </summary>
public class GarageManager : IGarageManagerService
{
/// <summary>
/// IGarageRepository's instance
/// </summary>
private readonly IGarageRepository GarageRepository;
/// <summary>
/// IUnitOfWork's instance
/// </summary>
private readonly IUnitOfWork unitOfWork;
/// <summary>
/// GarageManager class's constructor
/// </summary>
/// <param name="GarageRepository"></param>
/// <param name="unitOfWork"></param>
public GarageManager(IGarageRepository GarageRepository, IUnitOfWork unitOfWork)
{
this.GarageRepository = GarageRepository;
this.unitOfWork = unitOfWork;
}
}
i also added global.asax page as follows:
protected void Application_Start()
{
IUnityContainer container = GetUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
/// <summary>
/// Gets Unity Container
/// </summary>
private IUnityContainer GetUnityContainer()
{
//Create UnityContainer
IUnityContainer container = new UnityContainer()
//.RegisterType<IControllerActivator, CustomControllerActivator>() // No nned to a controller activator
.RegisterType<IDatabaseFactory, DatabaseFactory>(new HttpContextLifetimeManager<IDatabaseFactory>())
.RegisterType<IUnitOfWork, UnitOfWork>(new HttpContextLifetimeManager<IUnitOfWork>())
.RegisterType<IGarageRepository, GarageRepository>(new HttpContextLifetimeManager<IGarageRepository>())
.RegisterType<IGarageManagerService, GarageManager>(new HttpContextLifetimeManager<IGarageManagerService>());
//.RegisterType<ICountryRepository, CountryRepository>(new HttpContextLifetimeManager<ICountryRepository>())
// .RegisterType<ICountryService, CountryService>(new HttpContextLifetimeManager<ICountryService>());
return container;
}
i got error in wcf garageservice code.
pls help me, how to instantiate the repository.