在异步控制器中使用同步操作 [英] Using synchonous operations in an asynchronous controller

查看:86
本文介绍了在异步控制器中使用同步操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS 2013和4.5.1框架以及MVC5。

I'm using VS 2013 with the 4.5.1 framework and MVC5.

我有一个Web服务,其中包含从客户端MVC控制器调用的方法。其中大多数是异步的,少数是同步的。 **我的客户端控制器中的设计时编译错误是同步的。可以
任何人告诉我原因吗?**

I have a web service which contains methods that are called from the client MVC controllers. Most of them are asynchronous and a few are synchronous. **I am getting design time compile errors in my client controllers for the ones which are synchronous. Can anybody tell me why?**

这是我的后端网络服务上的异步和同步操作的代码。

Here is my code for an async and sync operation on my backend web service.

   公共类CategoryController:AsyncController

    public class CategoryController : AsyncController

** ASYNC **

**ASYNC**

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;        }       }

    **SYNC**       public void AddCategory(Category cat)       {        try        {         using (YeagerTechEntities DbContext = new YeagerTechEntities())         {              Category category = new Category();              category.Description = cat.Description;              DbContext.Categories.Add(category);          DbContext.SaveChanges();         }        }        catch (Exception)        {         throw;        }       }

 

以下是我的Controller中前端的代码。 ** ASYNC **

Here is the code on the front-end in my Controller. **ASYNC**

    

    

 [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;                 }

** SYNC **

**SYNC**

   

   

 [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));                     }

我得到了典型的"最佳重载方法匹配"的设计时编译错误。 "有一些无效的参数"这很容易理解。

I get the design time compile error of the typical "The best overloaded method match" "Has some invalid arguments" which is easy enough to understand.

如果我点击"AddCategory"方法,有一个蓝色下划线表示:为"AddCategory"生成方法存根。在YeagerTech.YeagerTechWcfService.YeagerTechWcfServiceClient"

If I click the "AddCategory" method, there is a blue underscore which states: Generate method stub for "AddCategory" in YeagerTech.YeagerTechWcfService.YeagerTechWcfServiceClient"

如果我这样做,它会在我的Reference.cs文件中添加一个内部方法,代理在添加典型的"未实现"服务引用时生成;创建时方法体中的错误。但是,对于Reference.cs文件中的AddCategory和EditCategory方法,已经有两种方法
。如果我遵守添加存根的话,它会抱怨该方法已经存在且理所当然。

If I do that, it adds an internal method in my Reference.cs file that the proxy generated when adding the service reference with a typical "Not implemented" error in the body of the method when it gets created. However, there are already two methods each for the AddCategory and EditCategory methods in my Reference.cs file. If I comply with adding the stub, it complains that the method already exists and rightfully so.

以下是在代理中生成的两种方法。第一个是同步,第二个是异步:

Here are the two methods that got generated in the proxy. The first one is sync and the second one is async:

   

   

 [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);

**我需要做些什么来解决这个问题?**

**What do I need to do in order to resolve this?**

Bill Yeager

Bill Yeager

推荐答案

Hello Bill_Yeager,

Hello Bill_Yeager,

由于此问题更多关于ASP.NET,我建议您将其发布到ASP.NET论坛:

Since this issue is more regarding ASP.NET, I suggest you posting it to the ASP.NET forum:

http:// forums。 asp.net/1146.aspx

感谢您的理解。

 


这篇关于在异步控制器中使用同步操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆