-
Notifications
You must be signed in to change notification settings - Fork 7
Dependency Injection
Shravan Jambukesan edited this page Mar 2, 2021
·
1 revision
TwelveDataSharp was built with DI in mind. You can inject the client into ASP.NET Core controllers by first creating a singleton in Startup.cs:
services.AddSingleton<TwelveDataSharp.TwelveDataClient>(new TwelveDataClient("API_KEY", new HttpClient()));
The client can now be injected into a controller:
private readonly TwelveDataClient_client;
public MyController(TwelveDataClient client)
{
_client = client;
}
And finally used in an endpoint:
[HttpGet]
[Route("GetPrice/{symbol}")]
public async Task<ActionResult> GetPrice(string symbol)
{
var response = await _client.GetRealTimePriceAsync(symbol);
return Ok(response?.Price);
}