在验证服务描述符时,某些服务无法构建错误
我有一个ASP.NET Core 5.0 MVC解决方案,
public abstract class HostedService : IHostedService, IDisposable
{
private Task _currentTask;
private readonly CancellationTokenSource _cancellationTokenSource = new
CancellationTokenSource();
protected abstract Task ExecuteAsync(CancellationToken token);
public virtual Task StartAsync(CancellationToken cancellationToken)
{
_currentTask = ExecuteAsync(cancellationToken);
return _currentTask.IsCompleted ? _currentTask : Task.CompletedTask;
}
public virtual async Task StopAsync(CancellationToken cancellationToken)
{
if (_currentTask == null) return;
try
{
_cancellationTokenSource.Cancel();
}
finally
{
await Task.WhenAny(_currentTask, Task.Delay(Timeout.Infinite, cancellationToken));
}
}
public virtual void Dispose()
{
_cancellationTokenSource.Cancel();
}
}
获取汇率
public class ExchangeSyncManager : HostedService
{
private readonly CurrencyServices _currencyServices;
private readonly ExchangeRateServices _exchangeRateServices;
public ExchangeSyncManager(CurrencyServices currencyServices, ExchangeRateServices
exchangeRateServices)
{
_currencyServices = currencyServices;
_exchangeRateServices = exchangeRateServices;
}
protected override async Task ExecuteAsync(CancellationToken token)
{
// işlem iptal edilmemişse…
if (!token.IsCancellationRequested)
{
var url = "http://www.tcmb.gov.tr/kurlar/today.xml";
XmlDocument xmlVerisi = new XmlDocument();
List<ExchangeRate> list = new List<ExchangeRate>();
xmlVerisi.Load(url);
foreach (var currency in _currencyServices.GetCurrencies())
{
var format = string.Format("Tarih_Date/Currency[@Kod='{0}']/ForexSelling", currency.Name);
var selectAndReplace = xmlVerisi.SelectSingleNode(format).InnerText.Replace('.', ',');
decimal value = Convert.ToDecimal(selectAndReplace);
list.Add(new ExchangeRate
{
Date = DateTime.Now,
Value = value,
CurrencyId = currency.Id
});
}
_exchangeRateServices.AddRange(list);
await Task.Delay(TimeSpan.FromDays(1), token);
}
}
}
从URL和启动中
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
//other services here
......................................................
.....................................................
services.AddScoped<CurrencyServices, CurrencyServices>();
services.AddScoped<ExchangeRateServices>();
services.AddHostedService<ExchangeSyncManager>();
}
:仍然会遇到此错误:
无法构建某些服务(在验证服务描述符的ServiceType时出现错误:Microsoft.extensions。 HOSTING.IHOSTEDSEVICE LIFETIME:SINGLETON IMPARINATIONTYTYPE:ipmms.business.managers.exchangesyncmanager':无法从Singletleton'Microsoft.extensions.hosting.hosting.ihosterdserdserviceservice''''''''中,无法消费scoped服务'ipmms.business.services.currencyseress.currencyservices'
。
I have a ASP.NET Core 5.0 MVC solution,
public abstract class HostedService : IHostedService, IDisposable
{
private Task _currentTask;
private readonly CancellationTokenSource _cancellationTokenSource = new
CancellationTokenSource();
protected abstract Task ExecuteAsync(CancellationToken token);
public virtual Task StartAsync(CancellationToken cancellationToken)
{
_currentTask = ExecuteAsync(cancellationToken);
return _currentTask.IsCompleted ? _currentTask : Task.CompletedTask;
}
public virtual async Task StopAsync(CancellationToken cancellationToken)
{
if (_currentTask == null) return;
try
{
_cancellationTokenSource.Cancel();
}
finally
{
await Task.WhenAny(_currentTask, Task.Delay(Timeout.Infinite, cancellationToken));
}
}
public virtual void Dispose()
{
_cancellationTokenSource.Cancel();
}
}
Gets the exchange rates from URL
public class ExchangeSyncManager : HostedService
{
private readonly CurrencyServices _currencyServices;
private readonly ExchangeRateServices _exchangeRateServices;
public ExchangeSyncManager(CurrencyServices currencyServices, ExchangeRateServices
exchangeRateServices)
{
_currencyServices = currencyServices;
_exchangeRateServices = exchangeRateServices;
}
protected override async Task ExecuteAsync(CancellationToken token)
{
// işlem iptal edilmemişse…
if (!token.IsCancellationRequested)
{
var url = "http://www.tcmb.gov.tr/kurlar/today.xml";
XmlDocument xmlVerisi = new XmlDocument();
List<ExchangeRate> list = new List<ExchangeRate>();
xmlVerisi.Load(url);
foreach (var currency in _currencyServices.GetCurrencies())
{
var format = string.Format("Tarih_Date/Currency[@Kod='{0}']/ForexSelling", currency.Name);
var selectAndReplace = xmlVerisi.SelectSingleNode(format).InnerText.Replace('.', ',');
decimal value = Convert.ToDecimal(selectAndReplace);
list.Add(new ExchangeRate
{
Date = DateTime.Now,
Value = value,
CurrencyId = currency.Id
});
}
_exchangeRateServices.AddRange(list);
await Task.Delay(TimeSpan.FromDays(1), token);
}
}
}
And in startup :
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
//other services here
......................................................
.....................................................
services.AddScoped<CurrencyServices, CurrencyServices>();
services.AddScoped<ExchangeRateServices>();
services.AddHostedService<ExchangeSyncManager>();
}
Still I am getting this error :
Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.Extensions.Hosting.IHostedService Lifetime: Singleton ImplementationType: IPMMS.Business.Managers.ExchangeSyncManager': Cannot consume scoped service 'IPMMS.Business.Services.CurrencyServices' from singleton 'Microsoft.Extensions.Hosting.IHostedService'.)'
What is wrong ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在单身人士内注入示波器服务。它们受到HTTP请求的约束。
托管服务的一生是Singleton。
但是,您可以使用IserviceProvider创建范围并检索范围范围的服务实例。
您将在此处找到如何解决问题:
https://learn.microsoft.com/en-en-us/ dotnet/core/Extensions/Scoped-Service
You cannot inject a scoped services inside a singleton. They are bound to HTTP requests.
A HostedService's lifetime is singleton.
However you can use the IServiceProvider to create a scope and retrieve an instance of your scoped service.
You will find how to fix your issue here :
https://learn.microsoft.com/en-us/dotnet/core/extensions/scoped-service