4

I am following this post, and implemented the Startup class so that I can inject my services in the constructor, but the service instance is always null and I am getting Object reference not set to an instance of an object when I run the application.

Below is my Startup class.

[assembly: FunctionsStartup(typeof(Backup.Functions.Startup))]
namespace Backup.Functions {
    public class Startup: FunctionsStartup {
        public override void Configure(IFunctionsHostBuilder builder) {
            builder.Services.AddSingleton < IBlobService,
            BlobService > ();
        }
    }
}

My Function code is as below.

public class DeleteDailyBlobs {
    private static IBlobService _blobService;
    public DeleteDailyBlobs(IBlobService blobService) {
        _blobService = blobService;
    } 
    [FunctionName("DeleteDailyBlobs")]
    public static void Run([TimerTrigger("0/3 * * * * *")] TimerInfo myTimer, ILogger log) {
        if (_blobService.PerformTasks().GetAwaiter().GetResult()) {
            log.LogInformation(SuccessMessages.FunctionExecutedSuccessfully);
        }
        else {
            log.LogError(ErrorMessages.SomethingBadHappened);
        }
    }
}

Here the _blobService is always null.

enter image description here

1
  • why would you inject a blobservice wrapper inside of your function? you are increasing the complexity of your code and decreasing maintainability. do you think you are doing unit tests? Commented Jul 25, 2019 at 20:59

2 Answers 2

4

Finally I was able to find what was the issue, unfortunately I forgot to mark my function non static, so all I had to do was to remove the static keyword from my Azure Function. After removing that, everything was fine.

public void Run([TimerTrigger("0/3 * * * * *")]TimerInfo myTimer, ILogger log)

enter image description here

Hope it helps.

As Nkosi was mentioning we should mark the return type of the Function as Task and I have written an article about this, and can be found here.

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

Comments

0

In addition to making the function a non static member, you should make the function return a Task and await the async function call.

public class DeleteDailyBlobs {
    private readonly IBlobService blobService;

    public DeleteDailyBlobs(IBlobService blobService) {
        this.blobService = blobService;
    } 

    [FunctionName("DeleteDailyBlobs")]
    public async Task Run([TimerTrigger("0/3 * * * * *")] TimerInfo myTimer, ILogger log) {
        if (await blobService.PerformTasks()) {
            log.LogInformation(SuccessMessages.FunctionExecutedSuccessfully);
        }
        else {
            log.LogError(ErrorMessages.SomethingBadHappened);
        }
    }
}

And since the dependency is also being added as a singleton, there really is no need to make it a static field as well. The instance will be the same where ever it is injected.

2 Comments

That is a very good point, and I had already updated my code and written an article about it. The information is been added to the answer.
What's the reason for needing to await it?

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.