I want to make sure I have the correct code in place for an asynchronous call to my web service. If someone can please help, I would appreciate it very much.
I am using Visual Studio 2013 with the 4.5.1 framework.
I have the following code in my client application making a call to the service:
public async Task<ActionResult> Read([DataSourceRequest]DataSourceRequest request)
{
try
{
YeagerTechWcfService.Status[] status = await db.GetStatusesAsync();
var serializer = new JavaScriptSerializer();
var result = new ContentResult();
serializer.MaxJsonLength = Int32.MaxValue;
result.Content = serializer.Serialize(status.ToDataSourceResult(request));
result.ContentType = "application/json";
return result;
}
I have the following in my WCF Service Contract for the above method call:
[ServiceContract]
public interface IYeagerTechWcfService
{
[OperationContract]
Task<List<Status>> GetStatusesAsync();
I have the following in my web service class that implements the interface class for the same method:
public class YeagerTechWcfService : IYeagerTechWcfService
{
public async Task<List<Status>> GetStatusesAsync()
{
try
{
using (YeagerTechEntities DbContext = new YeagerTechEntities())
{
DbContext.Configuration.ProxyCreationEnabled = false;
DbContext.Database.Connection.Open();
var status = await DbContext.Status.ToListAsync();
return status;
}
}
catch (Exception)
{
throw;
}
}
According to what I was further reading, I would also need the following placed after my OperationContract attribute: *Is this true?*
[OperationContractAttribute(AsyncPattern = true)]
IAsyncResult BeginGetStatusesAsync(AsyncCallback callback, object asyncState);
string EndGetStatusesAsync(IAsyncResult result);