简体   繁体   English

异步调用wcf 4.5 WCF服务

[英]calling a wcf 4.5 WCF service asynhronously

I want to make sure I have the correct code in place for an asynchronous call to my web service. 我想确保我有正确的代码来异步调用Web服务。 If someone can please help, I would appreciate it very much. 如果有人可以帮助,我将非常感谢。

I am using Visual Studio 2013 with the 4.5.1 framework . 我将Visual Studio 2013与4.5.1框架一起使用

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: 我的WCF服务合同中有以下用于上述方法调用的内容:

[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: 我的Web服务类中具有以下内容,该类为同一方法实现了接口类:

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? 根据我的进一步阅读,我还需要在OperationContract属性后放置以下内容: * 这是真的吗? * *

[OperationContractAttribute(AsyncPattern = true)]
    IAsyncResult BeginGetStatusesAsync(AsyncCallback callback, object asyncState);

    string EndGetStatusesAsync(IAsyncResult result);

No, AsyncPattern is no longer required as of .NET 4.5. 不,从.NET 4.5开始,不再需要AsyncPattern

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). 我有一个博客文章Async WCF Today and Tomorrow ,将异步WCF的旧方法(博客文章:.NET 4.0中的“ Today”)与异步WCF的新方法(博客.NET 4.5中的“ Tomorrow”)进行了比较。 )。

Your WCF service doesn't have to support asynchronous methods; 您的WCF服务不必支持异步方法。 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! 简单!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM