简体   繁体   English

参数1:无法从“ System.Threading.Tasks.Task”转换 <Project.Models.Booking> &#39;到Project.Models.Booking

[英]Argument 1: cannot convert from 'System.Threading.Tasks.Task<Project.Models.Booking>' to Project.Models.Booking

I'm working on school project, and I'm trying to implement asynchronous methods in my repository classes and got this error in my API for the POST-method: Argument 1: cannot convert from 'System.Threading.Tasks.Task' to Project.Models.Booking. 我正在学校项目上工作,并且试图在存储库类中实现异步方法,并且在POST方法的API中遇到此错误:参数1:无法从'System.Threading.Tasks.Task'转换为Project.Models.Booking。

Can someone please help me to solve this error? 有人可以帮我解决这个错误吗?

This is how my repository-method looks like: 这是我的存储库方法的样子:

    public async Task<Booking>CreateBooking(Booking inBooking) {
        Booking booking = new Booking();

        booking.BookingId = ApplicationDbContext.Bookings.Count();
        booking.UserName = inBooking.UserName;
        booking.UserMail = inBooking.UserMail;
        booking.UserMobile = inBooking.UserMobile;
        booking.Available = false;
        booking.StartTime = inBooking.StartTime;
        booking.EndTime = inBooking.EndTime;
        booking.Date = inBooking.Date;

        Add(booking);
        return await Task.FromResult(booking);
        //return booking;
    }

And this how my Post-method looks like: 这就是我的后期方法:

 [Route("api/PostBooking")]
        [HttpPost]
        public async Task<IHttpActionResult> PostBooking(BookingSystemServiceBookingViewModel inBooking)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest("Invalid data");
            }

            var booking =  inBooking.booking;

            booking.StartTime = inBooking.startTime;
            booking.EndTime = inBooking.startTime.AddMinutes(inBooking.service.Duration);

             uw.Services.AddBooking(uw.Bookings.CreateBooking(booking), inBooking.service.ServiceId);
            uw.Complete();

            return Ok();
        }

I get the error message at: uw.Services.AddBooking(uw.Bookings.CreateBooking(booking), inBooking.service.ServiceId);` 我在以下位置收到错误消息:uw.Services.AddBooking(uw.Bookings.CreateBooking(booking),inBooking.service.ServiceId);`

trying to implement asynchronous methods in my repository classes 尝试在我的存储库类中实现异步方法

The best way to make things asynchronous is not from the "outside in". 使事物异步的最佳方法不是“从外而内”。 That is, you don't want to start at the controller action and say "how can I make this asynchronous?". 也就是说,您不想从控制器动作开始就说“如何使它异步?”。 Instead, you want to start from the lowest-level API and say "this should be asynchronous" (ie, it's I/O-based), and let asynchrony grow out from there. 相反,您想从最低级别的API开始,说“这应该是异步的”(即,它是基于I / O的),然后让异步从那里开始。

If Add actually does the database insert, then that would be the first method to be made asynchronous. 如果Add实际上确实在数据库中插入,那么那将是第一个被异步化的方法。 Then you await your AddAsync method, and the asynchrony grows from AddAsync to CreateBookingAsync : 然后,您将await AddAsync方法,并且异步性将从AddAsyncCreateBookingAsync

public async Task<Booking> CreateBookingAsync(Booking inBooking) {
  Booking booking = new Booking();

  ...

  await AddAsync(booking);
  return booking;
}

[Route("api/PostBooking")]
[HttpPost]
public async Task<IHttpActionResult> PostBooking(BookingSystemServiceBookingViewModel inBooking)
{
  ...

  uw.Services.AddBooking(await uw.Bookings.CreateBookingAsync(booking), inBooking.service.ServiceId);
  uw.Complete();

  return Ok();
}

If, on the other hand, it is the Complete method that actually communicates with the database, then that is the one that should be made asynchronous. 如果,另一方面,它是Complete ,实际上与数据库通信的方法,那么是应该进行异步之一。 In this case Add is purely an in-memory operation, and Complete is the one that saves all the database updates. 在这种情况下, Add纯粹是内存中的操作,而Complete是保存所有数据库更新的操作。 So, after you convert Complete to CompleteAsync , your code would look like: 因此, Complete转换为CompleteAsync ,您的代码将如下所示:

public Booking CreateBooking(Booking inBooking) {
  Booking booking = new Booking();

  ...

  Add(booking);
  return booking;
}

[Route("api/PostBooking")]
[HttpPost]
public async Task<IHttpActionResult> PostBooking(BookingSystemServiceBookingViewModel inBooking)
{
  ...

  uw.Services.AddBooking(uw.Bookings.CreateBooking(booking), inBooking.service.ServiceId);
  await uw.CompleteAsync();

  return Ok();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型&#39;bool&#39;隐式转换为&#39;System.Threading.Tasks.Task&#39; - Cannot implicitly convert type 'bool' to 'System.Threading.Tasks.Task' 无法将类型&#39;void&#39;隐式转换为&#39;System.Threading.Tasks.Task&#39; - Cannot implicitly convert type 'void' to 'System.Threading.Tasks.Task' 无法隐式转换类型&#39;System.Threading.Tasks.Task - Cannot implicitly convert type 'System.Threading.Tasks.Task 无法从“无效”转换为System.Threading.Tasks.Task <System.Action> - Cannot convert from 'void' to System.Threading.Tasks.Task<System.Action> 无法从“ System.Threading.Tasks.Task”转换为“ System.Collections.Generic.Dictionary” <string,string> &#39; - Cannot convert from 'System.Threading.Tasks.Task' to 'System.Collections.Generic.Dictionary<string,string>' 无法将 System.Threading.Tasks.Task... 转换为 System.Threading.Tasks.Task... 尽管看似相同的类型 - Cannot convert System.Threading.Tasks.Task… to System.Threading.Tasks.Task… despite seemingly being same type C#无法从&#39;void&#39;转换为&#39;System.Threading.Tasks.Task - C# Cannot convert from 'void' to 'System.Threading.Tasks.Task 无法将 System.Threading.Task.Tasks 转换为 System.Func <system.func<system.threading.tasks.task></system.func<system.threading.tasks.task> - Cannot convert System.Threading.Task.Tasks to System.Func<System.Func<System.Threading.Tasks.Task> 无法将类型&#39;System.Collections.Generic.List &lt;&gt;&#39;隐式转换为&#39;System.Threading.Tasks.Task &lt;&gt;&gt; - Cannot implicitly convert type 'System.Collections.Generic.List<>' to 'System.Threading.Tasks.Task<>> 无法将布尔类型隐式转换为System.Threading.Tasks.Task <bool> - Cannot implicitly convert type bool to System.Threading.Tasks.Task<bool>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM