Auth0 with Xamarin PCL Dependency Service NullReferenceException

I am trying to use DependencyService with Auth0 component and am stuck with System.NullReferenceException: Object reference not set to object error instance.

Below is my code in android project:

namespace LoginPattern.Android
{

[assembly: Xamarin.Forms.Dependency (typeof(LoginPattern.Android.Auth0WidgetLogin))]
public class Auth0WidgetLogin : FormsApplicationActivity, LoginPattern.IAuth0WidgetLogin
{

    private Auth0Client auth0 = new Auth0Client (
                                    "xxx.auth0.com",
                                    "xxxxxxxxxxxxxx");

    public Auth0WidgetLogin ()
    {
    }

    public async Task<LoginPattern.User> LoginUseAuth0EmbeddedWidget()
    {
        Auth0User usr = null;


        try {
            usr = await this.auth0.LoginAsync(Forms.Context);
        } catch (Exception ex) {
            throw ex;
        }

        LoginPattern.User userObj = new User(usr.Auth0AccessToken);

        return userObj;

    }
}
}

      

This is in the shared library:

namespace LoginPattern
{
    public interface IAuth0WidgetLogin
    {
        Task<User> LoginUseAuth0EmbeddedWidget();
    }

    public class User
    {
        public User(
            string accessToken)
        {
            AccessToken = accessToken;
        }

        public string AccessToken {get; private set;}
        public string Scope {get; private set;}
    }
}

      

This is where you get an error when calling the dependency service:

public async void Login ()
    {
        LoginPattern.User usr = null;
        usr = await DependencyService.Get<IAuth0WidgetLogin>().LoginUseAuth0EmbeddedWidget();
        App.Current.Properties["IsLoggedIn"] = true;
    }

      

+3


source to share


1 answer


Place your attribute above in your namespace declaration:



    [assembly: Xamarin.Forms.Dependency (typeof(LoginPattern.Android.Auth0WidgetLogin))]
    namespace LoginPattern.Android
    {
          public class Auth0WidgetLogin : FormsApplicationActivity, LoginPattern.IAuth0WidgetLogin
          ...
    {

      

+4


source







All Articles