WCF中子类缺少发送字段-试图获得多态性 [英] Lack of sent field from subclass in WCF - trying to gain polymorphism

查看:46
本文介绍了WCF中子类缺少发送字段-试图获得多态性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先请记住,我在Google上关注了很多关于该主题的内容.
我正在使用 WCF 公开一些服务.我有类似的东西:

First of all keep in mind, that I followed a very much stuff in google about this subject.
I am using WCF, to exposing some services. I have something like:

[DataContract]
[KnownType(typeof(Sub))]
public class Base
{
    [DataMember]
    public int base;

}

[DataContract]
public class Sub : Base
{
    [DataMember]
    public int sub;
}

[ServiceContract]
[ServiceKnownType(typeof(Sub))]
public interface IServices
{
    [OperationContract]
    public void test(Base b);
}

我希望能够以XML的形式发送 Sub 对象和 Base 对象.当我在 b test(Base b)的第一行中断调试器时,看不到 sub 字段.

I would like to be able, as XML, send both Sub object and Base object. When I break with debugger in first line of test(Base b) in b I can't see sub field.

问题是:

<?xml version="1.0"?>

<soapenv:Envelope
xmlns:xs="http://www.w3.org/2003/05/soap-envelope/"
soapenv:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
   <soapenv:Header/>
   <soapenv:Body>
      <xs:test>
         <xs:b>
           <xs:base>123</xs:base>
           <xs:sub>1234</xs:sub>
        </xs:b>
   </xs:test>
 </soapenv:Body>
</soapenv:Envelope>

此XML已成功反序列化,但是在对象中我只能看到 base 字段(等于 123 ),但是看不到 sub .
我在哪里错了?

This XML is successfully deserialized, but in object I can see only base field (equals to 123), however I can't see field sub.
Where Am I wrong ?

推荐答案

据我了解,您想手动将正确的XML发送到您的服务并使其能够识别Sub实体.在对您的问题的评论中,我提到了KnownTypes发现的方法,但它正在解决另一种问题.

As I understand, you want to manually send correct XML to your service and make it recognize the Sub entity. In my comments to your question I mentioned approach with KnownTypes discovery, but it is solving another kind of problems.

您当前的C#代码看起来还不错,但是您缺少XML中的某些类型属性.它应该看起来像这样:

Your current C# code looks ok, but you are missing some type attributes in the XML. It should look like this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:tem="http://tempuri.org/" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:wcf="http://schemas.datacontract.org/2004/07/<PUT THE C# NAMESPACE OF YOUR ENTITIES HERE>">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:Test>
         <tem:b xsi:type="wcf:Sub">
            <wcf:base>1</wcf:base>
            <wcf:sub>2</wcf:sub>
         </tem:b>
      </tem:Test>
   </soapenv:Body>
</soapenv:Envelope>

我刚刚对其进行了测试,并且在由VS 2015创建并通过SoapUI测试的WCF项目中运行良好.子值和基值都达到了测试方法,并且在其中可以使用子实体.

I just tested it and it worked fine in a WCF project created with VS 2015 and tested with SoapUI. Both sub and base values reached the test method and Sub entity was available inside of it.

关于StackOverflow的类似问题,具有相同的xsi:type解决方案,也在此处.

A similar question on StackOverflow with the same xsi:type solution also given here.

如果这还不够用,我可以将演示项目上载到GitHub上的某个地方.

If this does not help enough, I can upload the demo project somewhere on GitHub.

这篇关于WCF中子类缺少发送字段-试图获得多态性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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