声明不同程序集中数据协定的已知类型
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的错。 我已经重新尝试过,上面发布的两个解决方案实际上都有效。 我认为这是在运行测试之前没有更新我的测试项目上的服务引用的情况 - 哎呀!!!
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!!!