简体   繁体   English

从wcf服务中的类继承

[英]Inherit from a class in a wcf service

I want to inherit from a class which is located in a WCF Service. 我想从WCF服务中的类继承。 The Inheritance works fine (I see the properties of the base class in my child class), my problem occurs when I try to call a method from the service and pass the childtype as a parameter and not the basetype. 继承工作正常(我在子类中看到了基类的属性),当我尝试从服务中调用方法并将子类型作为参数而不是基类型传递时,会出现我的问题。

Base class in WCF Service ( Pet ): WCF服务( Pet )中的基类:

[ServiceContract]
public interface IService
{
    [OperationContract]
    void BringPet(Pet pet);

    [OperationContract]
    void TakePet(Pet pet);

    [OperationContract]
    List<Pet> GetAllPets();
}

[DataContract]
public class Pet
{
    private string _name;
    private string _color;

    [DataMember]
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    [DataMember]
    public string Color
    {
        get { return _color; }
        set { _color = value; }
    }
}

Class on the client ( Dog inherits from Pet ): 客户端上的类( Dog继承自Pet ):

[DataContract()]
class Dog : PetService.Pet
{
    [DataMember()]
    public bool HasPedigree { get; set; }
    [DataMember()]
    public string Race { get; set; }
}

When I try calling something like this: 当我尝试调用类似这样的内容时:

        Dog dog = new Dog()
        {
            Color = "Black",
            Name = "Pluto",
            HasPedigree = true,
            Race = "Travolta"
        };

        _client.BringPet(dog);

I get a CommunicationException which says that the type Dog is not expected by the method BringPet(Pet pet) . 我收到一个CommunicationException ,它说BringPet(Pet pet)方法不希望使用Dog类型。 I would solve this problem by setting the KnownType attributes on the service side, but because my service must not know the type Dog I can't set the KnownType or ServiceKnownType attributes. 我可以通过在服务端设置KnownType属性来解决此问题,但是由于我的服务必须不知道Dog类型,因此无法设置KnownTypeServiceKnownType属性。

Can someone help me out? 有人可以帮我吗?

If you want to have inherited classes that get returned instead of the ones defined in your service contract, you need to make this fact known to WCF by means of the ServiceKnownType attribute: 如果要继承继承的类而不是返回服务合同中定义的类,则需要通过ServiceKnownType属性将此事实告知WCF:

[ServiceContract]
[ServiceKnownType(typeof(Dog))]
public interface IService
{
    [OperationContract]
    void BringPet(Pet pet);

    [OperationContract]
    void TakePet(Pet pet);

    [OperationContract]
    List<Pet> GetAllPets();
}

This basically tells the WCF service to also allow classes of type Dog to be used in your service. 这基本上告诉WCF服务也允许在您的服务中使用Dog类型的类。

Or you can decorate your data contract with the KnownType attribute: 或者,您可以使用KnownType属性装饰数据协定:

[DataContract]
[KnownType(typeof(Dog))]
public class Pet
{
.....
}

to associate your data contract type with additional types that could be used in its place. 将您的数据合同类型与可以在其位置使用的其他类型相关联。

One way or the other: you have to make your extra type known to the WCF runtime - WCF cannot figure out that fact by just checking .NET type inheritance. 一种或另一种方式:必须使WCF运行时知道您的额外类型-WCF不能仅通过检查.NET类型继承来弄清这一事实。

I think this is very similar to this question: Service - client interface, architecture advice 我认为这与以下问题非常相似: 服务-客户端界面,体系结构建议

As you will see, there is no really easy way to do this without making your service operation more generic. 正如您将看到的,没有真正简单的方法可以使服务操作更加通用。

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

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