C# 在异步控制器中使用同步操作
我将VS2013与4.5.1框架和MVC5一起使用 我有一个web服务,其中包含从客户端MVC控制器调用的方法。它们大多数是异步的,少数是同步的对于同步的客户端控制器,我的客户端控制器中出现了设计时编译错误。有人能告诉我为什么吗? 下面是我在后端web服务上执行异步和同步操作的代码C# 在异步控制器中使用同步操作,c#,wcf,asynchronous,asp.net-mvc-5,C#,Wcf,Asynchronous,Asp.net Mvc 5,我将VS2013与4.5.1框架和MVC5一起使用 我有一个web服务,其中包含从客户端MVC控制器调用的方法。它们大多数是异步的,少数是同步的对于同步的客户端控制器,我的客户端控制器中出现了设计时编译错误。有人能告诉我为什么吗? 下面是我在后端web服务上执行异步和同步操作的代码 public class CategoryController : AsyncController 异步 public async Task<Category> GetCategoryIDAsync(I
public class CategoryController : AsyncController
异步
public async Task<Category> GetCategoryIDAsync(Int16 categoryid)
{
try
{
using (YeagerTechEntities DbContext = new YeagerTechEntities())
{
DbContext.Configuration.ProxyCreationEnabled = false;
DbContext.Database.Connection.Open();
var categoryEntity = await DbContext.Categories.Include("Projects").FirstOrDefaultAsync(f => f.CategoryID == categoryid);
Category category = null;
if (categoryEntity != null)
{
category = new Category();
category.CategoryID = categoryEntity.CategoryID;
category.Description = categoryEntity.Description;
category.Projects = categoryEntity.Projects;
}
else
throw new Exception("Category " + categoryid + " not found!");
return category;
}
}
catch (Exception)
{
throw;
}
}
[Authorize(Roles = "Admin, AdminStage")]
public async Task<ActionResult> SelectCategoryProjects([DataSourceRequest]DataSourceRequest request, string intCategoryID)
{
if (Session["Culture"] != null)
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Session["Culture"].ToString());
try
{
YeagerTechWcfService.Project[] projectList = await db.GetProjectByCategoryIDAsync(Convert.ToInt16(intCategoryID));
var serializer = new JavaScriptSerializer();
var result = new ContentResult();
serializer.MaxJsonLength = Int32.MaxValue;
result.Content = serializer.Serialize(projectList.ToDataSourceResult(request));
result.ContentType = "application/json";
return result;
}
[HttpPost]
[Authorize(Roles = "Admin, AdminStage")]
public ActionResult UpsertCategory([DataSourceRequest]DataSourceRequest request, Category cat, Boolean blnInsert)
{
if (TryUpdateModel(cat))
{
try
{
if (blnInsert)
db.AddCategory(cat);
else
db.EditCategory(cat);
return Json(new[] { cat }.ToDataSourceResult(request, ModelState));
}
这是我的控制器前端的代码
异步
public async Task<Category> GetCategoryIDAsync(Int16 categoryid)
{
try
{
using (YeagerTechEntities DbContext = new YeagerTechEntities())
{
DbContext.Configuration.ProxyCreationEnabled = false;
DbContext.Database.Connection.Open();
var categoryEntity = await DbContext.Categories.Include("Projects").FirstOrDefaultAsync(f => f.CategoryID == categoryid);
Category category = null;
if (categoryEntity != null)
{
category = new Category();
category.CategoryID = categoryEntity.CategoryID;
category.Description = categoryEntity.Description;
category.Projects = categoryEntity.Projects;
}
else
throw new Exception("Category " + categoryid + " not found!");
return category;
}
}
catch (Exception)
{
throw;
}
}
[Authorize(Roles = "Admin, AdminStage")]
public async Task<ActionResult> SelectCategoryProjects([DataSourceRequest]DataSourceRequest request, string intCategoryID)
{
if (Session["Culture"] != null)
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Session["Culture"].ToString());
try
{
YeagerTechWcfService.Project[] projectList = await db.GetProjectByCategoryIDAsync(Convert.ToInt16(intCategoryID));
var serializer = new JavaScriptSerializer();
var result = new ContentResult();
serializer.MaxJsonLength = Int32.MaxValue;
result.Content = serializer.Serialize(projectList.ToDataSourceResult(request));
result.ContentType = "application/json";
return result;
}
[HttpPost]
[Authorize(Roles = "Admin, AdminStage")]
public ActionResult UpsertCategory([DataSourceRequest]DataSourceRequest request, Category cat, Boolean blnInsert)
{
if (TryUpdateModel(cat))
{
try
{
if (blnInsert)
db.AddCategory(cat);
else
db.EditCategory(cat);
return Json(new[] { cat }.ToDataSourceResult(request, ModelState));
}
我得到了典型的“最佳重载方法匹配”“有一些无效参数”的设计时编译错误,这很容易理解
如果单击“AddCategory”方法,则会出现一条蓝色下划线,表示:
为YeagerTech.YeagerTechWcfService.YeagerTechWcfServiceClient中的“AddCategory”生成方法存根
如果我这样做,它会在Reference.cs文件中添加一个内部方法,代理在添加带有典型“未实现”的服务引用时生成该方法“方法体在创建时出错。但是,在my Reference.cs文件中,AddCategory和EditCategory方法已经各有两个方法。如果我遵守添加存根的规定,它会抱怨该方法已经存在,而且是理所当然的
下面是在代理中生成的两个方法。第一个是同步的,第二个是异步的:
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IYeagerTechWcfService/AddCategory", ReplyAction="http://tempuri.org/IYeagerTechWcfService/AddCategoryResponse")]
void AddCategory(YeagerTech.YeagerTechWcfService.Category cat);
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IYeagerTechWcfService/AddCategory", ReplyAction="http://tempuri.org/IYeagerTechWcfService/AddCategoryResponse")]
System.Threading.Tasks.Task AddCategoryAsync(YeagerTech.YeagerTechWcfService.Category cat);
我需要做什么才能解决此问题?尝试将
AsyncController
更改为Controller
<不再需要代码>异步控制器 通过改变进入方法的参数来解决它
以前,它是“YeagerTechModel.Category”类型的。我将其更改为“YeagerTechWCDfsService.Category”(由代理生成),从而解决了设计时编译错误
感谢你们两位为我指明了正确的方向:)
以下是我将其更新为的代码:
[HttpPost]
[Authorize(Roles = "Admin, AdminStage")]
public ActionResult UpsertCategory([DataSourceRequest]DataSourceRequest request, YeagerTechWcfService.Category cat, Boolean blnInsert)
{
if (TryUpdateModel(cat))
{
try
{
if (blnInsert)
db.AddCategory(cat);
else
db.EditCategory(cat);
return Json(new[] { cat }.ToDataSourceResult(request, ModelState));
}
有没有可能有两个
Category
类在四处走动?PS-这真的有益吗catch(Exception){throw;}
model项目中的方法具有名称空间:“YeagerTechModel.Category”。从服务返回时,为了检索数据,我需要指定以下代码:“YeagerTechWcfService.Category[]categoryList=await db.GetCategoriesAsync();”如果我尝试使用此代码“YeagerTechModel.Category[]=await db.GetCategoriesAsync();”,我收到错误:无法将类型“YeagerTech.YeagerTechWcfService.Category[]”隐式转换为“YeagerTechModel.Category[]”。如果您注意到,我确实声明代理生成了上面的posts代码,用于保存数据。有什么想法吗?通常情况下,如果两个类位于同一层次结构中,并且可以一次将一个类转换为另一个类,那么数组就不能如此自动地转换。注意:在同一层次结构中有两个同名的类是完全错误的,所以在这种情况下它们不应该相关。我按照建议进行了更改。但是,我得到的消息是:与“YeagerTech.YeagerTechWcfService.YeagerTechWcfServiceClient.AddCategory(YeagerTech.YeagerTechWcfService.Category)”匹配的最佳重载方法具有一些无效参数。