[英]How to configure Azure App Service application settings for ASP.NET Core 3.1?
[英]Timeout calling TryUpdateModelAsync on ASP.NET Core 3.1 app in Azure App Service
我有一個 ASP.NET Core 3.1 Web 應用程序,它部署到帶有 ASP.NET Core 3.1 站點擴展的 Windows Azure 應用服務。 我收到“502 - Web 服務器在充當網關或代理服務器時收到無效響應”的消息。 每當我對 Web 應用程序執行 POST 並調用 TryUpdateModelAsync 時都會出錯。 我啟用了遠程調試並確定這是永不返回的方法。 當我調用該方法並手動更新我的實體時,一切正常。 另外,在本地調試時我沒有看到這個問題。
這是控制器操作:
[HttpPost]
[Route("/admin/products/{productid:int}/edit")]
public async Task<ActionResult> General(ProductGeneralViewModel model, int productid)
{
var product = await _db.Products.FindAsync(productid);
if (!ModelState.IsValid)
{
HydrateModel(model);
return View(model);
}
// Never gets past here and browser shows a 502
await TryUpdateModelAsync<Product>(product, "Product");
await _db.SaveChangesAsync();
return RedirectToAction("General", new { productid });
}
和實體框架模型:
[Table("Product")]
public class Product
{
public Product()
{
DateCreated = DateTime.UtcNow;
Status = ProductStatus.Draft;
Configurations = new List<ProductConfiguration>();
}
public string Container => "products";
[Key]
public int Id { get; set; }
public int? FeatureImageGalleryId { get; set; }
public int CategoryId { get; set; }
public int SeriesId { get; set; }
[Required]
[StringLength(100)]
public string Name { get; set; }
[StringLength(200)]
public string ThumbnailPhoto { get; set; }
[StringLength(200)]
public string FeaturePhoto { get; set; }
[Required]
[StringLength(100)]
[Display(Name = "Item #")]
public string ItemNumber { get; set; }
public ProductStatus Status { get; set; }
[StringLength(4000)]
public string Description { get; set; }
public DateTime DateCreated { get; set; }
public bool AvailableOnline { get; set; }
public decimal MinimumSize { get; set; }
[ForeignKey(nameof(FeatureImageGalleryId))]
public virtual Gallery FeatureImageGallery { get; set; }
[ForeignKey(nameof(CategoryId))]
public virtual Category Category { get; set; }
[ForeignKey(nameof(SeriesId))]
public virtual Series Series { get; set; }
public virtual ICollection<ProductConfiguration> Configurations { get; set; }
}
這發生在多個實體上。 我還嘗試使用 TryUpdateModelAsync 中的 IncludedExpressions 指定要更新的特定屬性,但這並沒有什么區別。
看起來這是 ASP.NET Core 和 TryUpdateModelAsync 中延遲加載復雜對象的問題。
我發現了這個 GitHub 問題(已關閉,但問題從未解決): https : //github.com/aspnet/EntityFrameworkCore/issues/12866
此處列出的一種解決方法是在調用 TryUpdateModelAsync 時禁用延遲加載。
try
{
_context.ChangeTracker.LazyLoadingEnabled = false;
return await TryUpdateModelAsync(...)
}
finally
{
_context.ChangeTracker.LazyLoadingEnabled = false;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.