不需要输入带有数据契约名称y的x

本文关键字:契约 输入带 数据 不需要 | 更新日期: 2023-09-27 18:03:29

在调用WCF服务时,我得到以下异常。服务结构是根据从company收到的.wsdl生成的。

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

这是所有部分的接口中webservice的结构。操作、响应、故障、合同类型等

[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;
    }
}
}

下面是Fault抽象类

///

[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;
    }
}
}

然后是businessrules

[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的操作契约

    [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();

当它在时,我在跟踪中得到一个异常。

下面有例外

响应操作抛出异常

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

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

不需要输入带有数据契约名称y的x

我想把这里的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