Using synchonous operations in an asynchronous controller RRS feed

  • Question

  • User-1355356054 posted

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

    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.

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


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



     [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.

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

    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?**

    Wednesday, November 20, 2013 12:06 PM