在WCF服务中序列化MethodBase和异常类型 [英] Serializing MethodBase and Exception types in a WCF Service

查看:52
本文介绍了在WCF服务中序列化MethodBase和异常类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个WCF服务来记录异常(我意识到如果网络中断,则不会记录任何东西...存在后备状态)

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)

当我尝试将服务添加到新项目时,未创建.cs文件.我运行svcutil,并将.cs和config设置复制到项目中,并尝试使用生成的客户端调用服务.我遇到以下两个错误

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

尝试进行操作时出现错误序列化参数 http://tempuri.org/:methodBase .这InnerException消息为类型'System.Reflection.RuntimeMethodInfo'带有数据合同名称'RuntimeMethodInfo:http://schemas.datacontract.org/2004/07/System.Reflection'不是预期的.考虑使用DataContractResolver或添加任何类型列表的静态未知已知类型-例如,通过使用KnownTypeAttribute属性或将它们添加到已知类型列表中传递给DataContractSerializer.".请参阅InnerException了解更多信息详细信息.

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.

有内部例外情况

类型'System.Reflection.RuntimeMethodInfo'带有数据合同名称'RuntimeMethodInfo:http://schemas.datacontract.org/2004/07/System.Reflection'不是预期的.考虑使用DataContractResolver或添加任何类型列表的静态未知已知类型-例如,通过使用KnownTypeAttribute属性或将它们添加到已知类型列表中传递给DataContractSerializer.

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?

推荐答案

通过WCF进行通信时,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.

因为这些不是您的类型,所以您可能无法使用KnownType属性(该属性通常放置在基类或接口上).在这种情况下,您需要使用数据合同解析器.它告诉序列化/反序列化引擎如何找到子类型.

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

尽管可以使用ServiceKnownType属性.您的合同应如下所示:

Although you can use the ServiceKnownType attribute. Your contract should look something like this:

[DataContract]
public interface ILoggingStuff // choose a better name than this
{
    [OperationContract]
    [ServiceKnownType(typeof(MethodInfo))]
    int LogException(MethodBase methodBase, Exception exception);
    [OperationContract]
    [ServiceKnownType(typeof(MethodInfo))]
    int LogMessage(MethodBase methodBase, string message, string data);
}

这告诉WCF MethodBase可能正在使用降序类型MethodInfo.

This tells WCF that MethodBase could be using the descending Type MethodInfo.

这篇关于在WCF服务中序列化MethodBase和异常类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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