如何在客户端 ASP.NET MVC 3/4 中使用 WCF RESTfull 服务?我已经实现了带有列表的返回类型的示例 RESTfull 服务
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(UriTemplate = "/mycities", ResponseFormat = WebMessageFormat.Json)]
List<string> GetData();
[OperationContract]
[WebGet(UriTemplate = "/mycities1/{cityId}",ResponseFormat =WebMessageFormat.Json)]
string GetData1(string cityId);
[OperationContract]
[WebGet(UriTemplate = "/XMLCity", ResponseFormat = WebMessageFormat.Xml)]
List<string> GetDataXML();
[OperationContract]
[WebGet(UriTemplate = "/EmployeesXML", ResponseFormat = WebMessageFormat.Xml)]
List<Employee> GetEmployeeXML();
}
public List<string> GetData()
{
List<string> cities = new List<string>();
cities.Add("Ahamadabad");
cities.Add("Chennai");
cities.Add("Bengaluru");
cities.Add("Delhi");
cities.Add("Kolkata");
cities.Add("Mumbai");
return cities;
}
public string GetData1(string cityId)
{
List<string> cities = new List<string>();
cities.Add("Ahamadabad");
cities.Add("Chennai");
cities.Add("Bengaluru");
cities.Add("Delhi");
cities.Add("Kolkata");
cities.Add("Mumbai");
ShoppingCartAPIEF.ShoppingCartAdminEntities db = new ShoppingCartAPIEF.ShoppingCartAdminEntities();
var customer = db.Customers.ToList();
var id = Convert.ToInt16( cityId);
var cust = from c in customer
where c.Id == Convert.ToInt16(cityId)
select c;
return cust.Select(l => new { l.LastName }).FirstOrDefault().LastName;
}
public List<string> GetDataXML()
{
List<string> cities = new List<string>();
cities.Add("Ahamadabad");
cities.Add("Chennai");
cities.Add("Bengaluru");
cities.Add("Delhi");
cities.Add("Kolkata");
cities.Add("Mumbai");
return cities;
}
public List<Employee> GetEmployeeXML()
{
List<Employee> employees = new List<Employee>();
Employee emp = new Employee { EmpId = 100, FirstName = "JaiSankar", LastName = "Navarathinam" };
Employee emp1 = new Employee { EmpId = 101, FirstName = "Vimmal", LastName = "Lodaya" };
Employee emp2 = new Employee { EmpId = 102, FirstName = "Ganapathy", LastName = "Thayanidhi" };
//new Employee { EmpId = 103, FirstName = "Sivakumar", LastName = "LaxmanaPerumal" };
//new Employee { EmpId = 104, FirstName = "Gajendra", LastName = "Gowda" };
employees.Add(emp);
employees.Add(emp1);
employees.Add(emp2);
return employees;
}
客户端应用程序:控制台:
方法一:我们消费WCF应用时,在客户端添加服务引用,调用如下。这就像普通的 WCF 应用程序
ServiceReference1.Service1Client client1 = new ServiceReference1.Service1Client();
var name = client1.GetData1("2");
//Output: single name
var cityNames = client1.GetData();
//Output: List of strings
var employees = client1.GetEmployeeXML();
//OutPut: List of Employees
方法2:如果是普通的WCF应用,上述方法就可以了,所以我尝试如下使用WCF RESTfull服务
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:57278");
//JSON with list of strings
HttpResponseMessage response = client.GetAsync("Service1.svc/XMLService/mycities").Result;
string json = response.Content.ReadAsStringAsync().Result;
//OutPut: ["Ahamadabad","Chennai","Bengaluru","Delhi","Kolkata","Mumbai"]
//XML with List of strings
HttpResponseMessage responseXML = client.GetAsync("Service1.svc/XMLService/XMLCity").Result;
var xml = responseXML.Content.ReadAsStringAsync().Result;
//XmlDocument doc = new XmlDocument();
//doc.LoadXml(xml);
//输出:AhamadabadChennaiBengaluru德里加尔各答孟买
//XML with Employee object
HttpResponseMessage responseXML1 = client.GetAsync("Service1.svc/XMLService/EmployeesXML").Result;
var xml1 = responseXML1.Content.ReadAsStreamAsync().Result;
//返回 ((System.IO.MemoryStream)(xml1)) }
在这里,我只有 JSON 字符串列表或 ArrayOfstring Xml 标记即将到来。如何在客户端将 JSON 转换为字符串列表或 Employee 对象。
我的问题是如何从字符串列表或 AraryOfString 或 MemoryStream 进行进一步操作。
提前感谢杰。