在控制台中学习WCF RESTful,主持人,
我的步骤:
我试过WCF(不是REST)控制台托管的WebAPI,一切都好。
最后,stackoverflow.com
请帮助
模型
[Serializable]
public abstract class Building
{
public Manufacturer Manufacturer { get; set; }
}
[Serializable]
public class Manufacturer
{
public string Name { get; set; }
public string Telephone { get; set; }
}
[Serializable]
public class Furniture : Building
{
public string Name { get; set; }
}
[Serializable]
public class Reception
{
public int Floor { get; set; }
public int Number { get; set; }
}
[Serializable]
public class Room : Building
{
public string Number { get; set; }
public List<Furniture> Furnitures { get; set; }
}
[Serializable]
public class Hotel : Building
{
public Guid Guid { get; set; }
public List<Reception> Receptions { get; set; }
public List<Room> Rooms { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
合同
[ServiceContract]
public interface IHotel
{
// Create objct Hotel
[OperationContract]
[WebInvoke(UriTemplate = "", Method = "POST", RequestFormat= WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json)]
bool Create(Hotel hotel);
}
服务
public class HotelService : I Hotel
{
public bool Build(Models.Hotel hotel)
{
if (hotel == null)
return false;
// codes here is object hotel(EF) creation, test OK
return true;
}
}
主机(控制台)
WebServiceHost serviceHost = new WebServiceHost(typeof(Demo.Services.HotelService), new Uri("http://192.168.1.101/HotelService"));
ServiceEndpoint endpoint = serviceHost.AddServiceEndpoint(typeof(Demo.Contracts.IHotel), new WebHttpBinding(), "");
ServiceDebugBehavior sdb = serviceHost.Description.Behaviors.Find<ServiceDebugBehavior>();
sdb.HttpHelpPageEnabled = false;
Console.WriteLine("Starting Service...");
// start service
serviceHost.Open();
Console.WriteLine("Started, press RETURN to exit.");
Console.ReadLine();
serviceHost.Close();
客户端(Winform的)
呼叫者
public bool BuildHotel(string json)
{
WebRequest request = HttpWebRequest.Create("http://192.168.1.101/HotelService");
request.ContentType = "application/json";
byte[] data = Encoding.UTF8.GetBytes(json);
request.ContentLength = data.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
WebResponse response = request.GetResponse(); // 400 throwed here
Stream responseStream = response.GetResponseStream();
StreamReader responseStreamReader = new StreamReader(responseStream);
string result = responseStreamReader.ReadToEnd();
return true;
}
在
上的'bool BuildHotel(string)'的Json字符串{
"Guid":"ea59c011-d656-4870-b29b-30a44e668560",
"Receptions":[
{"Floor":1,"Number":1},
{"Floor":2,"Number":2}
],
"Rooms":[
{
"Number":"c",
"Furnitures":[
{"Name":"1","Manufacturer":{"Name":"1","Telephone":"1"}},
{"Name":"2","Manufacturer":{"Name":"2","Telephone":"2"}}
],
"Manufacturer":{"Name":"c","Telephone":"c"}
}
],
"Name":"x",
"Address":"x",
"Manufacturer":{"Name":"x","Telephone":"x"}
}
答案 0 :(得分:0)
它期望这种JSON:
{&#34; K_ BackingField&#34; {&#34; k _BackingField&#34;:&#34; 4&#34;&#34; K_ BackingField& #34;:&#34; 4&#34;}&#34; k _BackingField&#34;:&#34; X&#34;&#34; K_ BackingField&#34 ;: &#34; ea59c011-d656-4870-B29B-30a44e668560&#34;&#34; k _BackingField&#34;:&#34; X&#34;&#34; K_ BackingField&# 34;:[{&#34; k _BackingField&#34;:1,&#34; K_ BackingField&#34;:1},{&#34; k _BackingField&#34 ;:2&#34; K_ BackingField&#34;:2}],&#34; k _BackingField&#34;:[{&#34; K_ BackingField&#34 ;: {&#34; k _BackingField&#34;:&#34; 3&#34;&#34; K_ BackingField&#34;:&#34; 3&#34;}&#34 ; k _BackingField&#34;:[{&#34; K_ BackingField&#34; {&#34; k _BackingField&#34;:&#34; 1&#34 ;, &#34; K_ BackingField&#34;:&#34; 1&#34;}&#34; k _BackingField&#34;:&#34; 1&#34;},{&# 34; K_ BackingField&#34; {&#34; k _BackingField&#34;:&#34; 2&#34;&#34; K_ BackingField&#34;:&# 34; 2&#34;}&#34; k _BackingField&#34;:&#34; 2&#34;}],&#34; k__BackingField&#34;:空}]}
要更改它,您可以使用DataContract和DataMember属性标记所有数据合同:
[DataContract]
public abstract class Building
{
[DataMember]
public Manufacturer Manufacturer { get; set; }
}
在这种情况下,它会理解你在问题中给我们的json,并会成功处理它。