Reputation: 4692
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);
Upvotes: 2
Views: 3481
Reputation: 457217
No, AsyncPattern
is no longer required as of .NET 4.5.
I have a blog post Async WCF Today and Tomorrow that compares the old way of asynchronous WCF ("Today" in the blog post: .NET 4.0) with the new way of asynchronous WCF ("Tomorrow" in the blog post: .NET 4.5).
Upvotes: 5
Reputation: 33149
Your WCF service doesn't have to support asynchronous methods; the client generation (Add Service Reference) can generate client-side asynchronous call handling for you.
When adding a Service Reference to your client application, just tick the Generate asynchronous operations checkbox and your proxy class will contain the asynchronous versions of the calls. Easy!
Upvotes: 1