2017-05-11 32 views
5

我正在编写基于旧soap服务的WSDL的WCF服务。我使用svcutil来生成服务合同并做了一些更改以匹配我所托管的站点,但是当我从测试客户端调用服务时,请求对象将作为空值进入。使用XmlSerializerFormat时WCF服务请求为空

该服务正在返回的响应也正在序列化不正确,我敢肯定是相关的。但我不明白为什么它没有正确的序列化和反序列化。

这里的服务合同:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")] 
[System.ServiceModel.ServiceContractAttribute(Namespace = ApuConstants.Namespace)] 
public interface IApuService 
{ 
    [System.ServiceModel.OperationContractAttribute(Action = "*", ReplyAction = "*")] 
    [System.ServiceModel.XmlSerializerFormatAttribute(Style = System.ServiceModel.OperationFormatStyle.Rpc, Use = System.ServiceModel.OperationFormatUse.Encoded)] 
    [System.ServiceModel.ServiceKnownTypeAttribute(typeof(Part))] 
    [return: System.ServiceModel.MessageParameterAttribute(Name = "return")] 
    Brock.Web.Apu.Response check(Brock.Web.Apu.Request request); 
} 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "4.0.30319.33440")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.SoapTypeAttribute(Namespace = ApuConstants.Namespace)] 
public partial class Request 
{ 

    private RequestHeader headerField; 

    private Lookup lookupField; 

    /// <remarks/> 
    public RequestHeader header 
    { 
     get 
     { 
      return this.headerField; 
     } 
     set 
     { 
      this.headerField = value; 
     } 
    } 

    /// <remarks/> 
    public Lookup lookup 
    { 
     get 
     { 
      return this.lookupField; 
     } 
     set 
     { 
      this.lookupField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "4.0.30319.33440")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.SoapTypeAttribute(Namespace = ApuConstants.Namespace)] 
public partial class RequestHeader 
{ 

    private string accountField; 

    private string idField; 

    /// <remarks/> 
    public string account 
    { 
     get 
     { 
      return this.accountField; 
     } 
     set 
     { 
      this.accountField = value; 
     } 
    } 

    /// <remarks/> 
    public string id 
    { 
     get 
     { 
      return this.idField; 
     } 
     set 
     { 
      this.idField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "4.0.30319.33440")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.SoapTypeAttribute(Namespace = ApuConstants.Namespace)] 
public partial class Part 
{ 

    private string oemField; 

    private string hicField; 

    private string skuField; 

    private string descField; 

    private int daysField; 

    private string availField; 

    private string branchField; 

    private float listField; 

    private float netField; 

    private string typeField; 

    private string certField; 

    private string statusField; 

    /// <remarks/> 
    public string oem 
    { 
     get 
     { 
      return this.oemField; 
     } 
     set 
     { 
      this.oemField = value; 
     } 
    } 

    /// <remarks/> 
    public string hic 
    { 
     get 
     { 
      return this.hicField; 
     } 
     set 
     { 
      this.hicField = value; 
     } 
    } 

    /// <remarks/> 
    public string sku 
    { 
     get 
     { 
      return this.skuField; 
     } 
     set 
     { 
      this.skuField = value; 
     } 
    } 

    /// <remarks/> 
    public string desc 
    { 
     get 
     { 
      return this.descField; 
     } 
     set 
     { 
      this.descField = value; 
     } 
    } 

    /// <remarks/> 
    public int days 
    { 
     get 
     { 
      return this.daysField; 
     } 
     set 
     { 
      this.daysField = value; 
     } 
    } 

    /// <remarks/> 
    public string avail 
    { 
     get 
     { 
      return this.availField; 
     } 
     set 
     { 
      this.availField = value; 
     } 
    } 

    /// <remarks/> 
    public string branch 
    { 
     get 
     { 
      return this.branchField; 
     } 
     set 
     { 
      this.branchField = value; 
     } 
    } 

    /// <remarks/> 
    public float list 
    { 
     get 
     { 
      return this.listField; 
     } 
     set 
     { 
      this.listField = value; 
     } 
    } 

    /// <remarks/> 
    public float net 
    { 
     get 
     { 
      return this.netField; 
     } 
     set 
     { 
      this.netField = value; 
     } 
    } 

    /// <remarks/> 
    public string type 
    { 
     get 
     { 
      return this.typeField; 
     } 
     set 
     { 
      this.typeField = value; 
     } 
    } 

    /// <remarks/> 
    public string cert 
    { 
     get 
     { 
      return this.certField; 
     } 
     set 
     { 
      this.certField = value; 
     } 
    } 

    /// <remarks/> 
    public string status 
    { 
     get 
     { 
      return this.statusField; 
     } 
     set 
     { 
      this.statusField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "4.0.30319.33440")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.SoapTypeAttribute(Namespace = ApuConstants.Namespace)] 
public partial class ResponseHeader 
{ 

    private string statusField; 

    private string reasonField; 

    private string accountField; 

    private string idField; 

    /// <remarks/> 
    public string status 
    { 
     get 
     { 
      return this.statusField; 
     } 
     set 
     { 
      this.statusField = value; 
     } 
    } 

    /// <remarks/> 
    public string reason 
    { 
     get 
     { 
      return this.reasonField; 
     } 
     set 
     { 
      this.reasonField = value; 
     } 
    } 

    /// <remarks/> 
    public string account 
    { 
     get 
     { 
      return this.accountField; 
     } 
     set 
     { 
      this.accountField = value; 
     } 
    } 

    /// <remarks/> 
    public string id 
    { 
     get 
     { 
      return this.idField; 
     } 
     set 
     { 
      this.idField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "4.0.30319.33440")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.SoapTypeAttribute(Namespace = ApuConstants.Namespace)] 
public partial class Response 
{ 

    private ResponseHeader headerField; 

    private Part[] itemsField; 

    /// <remarks/> 
    public ResponseHeader header 
    { 
     get 
     { 
      return this.headerField; 
     } 
     set 
     { 
      this.headerField = value; 
     } 
    } 

    /// <remarks/> 
    public Part[] items 
    { 
     get 
     { 
      return this.itemsField; 
     } 
     set 
     { 
      this.itemsField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "4.0.30319.33440")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.SoapTypeAttribute(Namespace = ApuConstants.Namespace)] 
public partial class Lookup 
{ 

    private string oemField; 

    private string hicField; 

    private int qtyField; 

    private string zipField; 

    /// <remarks/> 
    public string oem 
    { 
     get 
     { 
      return this.oemField; 
     } 
     set 
     { 
      this.oemField = value; 
     } 
    } 

    /// <remarks/> 
    public string hic 
    { 
     get 
     { 
      return this.hicField; 
     } 
     set 
     { 
      this.hicField = value; 
     } 
    } 

    /// <remarks/> 
    public int qty 
    { 
     get 
     { 
      return this.qtyField; 
     } 
     set 
     { 
      this.qtyField = value; 
     } 
    } 

    /// <remarks/> 
    public string zip 
    { 
     get 
     { 
      return this.zipField; 
     } 
     set 
     { 
      this.zipField = value; 
     } 
    } 
} 

和实现:

[ServiceBehavior(Namespace = ApuConstants.Namespace)] 
public class ApuService : IApuService 
{ 
    private readonly IApuServiceHandler _handler; 
    private readonly ISettingService _settingService; 

    public ApuService() 
    { 
     _handler = EngineContext.Current.Resolve<IApuServiceHandler>(); 
     _settingService = EngineContext.Current.Resolve<ISettingService>(); 
    } 

    public Response check(Request request) 
    { 
     if (Authorized(request)) 
      return _handler.ProcessRequest(request); 
     return _handler.ErrorResponse("Invalid credentials"); 
    } 

    protected bool Authorized(Request request) 
    { 
     if (request == null || request.header == null) 
      return false; 

     var settings = _settingService.LoadSetting<ApuSettings>(0); 

     if (!string.Equals(request.header.account, settings.Username, StringComparison.InvariantCultureIgnoreCase)) 
      return false; 

     if (!string.Equals(request.header.id, settings.Password)) 
      return false; 

     return true; 
    } 
} 

配置:

<configuration> 
    <system.web> 
    <compilation targetFramework="4.5.1" /> 
    </system.web> 

    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="ApuBehavior"> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="true"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <bindings> 
    </bindings> 
    <services> 
     <service name="Apu.WebService.ApuService" behaviorConfiguration="ApuBehavior"> 
     <endpoint address="" binding="basicHttpBinding" contract="Web.Apu.IApuService" bindingNamespace="http://www.testurl.com/apu" /> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     </service> 
    </services> 
    </system.serviceModel> 
</configuration> 

由服务接收(从OperationContext.Current.RequestContext请求.RequestMessage):

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Header xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://localhost:15555/Plugins/Brock.Apu/Remote/ApuService.svc</To> 
    </s:Header> 
    <Body> 
     <check xmlns="http://www.testurl.com/apu"> 
      <request> 
       <header> 
        <account>apu</account> 
        <id>apu001!</id> 
       </header> 
       <lookup> 
        <hic>323-01327</hic> 
        <oem>5014351AB</oem> 
        <qty>2</qty> 
        <zip>85304</zip> 
       </lookup> 
      </request> 
     </check> 
    </Body> 
</Envelope> 

由服务(其被不正确地串行化)发送的响应:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Body s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
     <q1:checkResponse xmlns:q1="http://www.testurl.com/apu"> 
      <return href="#id1"/> 
     </q1:checkResponse> 
     <q2:Response id="id1" xsi:type="q2:Response" xmlns:q2="http://www.testurl.com/apu"> 
      <header href="#id2"/> 
     </q2:Response> 
     <q3:ResponseHeader id="id2" xsi:type="q3:ResponseHeader" xmlns:q3="http://www.testurl.com/apu"> 
      <status xsi:type="xsd:string">no</status> 
      <reason xsi:type="xsd:string">Invalid credentials</reason> 
      <account xsi:type="xsd:string"/> 
      <id xsi:type="xsd:string"/> 
     </q3:ResponseHeader> 
    </s:Body> 
</s:Envelope> 
+0

当有人说'做了一些改变'时,我倾向于认为这是问题所在。 –

+0

你检出了wsdl.exe吗? https://msdn.microsoft.com/en-us/library/7h3ystb6(v=vs.100).aspx。旧的.asmx服务用于代理生成。取决于服务的年龄,您可能会得到不同的结果。 –

+0

@JssDwt - 完全没有。我能够进一步修改服务以使用Data Contract序列化,并且它工作得很好。但是,我需要它来使用XML序列化。问题不在于修改。它知道修改它的正确方法。 当我们开始说我们不能改变旧的(或在这种情况下是预生成的)代码来满足我们的需求时,对于编程世界来说这是一个糟糕的风景。 –

回答

0

后续服务:原来我发布的服务合同就好了。这是请求的格式错误。而不是一个简单的内联XML结构,解串期待这样的:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soapenv:Body> 
     <ns1:check xmlns:ns1="http://www.testurl.com/apu" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
      <request href="#id0"/> 
     </ns1:check> 
     <multiRef xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="http://www.testurl.com/apu" id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:Request"> 
      <header href="#id1"/> 
      <lookup href="#id2"/> 
     </multiRef> 
     <multiRef xmlns:ns3="http://www.testurl.com/apu" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" id="id1" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns3:RequestHeader"> 
      <account xsi:type="xsd:string"></account> 
      <id xsi:type="xsd:string"></id> 
     </multiRef> 
     <multiRef xmlns:ns4="http://www.testurl.com/apu" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" id="id2" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns4:Lookup"> 
      <oem xsi:type="xsd:string">6C3Z13008BB</oem> 
      <hic xsi:type="xsd:string"/> 
      <qty href="#id3"/> 
      <zip xsi:type="xsd:string">70764</zip> 
     </multiRef> 
     <multiRef xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" id="id3" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="xsd:int">1</multiRef> 
    </soapenv:Body> 
</soapenv:Envelope> 

我有客户登录的原始请求通过他们的客户越来越发出后想通了这一点。

1

它看起来像Response是一个局部类。有没有另一部分,也许在另一个文件?那个其他作品是可序列化的吗? ResponseHeaderPart同样的问题?

我通常使用装饰者[Serializable]

Part类中的public float list关键字列表可能存在问题。

+0

任何部分都没有其他部分。数据对象已经被标记为Serializable([System.SerializableAttribute()])。 C#是一种区分大小写的语言,因此小写的“列表”不会与List类混淆。也不是“List”和“list”是c#中的保留关键字。 –