You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A Generic Host encapsulates Dependency Injection, Logging, and Configuration in a nice, easy package.
Our current setup seems to want to achieve this but does this in a sort of homebrew way.
Changes to make to implement this:
Rewrite Program.cs to use a generic host
Services will be injected and hosted as either a singleton or in a scoped context.
Service and module dependencies will be injected (automatically) into the constructor.
Benefits of using a Generic Host:
Error handling is more intuitive
Scoped services can be created and destroyed as needed, proving some light scaling.
Resources are injected through the constructor, meaning they're easily available
Scoped ILogger<TService> instances
Configurations constructed from multiple sources, such as json configs, environment variables, or command line arguments, and injected as an IOptions<TConfiguration> object.
Easy implementation of libraries to assist the bot with growth, such as MediatR.
If this is something we might be interested in, I would be happy to build a PR with all of the necessary tests.
The text was updated successfully, but these errors were encountered:
The idea sounds good.
I'm wondering what you mean by the error handling being more intuitive?
Also, the current services might not be services that you're familiar with; they're more helper classes.
"Intuitive" may not be the right word here but it helps reinforce good error handling practices by providing tools that tie in to establishing a leveled handling system. Anything fatal bubbles up all the way to the top whereas one-off issues (like network timeouts) can be handled locally in that service (through Polly or something similar). Teamed with Microsoft.Extensions.Logging and the ability to make scoped loggers, it makes it easy to track where errors came from on the other end.
And as far as services go, a host's definition of a service is rather weak. Essentially, anything that can be instantiated and consumed somewhere in the bot qualifies as a service. It has a few modes for this. Singletons used application-wide, scoped where a new instance is generated any time an object of that type is requested, but is consistent within that request, and transient where it's always a new object no matter what.
A Generic Host encapsulates Dependency Injection, Logging, and Configuration in a nice, easy package.
Our current setup seems to want to achieve this but does this in a sort of homebrew way.
Changes to make to implement this:
Program.cs
to use a generic hostBenefits of using a Generic Host:
ILogger<TService>
instancesIOptions<TConfiguration>
object.If this is something we might be interested in, I would be happy to build a PR with all of the necessary tests.
The text was updated successfully, but these errors were encountered: