Asp.net core 在Razor视图中填充selectlistitem时同步等待空引用异常

Asp.net core 在Razor视图中填充selectlistitem时同步等待空引用异常,asp.net-core,dependency-injection,async-await,Asp.net Core,Dependency Injection,Async Await,我注入我的服务是为了像这样填充razor中的下拉列表 @inject Web.Services.MyService Options <select asp-for="CityID" asp-items="await Options.GetCities();" class="form-control form-control-sm"> <option>--Select--</option> </select> 现在我试着

我注入我的服务是为了像这样填充razor中的下拉列表

@inject Web.Services.MyService  Options
<select asp-for="CityID" asp-items="await Options.GetCities();" class="form-control form-control-sm">
            <option>--Select--</option>
</select>
现在我试着像这样填充我的下拉列表

@inject Web.Services.MyService  Options
<select asp-for="CityID" asp-items="await Options.GetCities();" class="form-control form-control-sm">
            <option>--Select--</option>
</select>

--挑选--
Getcities方法如下所示

public async Task<IEnumerable<SelectListItem>> GetCities()

        {   

            var cities = await _cityrepository.ListAllAsync();

            var items = new List<SelectListItem>
            {
                new SelectListItem() { Value = null, Text = "All", Selected = true }
            };
            foreach (SetupCities city in cities)
            {
                items.Add(new SelectListItem() { Value = city.CityId.ToString(), Text = city.Name});
            }

            return items;
        }
公共异步任务GetCities() { var cities=await_cityrepository.ListAllAsync(); var items=新列表 { 新建SelectListItem(){Value=null,Text=“All”,Selected=true} }; foreach(设置城市中的城市) { 添加(新SelectListItem(){Value=city.CityId.ToString(),Text=city.Name}); } 退货项目; } 我得到以下错误

NullReferenceException: Object reference not set to an instance of an object.
EvolvedX.Web.Services.VMWHService.GetCities() in VMWHService.cs
+
            var cities = await _cityrepository.ListAllAsync();
AspNetCore.Views_Warehouse_CreateWarehouse.ExecuteAsync() in CreateWarehouse.cshtml
+
        <select asp-for="CityID" asp-items="await Options.GetCities();" class="form-control form-control-sm">
NullReferenceException:对象引用未设置为对象的实例。
VMWHService.cs中的EvolvedX.Web.Services.VMWHService.GetCities()
+
var cities=await_cityrepository.ListAllAsync();
CreateWarehouse.cshtml中的AspNetCore.Views\u Warehouse\u CreateWarehouse.ExecuteAsync()
+
我可以看到函数退出时的第一个wait hit getcities。处理这种情况的正确方法是什么
M函数在完成方法之前退出,但它发现Wait导致我的selectlistitem为null。我想知道我是如何修改我的代码的,这样selectlistitem get filled null异常将得到解决

可能重复@SirRufo并不真正是我真正的问题是处理导致空引用的异步方法,在上面的帖子中没有对异步foundTrust me的解释,不管是异步还是非异步,something返回null,您必须在完成导致我的selectlistitem为null的方法之前调试factM函数是否退出。我想知道我如何修改我的代码,以便selectlistitem get filled null异常将得到解决您在
Startup
中注册了
MyService
依赖项了吗?@SirRufo的可能重复不是真的我真正的问题是处理导致空引用的异步方法,在上面的帖子中没有解释异步查找信任我,不管是异步还是非异步,something返回null,您必须在完成导致我的selectlistitem为null的方法之前调试factM函数是否退出。我想知道我是如何修改代码的,这样selectlistitem get filled null异常将得到解决的。您是否已在
Startup
中注册了
MyService
依赖项?