Trying to add a service reference results in Bad Request (400) in one project, otherwise runs fine

后端 未结 3 1453
被撕碎了的回忆
被撕碎了的回忆 2020-12-07 06:06

I\'m in a delicate situation: As the title suggests, I can\'t seem to connect to a WCF service I wrapped up in a Windows Service. I followed the tutorial http://msdn.microso

3条回答
  •  我在风中等你
    2020-12-07 06:32

    For anyone who is encountering similar issues without detailed error, I'll describe what my problem was about. In my Visual Studio project, I used a class similar to the following:

    public class Info
    {
      public string Key { get; set; }
      public string Value { get; set; }
    
      public Info(string key, string value)
      {
        this.Key = key;
        this.Value = value;
      }
    }
    

    The difference to the other project (which worked) is that this POCO has a constructor that takes parameters, thus doesn't provide a standard constructor without any arguments. But this is needed in order for Info objects to be serialized (I hope I use that term correctly here) and deserialized. So in order to make it work, either just add a standard constructor which may just do nothing, or (perhaps better) use the following instead:

    [DataContract]
    public class Info
    {
      [DataMember]
      public string Key { get; set; }
      [DataMember]
      public string Value { get; set; }
    
      public Info(string key, string value)
      {
        this.Key = key;
        this.Value = value;
      }
    }
    

    I didn't think about the DataContract part before, because I didn't use any custom constructor and just initialized the objects like

    Info info = new Info { Key = "a", Value = "b" };
    

提交回复
热议问题