Open Closed

How to access services from controller #240

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<Geo> geoRepository) { _geoService = new GeoAppService(geoRepository); }

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

    well i figured it out.

            var city = _geoRepository.Query&lt;Geo&gt;(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
    Support Team


    Never intantiate a class directly, always use dependency injection. See docs: <a class="postlink" href=""> ... -Injection</a>

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