C# 测试.net core 2.2 web api控制器时收到InvalidOperationException错误
我正在使用C#for.NET2.2创建一个.NETWebAPI。我正在实现存储库设计模式,并一直遵循本教程: 我的代码生成并成功运行,直到我使用postman测试向我的api控制器发送请求为止,我收到的错误是: System.InvalidOperationException:无法使用服务容器中的服务和默认值实例化类型“Cotal.Infrastructure.Repositories.TableStorageRepository`1[Cotal.Models.EventEntity]”的构造函数 我的startup.cs文件包含以下内容:C# 测试.net core 2.2 web api控制器时收到InvalidOperationException错误,c#,C#,我正在使用C#for.NET2.2创建一个.NETWebAPI。我正在实现存储库设计模式,并一直遵循本教程: 我的代码生成并成功运行,直到我使用postman测试向我的api控制器发送请求为止,我收到的错误是: System.InvalidOperationException:无法使用服务容器中的服务和默认值实例化类型“Cotal.Infrastructure.Repositories.TableStorageRepository`1[Cotal.Models.EventEntity]”的构造函
services.TryAddSingleton()代码>
我的EventEntity模型如下所示:
public class EventEntity : TableEntity
{
public EventEntity()
{
}
public EventEntity(string userId, string rowKey)
{
PartitionKey = userId;
RowKey = rowKey;
}
public int BrandId { get; set; }
TableStorageRepository的具体类如下所示(为了简单起见,我删去了其余代码):
公共类TableStorageRepository:StorageRepository,ITableStorageRepository
其中TModel:class、ITableEntity、new()
{
公用表存储器存储库(ITableStorageSettings存储设置)
:基本(存储设置)
{
}
公共表存储器存储库(CloudTable表)
:此(新的CloudTableSettings(表))
{
}
与您的问题无关,但我强烈反对使用.NET Core 2.2创建新的API。该API已失去支持近一年,不再接收错误修复或安全更新,因此容易受到各种安全问题的攻击。有关受支持版本的更多详细信息,请参见此处:thank you@Martin Costello:),但并非如此我有兴趣尝试解决这个错误。
public class TableStorageRepository<TModel> : StorageRepository<ITableStorageSettings>, ITableStorageRepository<TModel>
where TModel : class, ITableEntity, new()
{
public TableStorageRepository(ITableStorageSettings storageSettings)
: base(storageSettings)
{
}
public TableStorageRepository(CloudTable table)
: this(new CloudTableSettings(table))
{
}