2

学习 WCF RESTful,在控制台中托管,

我的步骤:

  1. 创建示例模型
  2. 创建服务合同
  3. 创建服务
  4. 在控制台中托管此服务。
  5. 运行这个主机,看起来不错。
  6. 创建一个winform,通过json post使用服务地址到服务主机。
  7. 我希望它会工作,但返回 http 400。

我尝试了 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"}
}
4

1 回答 1

0

它需要这样的 JSON:

{" k_BackingField":{" k_BackingField":"4"," k_BackingField":"4"}," k_BackingField":"x"," k_BackingField":"ea59c011-d656-4870-b29b-30a44e668560 "," k_BackingField":"x"," k_BackingField":[{" k_BackingField":1," k_BackingField":1},{" k_BackingField":2," k_BackingField":2}], " k_BackingField":[{" k_BackingField":{" k_BackingField":"3"," k_BackingField":"3"}," k_BackingField":[{" k_BackingField":{" k_BackingField":"1"," k_BackingField":"1"}," k_BackingField":"1"},{" k_BackingField":{" k_BackingField":"2"," k_BackingField":"2"}, “k_BackingField":"2"}],"k__BackingField":null}]}

要更改它,您可以使用 DataContract 和 DataMember 属性标记所有数据合同:

    [DataContract]
    public abstract class Building
    {
        [DataMember]
        public Manufacturer Manufacturer { get; set; }
    }

在这种情况下,它将理解您在问题中给我们的 json 并成功处理它。

于 2014-04-12T11:15:19.390 回答