I'm trying to create an asynchronous method in my WCF service, but am getting the above subject error message. I simply want to return a list of Categories.
To be clear, tack on the following to the above error message at the end: YeagerTechModel.Category[]
The wcfclient service was added successfully except for this method (which has a red "X" next to it). In actuality, all of the methods (in the wcf service client) expect an associated asynchronous method (with red "X") in conjunction with the rest of the regular synchronous methods.
I know how to use WCF, but am not an expert in it. What do I need to do in order to get this functioning correctly?
I am using the full blown release version of VisualStudio 2013 and have the project using the 4.5.1 framework. I am also using EntityFramework 6.
My service contract has the following namespace included.
using System.Threading.Tasks;
My operation contract is as follows.
[OperationContract(Name="GetCategoriesAsync")]
Task<List<Category>> GetCategoriesAsync();
My DataContract is as follows:
namespace YeagerTechModel
{
using System;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Collections.Generic;
[Serializable, DataContract(IsReference = true)]
public partial class Category
{
public Category()
{
this.Projects = new HashSet<Project>();
}
[DataMember]
public short CategoryID { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public virtual ICollection<Project> Projects { get; set; }
}
}
The method in my web service is as follows. Note that I don't have any design time compile errors and the solution is able to be built successfully:
public async Task<List<Category>> GetCategoriesAsync()
{
try
{
using (YeagerTechEntities DbContext = new YeagerTechEntities())
{
DbContext.Configuration.ProxyCreationEnabled = false;
DbContext.Database.Connection.Open();
var category = await DbContext.Categories.ToListAsync();
return category;
}
}
catch (Exception ex)
{
throw ex;
}
}
Why are you making your service async? Async methods are created on the client side with the service reference. When you add a service reference to your service, mark the service reference as allow generation of asynchronous operations. With VS 2013 and .NET 4.5 you can either make task-based operations or asynchronous operations.
Then your client can call the async method to return a task and then future await the task to get the return object. Or call the begin method with a callback for when it finished the begin method call. Assumed is that you want client async calls. Those are made client side and not server side in the service code.
Example is shown on this post: http://www.codeguru.com/columns/experts/building-and-consuming-async-wcf-services-in-.net-framework-4.5.htm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With