Open Closed

How to access services from controller #240


0
naeazach created

So I've added a geo service that has some handy geo lookup utils in it. What I'm trying to do from the home controller on a form submit to do the following.

        MyApp.Programs.IGeoAppService ga = new Programs.GeoAppService(new IRepository<Programs.Geo>());
        ga.GetCityFromZip(new GetGeoInput { Zip = "85210" });

but obviously new IRepository<Programs.Geo>()) is not allowed. Can you tell me how to properly access the service from a MVC controller?

Also, it would be handy if there was a way that abp could allow some publicly exposed services before the angular stuff comes in. is there some sort of config to do that?


3 Answer(s)
  • 0
    naeazach created

    So I tried this, but the action inside the service is error that DbContext has already been disposed.

    private NAEA.DownPayment.Programs.IGeoAppService _geoService; public HomeController( IRepository

        public ActionResult Index()
        {
            _geoService.GetCityFromZip(new GetGeoInput { Zip = "85210" });
            return View();
        }
    
  • 0
    naeazach created

    well i figured it out.

            var city = _geoRepository.Query<Geo>(a => a.Where(aa=> aa.Zip == input.Zip).FirstOrDefault());
    

    cant use getall.

    However, I did have my ActionResult annotated with [UnitOfWork]... why didnt that work? How do I ensure that db connection is open when coming into the repo?

  • 0
    hikalkan created

    Hi,

    Never intantiate a class directly, always use dependency injection. See docs: http://www.aspnetboilerplate.com/Pages/ ... -Injection

    So, in controller constructor, you can inject your service and use it (as you did for repository in service).