如何在ASP.NET应用程序中使用WCF restful服务 [英] How to consume WCF restful service in ASP.NET application

查看:79
本文介绍了如何在ASP.NET应用程序中使用WCF restful服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个WCF Restful服务来从SQL Server 2008获取数据并在IIS服务器中托管服务。当我访问IIS中托管的服务时,它正常工作从数据库中获取结果并显示结果在浏览器中以XML格式。问题是当我在另一个ASP.Net Web应用程序中使用该服务作为服务引用时,我无法访问该服务中的方法。我已经检查了Web配置文件并对绑定,合同,地址进行了更改,但在进行了更改后,它显示错误

I've created a WCF Restful service to get the data from the SQL server 2008 and hosted the service in IIS sever.When I access the service hosted in the IIS its working fine get the result from the database and display the result in browser in XML format. The problem is when I use the service in the another ASP.Net web application as service reference I cannot access the method in the service. I've checked in the web config file and made changes in bindings,contracts,address but after made changes it shows the error that "

The remote server returned an error: (404) Not Found.





我尝试过:



Made更改配置文件中的绑定和其他内容

"

What I have tried:

Made changes in the configuration file for the bindings and other things

推荐答案

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("http://localhost:9000/");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    // New code:
    HttpResponseMessage response = await client.GetAsync("api/products/1");
    if (response.IsSuccessStatusCode)
    {
        Product product = await response.Content.ReadAsAsync>Product>();
        Console.WriteLine("{0}\t


{1} \t {2},product.Name,product.Price,product.Category);
}
}
{1}\t{2}", product.Name, product.Price, product.Category); } }







更多细节请参考从ASP.NET Web API中的.NET客户端调用Web API 2(C#)| ASP.NET站点


这篇关于如何在ASP.NET应用程序中使用WCF restful服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆