“不希望输入数据合同名称为y的x”

时间:2014-05-14 21:43:27

标签: c# web-services wcf soap datacontract

调用WCF服务时出现以下异常。服务结构是从公司收到的.wsdl生成的。

当我抛出FaultException时会发生这种情况。

以下是所有部件界面中Web服务的结构。操作,响应故障合同类型等。

[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "4.0.30319.18020")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace ="http://www.nrs.eskom.co.za/xmlvend/base/2.1/schema")]
public partial class XMLVendFaultResp : BaseResp 
{

private Fault faultField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order = 0)]
public Fault fault
{
    get
    {
        return this.faultField;
    }
    set
    {
        this.faultField = value;
    }
}
}

这是故障抽象类

///

[System.Xml.Serialization.XmlIncludeAttribute(typeof(BlockedMeterEx))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "4.0.30319.18020")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(BlockedMeterEx))]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.nrs.eskom.co.za/xmlvend/base/2.1/schema")]
public abstract partial class Fault
{

private string descField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order = 0)]
public string desc
{
    get
    {
        return this.descField;
    }
    set
    {
        this.descField = value;
    }
}
}

然后是商业规则

[System.Xml.Serialization.XmlIncludeAttribute(typeof(BlockedMeterEx))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "4.0.30319.18020")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.nrs.eskom.co.za/xmlvend/base/2.1/schema")]
public partial class BusinessRuleEx : Fault
{
}

最后是BlockedMeterEX

[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "4.0.30319.18020")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.nrs.eskom.co.za/xmlvend/base/2.1/schema")]
public partial class BlockedMeterEx : BusinessRuleEx
{
}

这里是抛出FaultException的operationcontract

    [System.ServiceModel.OperationContractAttribute(Action = "", ReplyAction = "*")]
    [System.ServiceModel.FaultContractAttribute(typeof(www.nrs.eskom.co.za.xmlvend.@base._2._11.schema.XMLVendFaultResp), Action = "", Name = "xmlvendFaultResp", Namespace = "http://www.nrs.eskom.co.za/xmlvend/base/2.1/schema")]
    [System.ServiceModel.XmlSerializerFormatAttribute()]
    [System.ServiceModel.ServiceKnownTypeAttribute(typeof(NonMeterSpecificTokenIssue))]
    [System.ServiceModel.ServiceKnownTypeAttribute(typeof(MeterSpecificTokenIssue))]
    [System.ServiceModel.ServiceKnownTypeAttribute(typeof(Receipt))]
    [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BaseReq))]
    ConfirmCustomerRequestResponse ConfirmCustomerRequest(ConfirmCustomerRequestRequest request);

以下是抛出异常的代码

XMLVendFaultResp xmlVFE = new XMLVendFaultResp();
xmlVFE.fault = new BlockedMeterEx();
//xmlVFE.fault.desc = " Blocked"; 
xmlVFE.operatorMsg =   " Blocked"; 
throw new FaultException<XMLVendFaultResp>(resp, new FaultReason(vr.ResultDescription));

当我评论这一行时,它有效。

xmlVFE.fault = new BlockedMeterEx();

当它出现时,我在跟踪中得到一个异常。例外情况如下

  

回复某项操作引发了异常

     

Type&#39; BlockedMeterEx&#39;数据合同名称&#39; BlockedMeterEx:http://schemas.datacontract.org/2004/07/&#39;不是预期的。考虑使用DataContractResolver或将任何静态未知的类型添加到已知类型列表中 - 例如,通过使用KnownTypeAttribute属性或将它们添加到传递给DataContractSerializer的已知类型列表中。

我尝试添加Knowntype但我不确定它应该在哪里以及它是如何工作的?

1 个答案:

答案 0 :(得分:1)

我想将ServiceKnownTypeAttribute更改为

就足够了
[System.Xml.Serialization.XmlIncludeAttribute(typeof(BlockedMeterEx))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "4.0.30319.18020")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(BlockedMeterEx))]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.nrs.eskom.co.za/xmlvend/base/2.1/schema")]
public abstract partial class Fault
{
//...
}

到KnownTypeAttribute

[System.Xml.Serialization.XmlIncludeAttribute(typeof(BlockedMeterEx))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "4.0.30319.18020")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.ServiceModel.KnownTypeAttribute(typeof(BlockedMeterEx))]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.nrs.eskom.co.za/xmlvend/base/2.1/schema")]
public abstract partial class Fault
{
//...
}

或者您可以在服务声明上提供ServiceKnownType(typeof(BlockedMeterEx)):

[ServiceKnownType(typeof(BlockedMeterEx))]
[ServiceContract]
public interface IMyService
{
//...
}

了解详情:http://msdn.microsoft.com/en-us/library/ms730167(v=vs.110).aspx