Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
Update ASP.NET section with better example
Browse files Browse the repository at this point in the history
  • Loading branch information
BlossomiShymae committed May 5, 2023
1 parent 8a459b7 commit c51e77b
Showing 1 changed file with 11 additions and 34 deletions.
45 changes: 11 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -574,52 +574,29 @@ Champion {
# Using with ASP.NET Core
RiotBlossom can be used with [ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/introduction-to-aspnet-core?view=aspnetcore-6.0), a cross-platform and open-source web framework.

One way of setting up for your totes awesome needs is to create a service container class for dependency injection.
One way of setting up for your totes awesome needs is to inject the `IRiotBlossomClient` as-is using dependency injection~*

We can first add a `IHttpClientFactory` to the services collection.
```csharp
var builder = WebApplication.CreateBuilder(args);

// ASP.NET Core setup may differ based on your project selection e.g. Web App, MVC, Blazor
// This example is shortened for brevity
builder.Services.AddHttpClient();
```

Now we can create a service class in the `Services` namespace that will contain an instance of RiotBlossom. Note the use
of the injected `IHttpClientFactory`. ovo
```csharp
public interface IRiotBlossomService
{
IRiotBlossomClient Client { get; init; }
}

public class RiotBlossomService : IRiotBlossomService
builder.Services.AddSingleton<IRiotBlossomClient>(p =>
{
public IRiotBlossomClient Client { get; init; }

public RiotBlossomService(IHttpClientFactory httpClientFactory)
{
HttpClient httpClient = httpClientFactory.CreateClient();
httpClient.Timeout = TimeSpan.FromSeconds(15);

Client = RiotBlossomCore.CreateClientBuilder()
.AddRiotApiKey(Environment.GetEnvironmentVariable("RIOT_API_KEY")
?? throw new NullReferenceException())
.AddHttpClient(httpClient)
// Middleware stack builders left out for brevity!
.Build();
}
}
IHttpClientFactory factory = p.GetRequiredService<IHttpClientFactory>();
HttpClient client = factory.CreateClient();
client.Timeout = TimeSpan.FromSeconds(5);
return RiotBlossomCore.CreateClientBuilder()
// Redacted for brevity, wah
.Build();
});
```

Now we can add our created a singleton service, ready for use! Yay!
```csharp
// Add as singleton to share a single instance!
builder.Services.AddSingleton<IRiotBlossomService>();
```
[Learn more on using dependency injection in ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-6.0)!

[Learn more on using dependency injection in ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-6.0).
![image](https://user-images.githubusercontent.com/87099578/236560869-c510c3b1-5432-4803-b2d2-603f252cbbf7.png)

# API Interfaces

Expand Down

0 comments on commit c51e77b

Please sign in to comment.