質問

I am getting the following exception when calling a WCF service. The service structure was generated from .wsdl received from company.

This happens when I throw an FaultException.

Here is the structure of the webservice in the interface for all parts. Operation,response faultcontract type etc.

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

Here is the Fault abstract class

///

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

and then the 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
{
}

and finally the 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
{
}

and here is the operationcontract where the FaultException gets thrown

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

Here is the code that throws the exception

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

When i comment this line out it works.

xmlVFE.fault = new BlockedMeterEx();

When it is in I get an exception in the trace. The exception is below

Replying to an operation threw an exception

Type 'BlockedMeterEx' with data contract name 'BlockedMeterEx:http://schemas.datacontract.org/2004/07/' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

I have tried adding Knowntype but I am not sure where exactly it should be and how it works?

役に立ちましたか?

解決

I guess it will be enough to change ServiceKnownTypeAttribute here to

[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
{
//...
}

to 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
{
//...
}

alternatively you can provide ServiceKnownType(typeof(BlockedMeterEx)) on your service declaration:

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

read more: http://msdn.microsoft.com/en-us/library/ms730167(v=vs.110).aspx

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top