扩展 Web 服务代理类以在 C# 中添加额外的方法 [英] Extending web service proxy classes to add extra methods in c#

查看:23
本文介绍了扩展 Web 服务代理类以在 C# 中添加额外的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am consuming a WCF service, in operation contract for that service I would like to add extra functionality.

所以在这种情况下,Proxy 类有一个合约 executeCreatePaperClipTransactionexecuteCreateLoanIncrease 作为单独的类.我想要的是executeCreateLoanIncrease 应该是父类的子对象 executeCreatePaperClipTransaction.

So here is the case, Proxy class has a contract executeCreatePaperClipTransaction and executeCreateLoanIncrease as separate classes. What I want is executeCreateLoanIncrease should be child object of parent class executeCreatePaperClipTransaction.

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

我试图在此处放置最少且有用的代码,因为这两个代理类都包含许多属性.

I am trying to put minimal and useful code over here as both proxy classes contain a lot of properties.

来自 WSDL 的原始代理类:

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

我的代码:

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

当我尝试创建自定义类的对象并将该对象传递给代理操作合同时,它给我一个错误提示类型不是预期的.使用 XmlInclude 或 SoapInclude 属性来指定不是静态已知."

When I try to create an object of my custom class and pass that object to proxy operation contract it gives me an error saying "The type was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically."

  • 我认为这可能是问题所在:

当我序列化 req 对象时,它会生成如下 xml:

When i serialize req object it generate xml as below:

<?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>

我注意到我不想要 ObjLoanIncreaseRequest XML 标记.当我删除该标签并直接从 SoapUI 执行相同的 XML 时.成功了! :)

I noticed that i do not want ObjLoanIncreaseRequest XML tag. When I remove that tag and execute the same XML directly from SoapUI. It worked! :)

知道我在代码中遗漏了什么吗?

Any idea what I am missing in the code?

推荐答案

我想通了.

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

Instead of extending the proxy DataContract, I could add it in the existing class. As it is a partial class. I just have to make sure that namespace of WSDL and custom class matches.

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

这篇关于扩展 Web 服务代理类以在 C# 中添加额外的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆