声明不同程序集中数据协定的已知类型

发布于 2024-07-25 04:30:26 字数 1615 浏览 5 评论 0原文

我在 WCF 服务中有一个方法,它返回一个复杂类型 (myComplexResult),其中包括一个 List (Of Common.myBaseClass) 作为其成员之一。 我希望这个列表包含可以是 Foo.myClass1 和 Bar.myClass2 类型的项目,这两者都继承自 Common.myBaseClass。 请注意,所有这些类都是在不同的程序集中定义的。

该服务抛出此异常:

类型 'Foo.myClass1' 带有数据合约名称 'myClass1:http://mynamespace/foo/' 预计不会。 添加任何类型不 静态已知已知列表 类型 - 例如,通过使用 KnownTypeAttribute 属性或通过 将它们添加到已知类型列表中 传递给 DataContractSerializer。

好的,所以我意识到我需要将 myClass1 和 myClass2 声明为 myBaseClass 的已知类型,以便 DataContractSerializer 知道如何处理它们。 我无法做明显的事情并使用 myClass1 和 myClass2 的 KnownType 属性来装饰 myBaseClass 类,因为这意味着添加对 Foo 和 Bar 程序集的引用,从而导致循环依赖。

我希望在我的配置文件中使用declaredTypes,我尝试了这个:

<system.runtime.serialization >
    <dataContractSerializer >
        <declaredTypes >
            <add type ="Common.myBaseClass, Common">
                <knownType type ="Foo.myClass1, Foo" />
                <knownType type ="Bar.myClass2, Bar" />
            </add>
        </declaredTypes>
    </dataContractSerializer>
</system.runtime.serialization>

这似乎没有帮助,所以我尝试向 myComplexResult 添加 KnownType 属性:

  <DataContract(name:="myComplexResult", [namespace]:="http://mynamespace/coo/")> _
    <KnownType(GetType(Foo.myClass1))> _
    Public Class myComplexResult
        <DataMember(name:="myList")> _
        Public myList As List(Of Common.myBaseClass)

但我仍然收到相同的错误。 帮助! 我该怎么办?

I have a method in a WCF service which returns a complex type (myComplexResult), which includes as one of its members a List (Of Common.myBaseClass). I want this list to hold items which can variously be of type Foo.myClass1 and Bar.myClass2, both of which inherit from Common.myBaseClass. Note that all of these classes are defined in different assemblies.

The service throws this exception:

Type
'Foo.myClass1'
with data contract name
'myClass1:http://mynamespace/foo/'
is not expected. 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.

Okay, so I realise that I need to declare myClass1 and myClass2 as known types of myBaseClass so that the DataContractSerializer knows what to do with them. I can't do the obvious thing and decorate the myBaseClass class with KnownType attributes for myClass1 and myClass2, since this would mean adding references to the Foo and Bar assemblies, which causes a circular dependency.

I was hoping to use declaredTypes in my config file and I tried this:

<system.runtime.serialization >
    <dataContractSerializer >
        <declaredTypes >
            <add type ="Common.myBaseClass, Common">
                <knownType type ="Foo.myClass1, Foo" />
                <knownType type ="Bar.myClass2, Bar" />
            </add>
        </declaredTypes>
    </dataContractSerializer>
</system.runtime.serialization>

That didn't seem to help, so I tried to add a KnownType attribute to myComplexResult:

  <DataContract(name:="myComplexResult", [namespace]:="http://mynamespace/coo/")> _
    <KnownType(GetType(Foo.myClass1))> _
    Public Class myComplexResult
        <DataMember(name:="myList")> _
        Public myList As List(Of Common.myBaseClass)

But I'm still getting the same error. Help! What do I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲸落 2024-08-01 04:30:26

我的错。 我已经重新尝试过,上面发布的两个解决方案实际上都有效。 我认为这是在运行测试之前没有更新我的测试项目上的服务引用的情况 - 哎呀!!!

My fault. I've re-tried and both of the solutions I posted above actually do work. I think this is a case of simply not updating the service reference on my test project before running the test - whoops!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文