mitesh jain
mitesh jain

Reputation: 139

ApplicationContext.Current is coming null

I found same question in stack over flow as here and tried same think but still I am receiving ApplicationContext.Current as null.

I am making the web service where I need to pull out a couple of pieces of data from an Umbraco database. I don't need any of the Umbraco views or any of that stuff. I'm new to the Umbraco Core libraries.

What I did was get a below reference to my new web service project

umbraco.dll
Umbraco.Core.dll
umbraco.DataLayer.dll
umbraco.editorControls.dll
umbraco.MacroEngines.dll
umbraco.providers.dll
Umbraco.Web.UI.dll
umbraco.XmlSerializers.dll
UmbracoExamine.dll

And the below class code where I am receiving ApplicationContext.Current as null

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Web.Mvc;
using umbraco.MacroEngines;
using Umbraco.Web;
using Umbraco.Web.WebApi;
using umbraco.NodeFactory;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using umbraco.cms.businesslogic.media;
using umbraco.BusinessLogic;
using System.Configuration;
using log4net;
using System.Reflection;
using Umbraco.Core.Logging;
using System.Web.Optimization;
using System.Web.Http;
using System.Data.SqlClient;
using System.Data;
using System.Web.Script.Serialization;
using System.Collections;
using System.Collections.Specialized;
using System.Web.UI.WebControls;
using Stripe;
using System.Web.UI;
using System.Text.RegularExpressions;
using System.Web.Configuration;
using System.Web.Security;
using Newtonsoft.Json;
using System.Net.Mail;
using System.Text;
using System.Net;
using System.IO;

public class RegisterUserController : UmbracoApiController
{
    public static string UmbracoConnectionString = ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ConnectionString;

    public static IMemberService memberService = ApplicationContext.Current.Services.MemberService;// Here I am getting ApplicationContext.Current as null

}

But I am calling this class I am getting ApplicationContext.Current as null. So because of that I have Many methods in this class which works on memberService and due to this null reference they are not working. Even other methods which are not using member service are not being called.

Upvotes: 0

Views: 622

Answers (1)

Mikkel
Mikkel

Reputation: 1865

For me, the code below seems to work.

public class TestApiController : UmbracoApiController
{
    private static string _umbracoConnectionString = ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ConnectionString;

    private static IMemberService _memberService = global::Umbraco.Core.ApplicationContext.Current.Services.MemberService;

    public int GetTest()
    {
        var memberCount = _memberService.Count();
        return memberCount;
    }
}

If the above code isn't working for you e.g. with AJAX, try the below code:

public class TestApiController : UmbracoApiController
{
    public int GetTest()
    {
        var ms = Services.MemberService;
        return ms.Count();
    }
}

Upvotes: 0

Related Questions