I had developed the
blazorWASM
Hosted Application with
IndexedDb
.
This application has two projects, client and server projects.
Client project purely runs on browser and server project contains ASP.NET Core API and ASP.NET Core Server hosted the client application or client project.
I had developed background service for long running on background of application in server project or server. Blazor client makes a request to
background service using HTTP API request at the time, got the error like:
Unable to resolve service for type 'WebAssembly.Server.Data.UpdateDbBackgroundService' while attempting to activate 'WebAssembly.Server.Controllers.BackgroundServiceController'.
Backgroundservice
:
public class UpdateDbBackgroundService : BackgroundService
{
private CancellationTokenSource _stoppingCts;
private Task _executeTask;
private SyncWorkerService _worker;
private readonly ILogger<UpdateDbBackgroundService> _logger;
private readonly IBlazorDbFactory _dbFactory;
private readonly IHttpClientFactory _httpClientFactory;
private IndexedDbManager _manager;
private readonly IServiceProvider _serviceProvider;
public UpdateDbBackgroundService
(IServiceProvider serviceProvider,
IHttpClientFactory httpClientFactory,
IBlazorDbFactory dbFactory,
SyncWorkerService worker, IndexedDbManager manager,
ILogger<UpdateDbBackgroundService> logger)
{
_serviceProvider = serviceProvider;
_httpClientFactory = httpClientFactory;
_dbFactory = dbFactory;
_worker = worker;
_manager = manager;
_logger = logger;
}
protected override async Task ExecuteAsync
(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
_logger.LogInformation("background process started");
await ProcessOrdersAsync();
}
}
public async Task ProcessOrdersAsync()
{
_manager = await _dbFactory.GetDbManager("resdb");
var order = (await _manager.ToArray<Orders>
("Orders")).OrderByDescending(s => s.Id).FirstOrDefault();
var client = _httpClientFactory.CreateClient("WebAPI");
var updaterequest = new HttpRequestMessage
(HttpMethod.Post, "api/Orders/UpdateOrder");
HttpResponseMessage response = await
HttpClientJsonExtensions.PostAsJsonAsync
(client, updaterequest.RequestUri, order);
if (response.IsSuccessStatusCode)
{
var updatereponse =
response.Content.ReadAsStringAsync().Result;
}
}
public Task StartAsync(CancellationToken cancellationToken)
{
_stoppingCts =
CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
_executeTask = ExecuteAsync(_stoppingCts.Token);
if (_executeTask.IsCompleted)
{
return _executeTask;
}
return Task.CompletedTask;
}
public async Task StopAsync(CancellationToken cancellationToken)
{
if (_executeTask == null)
{
return;
}
try
{
_stoppingCts.Cancel();
}
finally
{
await Task.WhenAny(_executeTask, Task.Delay
(Timeout.Infinite, cancellationToken)).ConfigureAwait(false);
}
}
}
BackgroundServiceController
:
[Route("api/background-service")]
[ApiController]
public class BackgroundServiceController : ControllerBase
{
private readonly UpdateDbBackgroundService _backgroundService;
public BackgroundServiceController
(UpdateDbBackgroundService backgroundService)
{
_backgroundService = backgroundService;
}
[HttpPost("start")]
public IActionResult StartBackgroundService()
{
_backgroundService.StartAsync(CancellationToken.None);
return Ok();
}
[HttpPost("stop")]
public IActionResult StopBackgroundService()
{
_backgroundService.StopAsync(CancellationToken.None);
return Ok();
}
}
I had registered the service in
starup.cs file:
public void ConfigureServices(IServiceCollection services)
{
services.AddHostedService<UpdateDbBackgroundService>();
services.AddScoped<IBlazorDbFactory, BlazorDbFactory>();
services.AddScoped<SyncWorkerService>();
}
Namespace
where
backgroundservice
defined that is the same for where the register service using Dependency injection(DI) for
AddHostedservice
also but didn't find the service or NO service for Type
UpdateDbBackgroundService
has been registered when request controller from client.
Please help me to fix the issue. Thanks!
What I have tried:
I had developed background service for long running on background of application in server project or server. Blazor client makes a request to
background service using HTTP API request at the time got the error like:
Unable to resolve service for type 'WebAssembly.Server.Data.UpdateDbBackgroundService' while attempting to activate 'WebAssembly.Server.Controllers.BackgroundServiceController'.