-1

Let's go straight to my probably fairly simple problem.

I have a LoginService class in my Services folder which makes a connection with the server. I have another UserModel where I want to receive information from the server. In order not to login again (which would be stupid), I need to maintain the client reference in both files having the same value. In other words, I need to be able to access the same object from a different class (make another reference).

I have tried and tried and searched but I am missing something. A fairly similar post that I found that still didn't solve mine is this.

The code in my LoginService:

namespace App_Name.Services
{
    class LoginService
    {
        public static Class_Name client;

        public async Task MakeConnectionAsync(string userToken)
        {
            client = new Class_Name();

            PasswordVault vault = new PasswordVault();

            await client.LoginAsync(TokenType.User, userToken);

            await client.StartAsync();
        }

So now I want to get the user avatar on my UserModel.cs:

namespace App_Name.Models
{
    class UserModel
    {
        public string GetAvatar()
        {
            return LoginService.client.CurrentUser.GetAvatarUrl();
        }

But it always gives an exception because it tries but there is no connection. I am sure that it was Connected because in order to load the UserModel it has to be a successful connection.

Any ideas ?

1
  • idk if this is useful for you , but have you tried using dependency injection and interfaces ? it may solve your problem , creating an interface containing your login method and then injecting this interface method anywhere in your program Commented Feb 26, 2017 at 20:12

2 Answers 2

0

When you call directly the client.CurrentUser.GetAvatarUrl() method, its not determined by the LoginService class. You should be create the Login class before the usage. Also you are using async tasks on your LoginService class, you must confirm that already create user by your UserModel class.

For best practice you can create instance with your constructor like this.

  static class LoginService
    {
        public static Class_Name client;
        static LoginService()
        {
        client = new Class_Name();
        }

If you want to go with static (which I not prefer for service level) not use static for like this purpose of usage. Firstly you should be check dependency injection concepts; i suggest unity and structuremap containers. You can create your consume services by your classes without any object null ref. exception. Dependency injection decrease on coupling and null reference exception.

Sign up to request clarification or add additional context in comments.

Comments

0

Ehmmm, for anyone that can use this as a reference, my code above is just fine.

The problem was with the connection API not returning the status immediately. A delay of 2 seconds solved my problem. Thanks everyone for their help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.