我有一个模型,可以在async
方法中设置接收者,然后,如果电子邮件的类型为X,则需要从其他两个对象生成URL。但是,model.Url
不会在分配接收者时被分配。我应该如何修复它才能同时工作?
private async Task GenerateUrl(Model model)
{
var urlModel = GetUrlByOrganizationId(model.OrgId);
var org = GetOrg(model.Id);
model.Url = brand.Domain + org.Name;
await Task.FromResult(model.Url);
}
private async Task<Email> GenerateEmail(User receiver, Guid brandId)
{
var model = new Model()
{
title = "",
Organization = organization,
Url = string.Empty
};
try
{
await model.SetReceiverAsync(receiver, 0, model.Locale, model.Dialect);
await this.GenerateUrl(model);
}
catch (Exception exc)
{
this.Log(LogLevels.Warning, exc.Message);
return null;
}
// Send email
var email = Sender.RenderModel(template, model);
return email;
}
我想在model.Url
行中使用var email = Sender.RenderModel(template, model);
答案 0 :(得分:1)
目前尚不清楚为什么将GenerateUrl
定义为async
方法并返回Task
的原因。如果仅设置模型的Url
属性,则应按以下方式定义:
private void GenerateUrl(Model model)
{
var urlModel = GetUrlByOrganizationId(model.OrgId);
var org = GetOrg(model.Id);
model.Url = brand.Domain + org.Name;
}
并在没有await
的情况下呼叫:
this.GenerateUrl(model);
如果您打算从方法中返回URL,则返回类型应为string
或Task<string>
(尽管考虑到当前的实现,后者没有多大意义):
private async Task<string> GenerateUrl(Model model)
{
var urlModel = GetUrlByOrganizationId(model.OrgId);
var org = GetOrg(model.Id);
model.Url = brand.Domain + org.Name;
return Task.FromResult(model.Url);
}
private async string GenerateUrl(Model model)
{
var urlModel = GetUrlByOrganizationId(model.OrgId);
var org = GetOrg(model.Id);
model.Url = brand.Domain + org.Name;
return model.Url;
}