扩展Web服务代理类以在c#中添加额外的方法

时间:2017-08-03 16:01:52

标签: c# xml wcf

我正在使用WCF服务,在该服务的操作合同中我想添加额外的功能。

所以在这种情况下,Proxy类将合同website_urlexecuteCreatePaperClipTransaction作为单独的类。我想要的是executeCreateLoanIncrease 应该是父类的子对象 executeCreateLoanIncrease

我这样做的原因是,我使用的WSDL由一些供应商管理,他没有提供将此字段添加为DataContract的一部分。但这同样适用于XML请求。

我试图在这里放置最小和有用的代码,因为两个代理类都包含很多属性。

来自WSDL的原始代理类

executeCreatePaperClipTransaction

我的代码:

public partial class executeCreatePaperClipTransaction : object, System.ComponentModel.INotifyPropertyChanged {

    private CreatePaperClipTransactionType createPaperClipTransactionField; 

    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=0)]
    public CreatePaperClipTransactionType CreatePaperClipTransaction {
        //Getter and Setter Methods; 
    }   
}

public partial class CreatePaperClipTransactionType : object, System.ComponentModel.INotifyPropertyChanged {

    private string effectiveDateField;          

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string effectiveDate {
       //Getter and Setter Methods; 
    }
}

public partial class executeCreateLoanIncrease : object, System.ComponentModel.INotifyPropertyChanged {

    private CreateLoanIncreaseType createLoanIncreaseField;

    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=0)]
    public CreateLoanIncreaseType CreateLoanIncrease {
         //Getter and Setter Methods; 
    }
}

public partial class CreateLoanIncreaseType : object, System.ComponentModel.INotifyPropertyChanged {

    private string loanAliasField;

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string loanAlias {
         //Getter and Setter Methods; 
    }
}

当我尝试创建自定义类的对象并将该对象传递给代理操作契约时,它给出了一个错误“该类型不是预期的。使用XmlInclude或SoapInclude属性指定不是静态地知道。

  • 我假设这可能是问题所在:

当我序列化[XmlType("executeCreatePaperClipTransaction")] public partial class CustomExecuteCreatePaperClipTransaction : executeCreatePaperClipTransaction { [XmlElement(ElementName = "CreatePaperClipTransaction", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)] public CustomCreatePaperClipTransactionType ObjCreatePaperClipTransaction { get; set; } } public partial class CustomCreatePaperClipTransactionType : CreatePaperClipTransactionType { [XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)] public executeCreateLoanIncrease ObjLoanIncreaseRequest { get; set; } } 对象时,它会生成xml,如下所示:

req

我注意到我不想要<?xml version="1.0" encoding="utf-16"?> <executeCreatePaperClipTransaction xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <CreatePaperClipTransaction /* SomeAttributesAreHere */> <ObjLoanIncreaseRequest> <CreateLoanIncrease loanAlias="N00001" /> </ObjLoanIncreaseRequest> </CreatePaperClipTransaction> </executeCreatePaperClipTransaction> XML标记。当我删除该标记并直接从SoapUI执行相同的XML时。 有效!:)

知道我在代码中缺少什么吗?

1 个答案:

答案 0 :(得分:1)

我想出来了。

我可以将其添加到现有类中,而不是扩展代理DataContract。因为它是一个部分类。我只需要确保WSDL和自定义类的名称空间匹配。

namespace MyProject.Service // This is important 
{
    public partial class executeCreatePaperClipTransaction
    {        
        [XmlElement(ElementName = "CreatePaperClipTransaction", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]
        public CustomCreatePaperClipTransactionType ObjCreatePaperClipTransaction { get; set; }        
    }   

    public partial class CreatePaperClipTransactionType
    {
        [XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]
        public executeCreateLoanIncrease ObjLoanIncreaseRequest { get; set; }
    }
}