gpt4 book ai didi

How to correctly establish the type argument to CancellationToken in Net Core?(如何在Net Core中正确建立CancerationToken的类型参数?)

转载 作者:bug小助手 更新时间:2023-10-25 11:40:21 26 4
gpt4 key购买 nike



I'm developing a clean Architecture's services on Net Core. However when I refactor the methods, it return me the error cs0411. more specifically in the 'GetSubjectById' method when the Cancellation Token is usage and it didnt provide the type argument.

我正在开发一个干净的架构的服务在净核心。然而,当我重构这些方法时,它返回错误cs0411。更具体地说,当取消令牌被使用并且它没有提供类型参数时,在“GetSubjectById”方法中。


IRepository.cs #this is the repository interfaces that contain controller's method

IRepository.cs#这是包含控制器方法的存储库接口


public interface IRepository
{
Task<Subject> GetSubjectById(int id, CancellationToken CancellationToken);
}

MethodRepository.cs #this is the methods for the below Interface

MethodRepository.cs#这是以下接口的方法


  public class SubjectRepository : ISubjectRepository
{
private readonly dbContext _context;
private ISubjectRepository _subject;


public ISubjectRepository Subject {
get {
if(_subject == null)
{
_subject = new SubjectRepository(_context);
}
return _subject;
}
}
public SubjectRepository(dbContext context)
{
_context = context;
}
public async Task<Subject> GetSubjectById(int id, CancellationToken cancellationToken)
{
return _context.Subjects.FirstOrDefault(o => o.StudentId == id, cancellationToken) #This code line cause the error cs0411;

}
}

If anyone could bring me some, I'd be really grateful.

如果有人能给我带一些来,我会非常感激的。


更多回答

what is _context.Subjects? do you mean to use FirstOrDefaultAsync?

什么是_Conext.Subjects?您是要使用FirstOr DefaultAsync吗?

Thats the dbContext for subject model.

这就是主题模型的数据库上下文。

You have an async method that doesn't contain any await calls.

您有一个不包含任何等待调用的异步方法。

That implementation doesn't make sense in more than that aspect. Why the Subject property?

除了这一点,该实现在其他方面也没有意义。为什么是主题属性?

Obviously, because thats the model as it shown below. If you dont want to provide useful solutions, please hold yourself to denigrate people by their doubts.

显然,因为这就是如下所示的模型。如果你不想提供有用的解决方案,请控制住自己,用他们的怀疑来诋毁他们。

优秀答案推荐

It's just because FirstOrDefault doesn't have CancellationToken parameter, but FirstOrDefaultAsync does. Try this

这只是因为FirstOrDefault没有CancerancationToken参数,但FirstOrDefaultAsync有。尝尝这个


    public Task<Subject> GetSubjectById(int id, CancellationToken cancellationToken)
{
return _context.Subjects.FirstOrDefaultAsync(o => o.StudentId == id, cancellationToken)

}

更多回答

Either remove async or add await.

删除异步或添加等待。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com