WCF:是一个通用的接口系列化可能吗? [英] WCF: Is serialization of a generic interfaces possible?

查看:127
本文介绍了WCF:是一个通用的接口系列化可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个包含这需要一个通用接口的方法的服务合同,而通用接口本身是给定一个接口的参数。我饰ServiceKnownType服务接口,我都饰有定期KnownType服务实现,我也饰有定期KnownType的datacontract实现:

I'm trying to implement a service contract that contains a method which takes a generic interface, and that generic interface itself is given an interface parameter. I've decorated the service interface with ServiceKnownType, I have decorated the service implementation with regular KnownType, and I have decorated the datacontract implementation with regular KnownType:

[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(ICallbacks))]
[ServiceKnownType(typeof(Batch<object>))]
[ServiceKnownType(typeof(Command))]
public interface IActions
{
    [OperationContract]
    IResponse TakeAction(IBatch<ICommand> commands);
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Reentrant)]
[KnownType(typeof(Batch<object>))]
[KnownType(typeof(Command))]
internal class Actions : IActions
{
}

[DataContract]
[KnownType(typeof(Command))]
public class Batch<T> : IBatch<T>
{
}

有关的记录,我有批量那里,因为它似乎你只能表达对泛型类型一次knowntype - 这似乎发出BatchOfanyType,但我不知道如何处理这种

For the record, I have Batch there because it seems that you can only express a knowntype for a generic type once--it appears to emit BatchOfanyType, but I'm not sure how to handle this.

例外我m到处是添加不是静态已知的已知类型的列表中的任何类型 - 例如,通过使用KnownTypeAttribute属性或通过将其添加到已知类型传递给DataContractSerializer列表。

The exception I'm getting is "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."

有什么明显的,我做错了什么?是接口通用接口,只是不支持?为了记录我在C#2.0,并为这个项目.NET 3.0。

Is there anything obvious I'm doing wrong? Are generic interfaces of interfaces just not supported? For the record I'm on C# 2.0 and .NET 3.0 for this project.

推荐答案

您可以使用服务合同定义的接口如果你真的想,只要你包括已知类型,你正在做的(有轻微的调整,见下文)。

You can use interfaces in service contract definitions if you really want to, as long as you're including the known types as you are doing (with a slight adjustment, see below).

显然,使用一个接口作为泛型类型参数把它夺桥遗恨为C#3.0。我改变了已知类型的属性。

Apparently, using an interface as the generic type parameter is taking it a bridge too far for C# 3.0. I changed the known type attribute to

[ServiceKnownType(typeof(Batch<Command>))]
public interface IActions
{
}

这使得它的工作,到一个点。序列化和反序列化本身会工作,但你面对此异常:

Which makes it work, to a point. Serialization and deserialization itself will work, but then you're faced with this exception:

无法投类型的对象'Batch`1 [命令]'
键入IBatch`1 [ICommand的]。

Unable to cast object of type 'Batch`1[Command]' to type 'IBatch`1[ICommand]'.

对于投地工作,你需要的语言泛型类型的协方差的支持,这是在C#4.0中引入的一些东西。对于它在工作的C#4.0,虽然,你需要添加一个差异调节剂:

For that cast to work, you need language support for generic type covariance, something that's introduced in C# 4.0. For it to work in C# 4.0 though, you'd need to add a variance modifier:

public interface IBatch<out T>
{
}



然后,它完美的作品......不幸的是你。不使用C#4.0

Then it works perfectly... unfortunately you're not using C# 4.0.

有关使用您的服务合同接口最后一件事:如果你生成从他们的服务引用,它会输入所有的接口参数对象,becase的原始接口类型不是元数据的一部分。您可以通过集引用分享合同,或手动重构生成的代理来修复它,但所有的一切,使用接口与WCF可能是更多的麻烦比它的价值。

One last thing about using interfaces in your service contract: if you're generating a service reference from them, it will type all the interface parameters as object, becase the original interface type isn't part of the metadata. You can share contracts through an assembly reference, or manually refactor the generated proxy to fix it, but all in all, using interfaces with WCF is probably more trouble than it's worth.

这篇关于WCF:是一个通用的接口系列化可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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