Skip to content

Use BackgroundService and resolve with HostApplicationBuilder#23

Merged
LittleLittleCloud merged 6 commits intoRazorConsole:mainfrom
flynny75:hostedservices
Oct 20, 2025
Merged

Use BackgroundService and resolve with HostApplicationBuilder#23
LittleLittleCloud merged 6 commits intoRazorConsole:mainfrom
flynny75:hostedservices

Conversation

@flynny75
Copy link
Contributor

Implement in BackgroundService and use Microsoft HostApplicationBuilder to facilitate other DI usages

Eg. using a UI binder object with events, binding to the events in the razor, and raising the events in other BackgroundService(s)

Can remove a lot of boilerplate and rely on IHost

@DericHuynh
Copy link
Contributor

Please create a issue for this pull request for tracking, and highlight the advantages of the switch. Some notable ones may be the dependency on IHost and it's application builder means we can leverage existing integrations on those interfaces. It would also allow us to leverage the IConfigurationManager interface and allow users to bind Iconfiguration to IOptions configurations for their application.

@shlomiassaf
Copy link

@DericHuynh It's the official host application builder.
IHost is everywhere

I think it's best implementing IHostBuilder and let the existing code do the magic, since HostApplicationBuilder is not IHostBuilder but it is wrapped around it. So IHostBuilder is the best bet.

I would also keep the current implementation and apply it in a different type to support backward. Maybe add a obsolete notice

@DericHuynh
Copy link
Contributor

DericHuynh commented Oct 16, 2025

I completely understand that IHost is everywhere. Which is why I created arguments for the switch. However, it is best practice to create issues for discussion and requirements gathering, then resolving issues through PRs.

@DericHuynh
Copy link
Contributor

DericHuynh commented Oct 16, 2025

I would also keep the current implementation and apply it in a different type to support backward. Maybe add a obsolete notice

For sure, otherwise its a breaking change and we'd need to discuss a major version update, following semantic versioning. For example 0.0.5 -> 1.0.0

@flynny75
Copy link
Contributor Author

I didn't see a contribution guide for this repo, hence just creating the PR. Did I miss one? I will create an issue and link it if required. In my opinion, given the early stages of this project, I feel it overly bureaucratic. The project is not even 1.x old.

Related, Semantic Versioning does not require a major bump for breaking changes while at 0.x, thus I did not make any significant effort to maintain backwards compatibility.

Knowingly and willingly adding deprecated/obsolete methods while at 0.x seems illogical to me, 0.x is when the API surface is being designed, consumers should expect breaking changes.

# Conflicts:
#	src/RazorConsole.Core/AppHost.cs
@LittleLittleCloud
Copy link
Member

I didn't see a contribution guide for this repo, hence just creating the PR. Did I miss one? I will create an issue and link it if required. In my opinion, given the early stages of this project, I feel it overly bureaucratic. The project is not even 1.x old.

Would be appreciated if you can create a tracking issue given that it's not a small change. I can also create one for you. Will update the contribution guide with the preference of creating issue first before submitting large PR.

Related, Semantic Versioning does not require a major bump for breaking changes while at 0.x, thus I did not make any significant effort to maintain backwards compatibility.

Knowingly and willingly adding deprecated/obsolete methods while at 0.x seems illogical to me, 0.x is when the API surface is being designed, consumers should expect breaking changes.

I am open to breaking change for now given the early stage of this project. For this PR you can bump minor version to indicate the refactor change in host builder since we might want to make a release after this PR merged

@flynny75
Copy link
Contributor Author

I have created the issue #40 and bumped the minor version.

Unrelated, but may I gently suggest using GitVersion

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the RazorConsole architecture to use Microsoft's BackgroundService and HostApplicationBuilder instead of the custom ConsoleApp<TComponent> class, enabling better dependency injection integration and reducing boilerplate code.

Key changes:

  • Replaced custom ConsoleApp<TComponent> with ComponentService<TComponent> that inherits from BackgroundService
  • Modified AppHost to use HostApplicationBuilder and return IHost instances
  • Updated service registration pattern from configuration callbacks to direct DI container usage

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/RazorConsole.Core/AppHost.cs Major refactor replacing ConsoleApp with BackgroundService-based architecture and HostApplicationBuilder
src/RazorConsole.Core/RazorConsole.Core.csproj Added Microsoft.Extensions.Hosting package dependency
src/RazorConsole.Tests/ConsoleAppTests.cs Updated test to use new DI service registration pattern
src/RazorConsole.Gallery/Program.cs Updated to use new builder.Services API instead of ConfigureServices
examples/LLMAgentTUI/Program.cs Updated to use new builder.Services API instead of ConfigureServices
nuget/nuget-package.props Version bump from 0.0.6 to 0.1.0
Comments suppressed due to low confidence (1)

src/RazorConsole.Core/AppHost.cs:1

  • The indentation uses a tab character instead of spaces. According to the .editorconfig rule mentioned in the coding guidelines, four-space indentation should be used consistently.
using Microsoft.AspNetCore.Components;

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Xiaoyun Zhang <bigmiao.zhang@gmail.com>
@LittleLittleCloud LittleLittleCloud enabled auto-merge (squash) October 20, 2025 11:05
@LittleLittleCloud
Copy link
Member

LittleLittleCloud commented Oct 20, 2025

Thanks @flynny75 approved and merged

Unrelated, but may I gently suggest using GitVersion

It's my first time hearing GitVersion, let me take a look and rethink about it

@LittleLittleCloud LittleLittleCloud merged commit 8f891f5 into RazorConsole:main Oct 20, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants