简体   繁体   English

通过WCF服务传递指定的通用类型

[英]Passing Specified Generic Type through WCF Service

I have the following classes: 我有以下课程:

[DataContract]
[KnownType(typeof(SpecifiedItemCollection))]
public class ItemCollection<T> : IEnumerable where T : BaseItem
{
    private Dictionary<int, T> items = null;

    //Some NON-Generic Methods and Properties

    //Some methods like this:
    public T DoBla(int _1, bool _2) { ... }
}

[DataContract]
public class SpecifiedItemCollection : ItemCollection<SpecifiedItem>
{
    //...
}

[DataContract]
[KnownType(typeof(SpecifiedItem))]
public class BaseItem { ... }

[DataContract]
public class SpecifiedItem : BaseItem { ... }

How do I deliver SpecifiedItemCollection through a WCF Service? 如何通过WCF服务传递SpecifiedItemCollection?

My Interface looks like this, but unfortunately it won't work 我的界面看起来像这样,但是不幸的是它无法正常工作

[ServiceContract]
public interface IService
{
    [OperationContract]
    public SpecifiedItemCollection GetCol(int _1, bool _2);
}

And for additional information: 以及其他信息:

Yes, I saw that you can't pass Generic's through WCF (for example ItemCollection directly), but I found several sources that say you CAN pass them IF you specify the Generic itself. 是的,我看到您无法通过WCF传递泛型(例如直接使用ItemCollection),但是我发现有几个消息来源说,如果您指定了泛型本身,则可以通过它们。

So, what am I doing wrong? 那么,我在做什么错呢? :) :)

The problem is, it simply closes the connection. 问题是,它只是关闭了连接。 I am able to reference the service in my project and it generates the needed Classes/Files accordingly. 我能够在我的项目中引用该服务,它会相应地生成所需的类/文件。 I can instantiate a MyServiceName Client , but as soon as I'm calling a method from my service which will return an SpecifiedItemCollection, it closes the connection. 我可以实例化MyServiceName Client ,但是一旦我从服务中调用一个将返回SpecifiedItemCollection的方法,它将关闭连接。

Seems like I found a temporary solution, although I do not like it very much. 似乎我找到了一个临时解决方案,尽管我不太喜欢它。

In my example above, change the SpecifiedItemCollection-Class as follows: 在上面的示例中,如下更改SpecifiedItemCollection-Class:

[DataContract]
public class SpecifiedItemCollection
{
    public ItemCollection<SpecifiedItem> Base;

    public SpecifiedItemCollection()
    {
        Base = new ItemCollection<SpecifiedItem>();
    }

    //...
}

With this I am able to call the functions of ItemCollection and at the same Type have my SpecifiedItemCollection-Class with the extra Methods and Properties. 这样我就可以调用ItemCollection的函数,并且在相同的Type上具有带有额外的Methods和Properties的SpecifiedItemCollection-Class。

If anyone has another solution, I'd be more than happy to see it. 如果有人有其他解决方案,我将很高兴看到它。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM