【问题标题】:How to consume REST-based services asynchronously with WCF?如何使用 WCF 异步使用基于 REST 的服务?
【发布时间】:2011-10-14 08:15:04
【问题描述】:

我想创建一个代理类,它支持针对基于 REST 的服务的异步操作。

为了便于讨论,假设我有一个服务,IService

[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebGet(UriTemplate = "{a}/{b}")]
    void Action(string a, string b);
}

我可以轻松地创建一个同步代理类:

class Client: ClientBase<IService>, IService
{
    public void Action(string a, string b)
    {
        Channel.Action(a, b);
    }
}

(此技术来自article

是否有类似直接的方法使代理支持异步操作(BeginAction/EndActionActionAsync 模式)?还是手动滚动我自己的最佳做法?

请注意,我无法在 Visual Studio 中添加服务引用,因为没有可用的元数据。

【问题讨论】:

    标签: c# wcf rest asynchronous wcf-client


    【解决方案1】:

    如果您将操作合同替换为相应的开始/结束对,它也应该适用于基于 REST 的合同。您甚至可以在客户端上同时拥有同步和异步版本的操作(但在这种情况下,您只需要在同步版本上拥有 [WebGet] 属性)。

    public class StackOverflow_6846215
    {
        [ServiceContract(Name = "ITest")]
        public interface ITest
        {
            [OperationContract]
            [WebGet]
            int Add(int x, int y);
        }
        [ServiceContract(Name = "ITest")]
        public interface ITestClient
        {
            [OperationContract(AsyncPattern = true)]
            IAsyncResult BeginAdd(int x, int y, AsyncCallback callback, object state);
            int EndAdd(IAsyncResult asyncResult);
    
            [OperationContract]
            [WebGet]
            int Add(int x, int y);
        }
        public class Client : ClientBase<ITestClient>, ITestClient
        {
            public Client(string baseAddress)
                :base(new WebHttpBinding(), new EndpointAddress(baseAddress))
            {
                this.Endpoint.Behaviors.Add(new WebHttpBehavior());
            }
    
            public IAsyncResult BeginAdd(int x, int y, AsyncCallback callback, object state)
            {
                return this.Channel.BeginAdd(x, y, callback, state);
            }
    
            public int EndAdd(IAsyncResult asyncResult)
            {
                return this.Channel.EndAdd(asyncResult);
            }
    
            public int Add(int x, int y)
            {
                return this.Channel.Add(x, y);
            }
        }
        public class Service : ITest
        {
            public int Add(int x, int y)
            {
                return x + y;
            }
        }
        public static void Test()
        {
            string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
            WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
            host.Open();
            Console.WriteLine("Host opened");
    
            Client client = new Client(baseAddress);
            Console.WriteLine("Sync result: {0}", client.Add(66, 77));
            client.BeginAdd(44, 55, delegate(IAsyncResult ar)
            {
                int result = client.EndAdd(ar);
                Console.WriteLine("Async result: {0}", result);
            }, null);
    
            Console.WriteLine("Press ENTER to close the host");
            Console.ReadLine();
            host.Close();
        }
    }
    

    【讨论】:

    • 谢谢,这正是我想要的。很高兴我没有自己动手:)。
    猜你喜欢
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    相关资源
    最近更新 更多