必须处理简单 CPU 绑定操作的异步方法执行流程

Async method execution flow that has to deal a simple CPU bound operation

public class Car
    {
        public int TyresCount { get; set; }
        public int SpareTyreCount { get; set; }
        public int FrontTyres { get; set; }
        public int RearTyres { get; set; }
    }

    public class CarAssembly
    {

        public async Task<Car> Process(Car car)
        {
            car = await AssembleFrameAsync(car);
            //Option A
            SetTyreCount(car);
            //or Option B
            SetTireCountNonStatic(car);
            //or Option C
            await Task.Run(() => SetTyreCountAsync(car));
            car = await AssembleEngineAsync(car);
            return car;
        }

        public async Task<Car> AssembleFrame(Car car)
        {
            car = await httpclient.GetAsync("GetFrame");
            return car;
        }

        //Option A
        private static void SetTyreCount(Car car)
        {
            car.TyresCount = car.FrontTyres + car.RearTyres;
            car.SpareTyreCount = 1;
        }

        //Option B
        public void SetTyreCountNonStatic(Car car)
        {
            car.TyresCount = car.FrontTyres + car.RearTyres;
            car.SpareTyreCount = 1;
        }

        //Option C
        public void SetTyreCountAsync(Car car)
        {
            car.TyresCount = car.FrontTyres + car.RearTyres;
            car.SpareTyreCount = 1;
        }
        public async Task<Car> AssembleEngineAsync(Car car)
        {
            car = await httpclient.postAsync("assembleEngine");
            return car;
        }
    }

我有这个 class CarAssembly,它具有调用异步方法 的方法 Process AssembleFrame 然后它必须调用另一个方法 GetTyreCount 这是获取计数的非常简单的方法,如上面提到的上面 3 个选项 OptionA,OptionB,OptionC是调用这个简单的 CPU 绑定方法的正确方法。即使我陈述的选项不正确,请提供您的想法什么是正确的方法。假设它是 class 在 WebAPI 中执行(没有 UI)

在 WebAPI 上,您应该使用选项 A 或 B。如果您不需要访问实例变量(例如记录器),请使用选项 A。如果你这样做,请使用选项 B。

一般来说,do not use Task.Run on ASP.NET