This question might have been answered a million times, but I cannot seem to find the correct answer.
Im pretty new to Dependency Injection
, but have tried to make some DI in my Azure Function
. However, now I get the error "An object reference is required for the non-static method etc.."
I simply cant figure out a way to resolve this problem.
My Azure Function looks like this:
public class GetEloverblik
{
private readonly ConnectionSettings _connectionSettings;
private readonly ELoverblikAccess _eloverblikAccess;
public GetEloverblik(IOptions<ConnectionSettings> connectionStrings, IOptions<ELoverblikAccess> eloverblikAccess)
{
_connectionSettings = connectionStrings?.Value ?? throw new ArgumentNullException(nameof(connectionStrings));
_eloverblikAccess = eloverblikAccess?.Value ?? throw new ArgumentNullException(nameof(eloverblikAccess));
}
[FunctionName("GetEloverblik")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req, ILogger log)
{
try
{
string token = await ElOverblikToken.GetToken(); //MY ERROR
}
catch (Exception ex)
{
log.LogInformation(ex.Message);
}
return new OkObjectResult("Done");
}
}
And the ElOverblikToken.GetToken() Im calling:
public class ElOverblikToken {
private readonly ELoverblikAccess _eloverblikAccess;
public ElOverblikToken(IOptions<ConnectionSettings> connectionStrings, IOptions<ELoverblikAccess> eloverblikAccess)
{
_eloverblikAccess = eloverblikAccess?.Value ?? throw new ArgumentNullException(nameof(eloverblikAccess));
}
public async Task<string> GetToken()
{
try
{
//Do something
return token;
}
catch (Exception ex)
{
return null;
//log.LogInformation(ex.Message);
}
}
}
I have tried to make my GetToken Method static
public static async Task<string> GetToken()
But then I recieve an error on _eloverblikAccess saying it needs an object reference.
What am I missing?