获取"文件结束"将复杂类型传递给WCF服务时

时间:2015-01-25 13:45:12

标签: jquery ajax json wcf

我尝试使用带有以下数据的jquery ajax POST来调用WCF服务:

  

数据=   " {\" StudentID \":\" 18 \" \"消息\":{\"头\& #34;:{\" EMAILID \":\" 0 \" \"由\":{\"地址类型\& #34;:\" 0 \" \" AliasType \":\" 0 \" \"名称\&#34 ;:\"   教师\" \" StudentID \":\" 18 \" \" SymbolURL \":\"老师。 PNG \" \"地址\":\" 18 \"},\"为了\":{\"地址类型\":\" 0 \" \" AliasType \":\" 0 \" \"名称\ ":\"伊恩   \" \" StudentID \":\" 346 \" \" SymbolURL \":\" \&# 34; \"地址\":\" 346 \"},\" TimeRecieved \":\" /日期(0) / \"},\"版本\":\" 1 \" \" MailMessageXML \":\" \ " \" CompressedBody \":\" \"},\"映像文件名称\":\" \&#34 ;}"

我收到错误(回复正文):

  

{" ExceptionDetail" {" HELPLINK":空,"的InnerException" {" HELPLINK":空,"的InnerException" {" HELPLINK":空,"的InnerException":空,"消息":"的意外   文件结尾。"," StackTrace":"在   System.Xml.EncodingStreamWrapper.ReadBOMEncoding(布尔   notOutOfBand)\ u000d \ u000a at   System.Xml.EncodingStreamWrapper..ctor(Stream stream,Encoding   编码)\ u000d \ u000a

...

这是我的WCF功能:

    [DataContract]
    public class MailHeader
    {
        [DataMember] public Int64 EMailID;
        [DataMember] public MailAddress From;
        [DataMember] public MailAddress To;
        [DataMember] public DateTime TimeRecieved;
    }

    [DataContract]
    public class MailAddress
    {
        [DataMember] public Int64 AddressType;
        [DataMember] public Int64 AliasType;
        [DataMember] public string Name;
        [DataMember] public Int64 StudentID;
        [DataMember] public string SymbolURL;
        [DataMember] public string Address;
    }

    [DataContract]
    public class MailMessage
    {
        [DataMember] public MailHeader Header;
        [DataMember] public Int64 version;
        [DataMember] public XElement MailMessageXML;
        [DataMember] public byte[] CompressedBody;
    }

SendMessageStatusType SendMessage(Int64 StudentID, MailMessage Message,string ImageFileName);

This is my servicemodel from web.config:   
<system.serviceModel/>
    <behaviors>
      <endpointBehaviors>
        <behavior name="AjaxBehavior">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>

      <serviceBehaviors>
        <behavior name="metadataGetBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" ></serviceDebug>
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <bindings>
      <customBinding>
        <binding name="binaryEncoding" >
          <httpTransport />
        </binding>
      </customBinding>
    </bindings>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />


    <services>
      <service name="XXXX.Web.XXXXSVC" behaviorConfiguration="metadataGetBehavior">
        <endpoint address="ajax" behaviorConfiguration="AjaxBehavior"
            binding="webHttpBinding" contract="XXXX.Web.IXXXXSVC" />
        <endpoint address="binary"
          binding="customBinding" bindingConfiguration="binaryEncoding"
          contract="XXXX.Web.IXXXXSVC" />
      </service>
    </services>

  </system.serviceModel>

jquery调用很简单,与其他适用于我的调用相同:

$.ajax({

    type: "POST", 
    url: Url, // Location of the service
    data: Data, //Data sent to server
    contentType: "application/json; charset=utf-8",
    processdata: false, //True or False
    success: function (msg) {

        //On Successfull service call

        successCallBackFunc(msg);

    },

    error: errorCallBackFunc  // When Service call fails

});

为什么它不起作用的任何想法?

1 个答案:

答案 0 :(得分:0)

确定。我弄清楚了:

当你发送一个类型为XElement的字段时,它不能是一个空字符串,它必须是正确的XML格式。

同样适用于类型byte [],它必须在列表中:[0]