在 WCF 服务中序列化 MethodBase 和异常类型
我创建了一个用于记录异常的 WCF 服务(我意识到如果网络出现故障,事情将不会被记录...有后备措施)
无论如何,它有两种方法
int LogException(MethodBase methodBase, Exception exception)
int LogMessage(MethodBase methodBase, string message, string data)
当我尝试将服务添加到新项目时,未创建 .cs 文件。我运行 svcutil,并将 .cs 和配置设置复制到项目中,并尝试使用生成的客户端调用服务。我收到以下两个错误
尝试时发生错误 序列化参数 http://tempuri.org/:methodBase。这 InnerException 消息为“类型” 'System.Reflection.RuntimeMethodInfo' 带有数据合约名称 'RuntimeMethodInfo:http://schemas.datacontract.org/2004/07/System.Reflection' 预计不会。考虑使用 DataContractResolver 或添加任何类型 静态未知的列表 已知类型 - 例如,通过使用 KnownTypeAttribute 属性或通过 将它们添加到已知类型列表中 传递给 DataContractSerializer。'。 更多内容请参见InnerException 详细信息。
内心异常
类型 'System.Reflection.RuntimeMethodInfo' 带有数据合约名称 'RuntimeMethodInfo:http://schemas.datacontract.org/2004/07/System.Reflection' 预计不会。考虑使用 DataContractResolver 或添加任何类型 静态未知的列表 已知类型 - 例如,通过使用 KnownTypeAttribute 属性或通过 将它们添加到已知类型列表中 传递给 DataContractSerializer。
我需要做什么才能使这项工作成功?
I created a WCF service for logging exceptions (I realize if the network is down, things won't be logged...there are fallbacks in place)
Anyhow, it has two methods
int LogException(MethodBase methodBase, Exception exception)
int LogMessage(MethodBase methodBase, string message, string data)
When I try and add the service to a new project, the .cs file is not created. I ran svcutil, and copied the .cs and config settings into the project, and tried calling the service with the generated client. I got the following two error
There was an error while trying to
serialize parameter
http://tempuri.org/:methodBase. The
InnerException message was 'Type
'System.Reflection.RuntimeMethodInfo'
with data contract name
'RuntimeMethodInfo:http://schemas.datacontract.org/2004/07/System.Reflection'
is not expected. Consider using a
DataContractResolver or 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.'.
Please see InnerException for more
details.
With inner exception
Type
'System.Reflection.RuntimeMethodInfo'
with data contract name
'RuntimeMethodInfo:http://schemas.datacontract.org/2004/07/System.Reflection'
is not expected. Consider using a
DataContractResolver or 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.
What do I need to do to make this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过 WCF 进行通信时,WCF 需要准确地知道将跨边界发送的内容。因此,接受“异常”是可以的,但几乎总是,您将传递异常的子类型,因此您需要告诉合约哪些类型的异常将跨越边界传递。 MethodBase 也是如此。您可能想告诉它,有时您实际上会传递 MethodInfo。
因为这些不是您的类型,所以您可能无法使用 KnownType 属性(该属性通常放置在基类或接口上)。在这种情况下,您需要使用数据契约解析器。它告诉序列化/反序列化引擎如何查找子类型。
http://msdn.microsoft.com/en-us/library/ee358759.aspx
虽然您可以使用ServiceKnownType 属性。您的契约应如下所示:
这告诉 WCF MethodBase 可以使用降序类型 MethodInfo。
When communicating via WCF, WCF needs to know precisely what will be sent across the boundary. So taking an "Exception" is fine and all, but almost always, you'll be passing subtypes of exception, so you need to tell the contract which types of Exceptions will be passed across the boundary. The same thing goes for MethodBase. You probably want to tell it that you'll actually be passing a MethodInfo some of the time.
Because these aren't your types, you probably can't use the KnownType attribute (the attribute usually gets placed on the base class or interface). In this case, you need to use a Data Contract Resolver. It tells the serialization / deserialization engine how to find the sub types.
http://msdn.microsoft.com/en-us/library/ee358759.aspx
Although you can use the ServiceKnownType attribute. Your contract should look something like this:
This tells WCF that MethodBase could be using the descending Type MethodInfo.