Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Manoj 2 posts 74 karma points
    Dec 17, 2019 @ 12:08
    Manoj
    2

    ApplicationContext throw error on API services after upgrade to v8

    In umbraco v7 we make use of ApplicationContext which throws an error when upgrading to v8 , and also when we make us of IScope we get an error on getting the cache.

     public abstract class ApiServiceBase
        {
            protected readonly Uri baseUrl;
            protected readonly ApplicationContext appContext;
            protected readonly UmbracoContext umbContext;
            protected readonly IRuntimeCacheProvider cache;
            protected readonly IPublishedContent tradeRoot;
            protected readonly IPublishedContent publicRoot;
    
            public ApiServiceBase(ApplicationContext appContext, UmbracoContext umbContext)
            {
                ApplicationContext applicationContext = appContext;
                if (applicationContext == null)
                    throw new ArgumentNullException(nameof(appContext));
                this.appContext = applicationContext;
                UmbracoContext umbracoContext = umbContext;
                if (umbracoContext == null)
                    throw new ArgumentNullException(nameof(umbContext));
                this.umbContext = umbracoContext;
                this.cache = appContext.ApplicationCache.RuntimeCache;
                this.baseUrl = WebAPI.Helpers.UriHelper.GetBaseUriForSite(umbContext.HttpContext);
                this.tradeRoot = this.cache.GetUmbracoCacheItem<IPublishedContent>("ApiCache.TradeRoot", (Func<IPublishedContent>)(() => umbContext.ContentCache.GetAtRoot().FirstOrDefault<IPublishedContent>((Func<IPublishedContent, bool>)(c => c.ContentType.Alias == "Home"))), 240);
    
            }
        }
    

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    public EventsResponse GetAll()
            {
                LogHelper.Info<EventsController>(string.Format("Web API request to {0} via {1}", Request.RequestUri, Request?.Headers.UserAgent));
                EventsResponse eventsResponse = new EventsResponse();
                EventsService eventsService = new EventsService(this.ApplicationContext, this.UmbracoContext);
    
                try
                {
                    eventsResponse.events = eventsService.GetAllevents();
                    eventsResponse.Success = true;
                }
                catch (Exception ex)
                {
                    LogHelper.Info<EventsController>(string.Format("API Error calling events GetAll - {0}", ex.ToString()));
                    eventsResponse.Error = ex.Message;
                }
    
                return eventsResponse;
            }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies