从JS / TS(Angular 2)向WCF发送一组对象?

时间:2017-08-08 18:57:45

标签: javascript c# angular wcf typescript

我必须发送像这样的对象数组

[{"Cod":"1"},{"Cod":"5"}]

到我今天使用的C#WCF服务没有wsHttpBinding问题(所以这里没有使用JSON,只有XML)。

请求POST包含此参数:

   let param = "<cod>" + data + "</cod>";

我的问题出现在WCF上,因为我找不到从方法参数中获取该对象数组的方法:

public string GetArrayOfObjects(CompositeType[] cod)
    {//implementation not important because i can't enter here...}

其中CompositeType为:

 [DataContract]
public class CompositeType
{
    string cod;

    [DataMember]
    public string Cod
    {
        get { return cod; }
        set { cod = value; }
    }
}

我尝试了很多东西,例如将参数更改为:

  • Object[] cod
  • List<string> cod
  • ...

每次我收到此错误(翻译):

Exception generated by the formatter in an attempt to deserialize the message: Error trying to deserialize the parameter http://tempuri.org/:cod.  InnerException: 'Error at line 1 position 341. Expected status 'Element' .. Found 'Text' with '' name space ''. '.  
...

这是我发送的帖子:

"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
  "<s:Envelope xmlns:a=\"http://www.w3.org/2005/08/addressing\" xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\">" +
  "<s:Header>" +
  "<a:Action s:mustUnderstand=\"1\">http://tempuri.org/IService1/" + GetArrayOfObjects + "</a:Action>" +
  "</s:Header>" +
  "<s:Body>" +
  "<" + GetArrayOfObjects + " xmlns=\"http://tempuri.org/\">" +
  param +
  "</" + GetArrayOfObjects + ">" +
  "</s:Body>" +
  "</s:Envelope>";

之前定义param。 param中data的JSON.stringify给出了这个问题的第一个例子([{"Cod":"1"},{"Cod":"5"}])。这个帖子的消息总是没有单值参数。

有些网站在wcf上显示了一个带有json反序列化器的解决方案,但我正在使用XML ...现在不能选择在json中重新实现该项目。

我试了一天,但我找不到解决方案,我很想听听你的一些解决方案!谢谢大家。

0 个答案:

没有答案