使用 WCF WebApi 的异步 REST 服务

发布于 2024-12-28 08:06:49 字数 522 浏览 3 评论 0原文

我想知道各位开发人员对 WCF WebApi 服务有何看法。

在 N 层应用程序中,我们可以拥有多层服务。我们可以让服务使用来自外部服务的数据。在这种情况下,值得使用 WCF 4.0 创建异步休息服务。

public interface IService
{
   [OperationContractAttribute(AsyncPattern = true)]
   IAsyncResult BeginGetStock(string code, AsyncCallback callback, object asyncState);
    //Note: There is no OperationContractAttribute for the end method.
    string EndGetStock(IAsyncResult result); 
}

但随着 WCF WebApi 的发布,这种方法仍然需要吗?创建异步服务?

如何在 IIS/WAS/自托管 中托管它们,

期待建议和评论。

I want to know what is the opinion of you fellow Developers regarding WCF WebApi services.

In an N-tier application we can have multiple layers of services. We can have services consuming data from external services. In that scenario its worth to create Async Rest Services using WCF 4.0.

public interface IService
{
   [OperationContractAttribute(AsyncPattern = true)]
   IAsyncResult BeginGetStock(string code, AsyncCallback callback, object asyncState);
    //Note: There is no OperationContractAttribute for the end method.
    string EndGetStock(IAsyncResult result); 
}

But with the release of WCF WebApi this approach is still required? to create async services?

How to host them in IIS/WAS/Self Hosting

looking forward for suggestion and comments.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一袭水袖舞倾城 2025-01-04 08:06:49

嗯,我的感觉是,为了在最新的 WCF WebAPI(预览版 6)中创建异步操作,我仍然可以使用相同的模式(开始/结束),但我也可以使用任务编程模型来创建异步操作,这很多更简单。

下面显示了使用任务模型编写的异步操作的一个示例。

    [WebGet]
    public Task<Aggregate> Aggregation()
    {
        // Create an HttpClient (we could also reuse an existing one)
        HttpClient client = new HttpClient();

        // Submit GET requests for contacts and orders
        Task<List<Contact>> contactsTask = client.GetAsync(backendAddress + "/contacts").ContinueWith<Task<List<Contact>>>((responseTask) =>
            {
                return responseTask.Result.Content.ReadAsAsync<List<Contact>>();
            }).Unwrap();
        Task<List<Order>> ordersTask = client.GetAsync(backendAddress + "/orders").ContinueWith<Task<List<Order>>>((responseTask) =>
            {
                return responseTask.Result.Content.ReadAsAsync<List<Order>>();
            }).Unwrap();

        // Wait for both requests to complete
        return Task.Factory.ContinueWhenAll(new Task[] { contactsTask, ordersTask },
            (completedTasks) =>
            {
                client.Dispose();
                Aggregate aggregate = new Aggregate() 
                { 
                    Contacts = contactsTask.Result,
                    Orders = ordersTask.Result
                };

                return aggregate;
            });
    }

    [WebGet(UriTemplate = "contacts")]
    public Task<HttpResponseMessage> Contacts()
    {
        // Create an HttpClient (we could also reuse an existing one)
        HttpClient client = new HttpClient();

        // Submit GET requests for contacts and return task directly
        return client.GetAsync(backendAddress + "/contacts");
    }

Well What i feel,In order to create asynchronous operations in the latest WCF WebAPIs (preview 6) I can still use same pattern (Begin/End), but I can also use the Task programming model to create asynchronous operations, which is a lot simpler.

One example of an asynchronous operation written using the task model is shown below.

    [WebGet]
    public Task<Aggregate> Aggregation()
    {
        // Create an HttpClient (we could also reuse an existing one)
        HttpClient client = new HttpClient();

        // Submit GET requests for contacts and orders
        Task<List<Contact>> contactsTask = client.GetAsync(backendAddress + "/contacts").ContinueWith<Task<List<Contact>>>((responseTask) =>
            {
                return responseTask.Result.Content.ReadAsAsync<List<Contact>>();
            }).Unwrap();
        Task<List<Order>> ordersTask = client.GetAsync(backendAddress + "/orders").ContinueWith<Task<List<Order>>>((responseTask) =>
            {
                return responseTask.Result.Content.ReadAsAsync<List<Order>>();
            }).Unwrap();

        // Wait for both requests to complete
        return Task.Factory.ContinueWhenAll(new Task[] { contactsTask, ordersTask },
            (completedTasks) =>
            {
                client.Dispose();
                Aggregate aggregate = new Aggregate() 
                { 
                    Contacts = contactsTask.Result,
                    Orders = ordersTask.Result
                };

                return aggregate;
            });
    }

    [WebGet(UriTemplate = "contacts")]
    public Task<HttpResponseMessage> Contacts()
    {
        // Create an HttpClient (we could also reuse an existing one)
        HttpClient client = new HttpClient();

        // Submit GET requests for contacts and return task directly
        return client.GetAsync(backendAddress + "/contacts");
    }
放我走吧 2025-01-04 08:06:49

WCF Web API 附带完全异步的 HttpClient 实现,您可以在 IIS 中托管,也可以完全在 sefhost 中托管。

对于异步 REST“服务”场景,请阅读“慢速 REST

WCF Web API comes with an completely async HttpClient implementation and you can host in IIS and also completely sefhost.

For a async REST "service" scenario please read "Slow REST"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文