简体   繁体   English

WCF服务对象序列化

[英]WCF Service object serialization

I have one abstract class named contact and another class called client that inherits from contact. 我有一个名为contact的抽象类,另一个名为client的类继承自contact。 I'm dealing with a WCF Service with a method that takes a parameter of type contact. 我正在使用带有类型联系参数的方法处理WCF服务。 however what I have is an instance of client that I want to pass. 但是我拥有的是我想传递的客户端实例。 Im facing this Error: 我面临这个错误:

Type 'xxx.Client' with data contract name 'Client:http://schemas.datacontract.org/2004/07/xxx' is not expected. 输入数据合约名称为“客户:http://schemas.datacontract.org/2004/07/xxx”的“xxx.Client”不是预期的。 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. 将任何静态未知的类型添加到已知类型列表中 - 例如,通过使用KnownTypeAttribute属性或将它们添加到传递给DataContractSerializer的已知类型列表中。

you ned to let DataContractSerializer know that a Client is a type of Contact . 您需要让DataContractSerializer知道Client是一种Contact

There are several ways to do this, but they all revolve around using the KnownType attribute or the ServiceKnownType attributes. 有几种方法可以做到这一点,但它们都围绕着使用KnownType属性或ServiceKnownType属性。

The KnownType can be placed on the Client class to tell DataContractSerializer that it is a KnownType of Contact . 可以将KnownType放在Client类上,告诉DataContractSerializer它是Contact的KnownType。

[DataContract]
[KnownType(typeof(Client))]
public class Contact{}

The KnownType can also be placed on a class to indicate that when serialising this class you may also encounter this other class. KnownType也可以放在一个类上,表示在序列化这个类时你也可能会遇到另一个类。

You may want to do this if you have a DataContract class that has a property which is a Contact which may actually contain a Client : 如果您的DataContract类具有可能实际包含ClientContact的属性,则可能需要执行此操作:

[DataContract]
[KnownType(typeof(Client))]
public class Meeting
{
    Contact MeetingContact{get;}
}

In this case you could get away without specifying the KnownType on the Client. 在这种情况下,您可以在不指定客户端上的KnownType的情况下离开。 You may also want to do this if you have a property which returns a collection and you want to specify the types which can be in the collection. 如果您有一个返回集合的属性,并且您想要指定可以在集合中的类型,则可能还需要执行此操作。

You may, instead of specifying the actual type of the KnownType, specify the name of a static method which will return the known types instead: 您可以指定静态方法的名称,而不是指定KnownType的实际类型,而该方法将返回已知类型:

[DataContract]
[KnownType("GetKnownTypes")]
public class Meeting
{
    Contact MeetingContact{get;}

    private static Type[] GetKnownType()
    {
    return new Type[]{typeof(Client)};
    }
}

You can also specify the known type through the configuration file . 您还可以通过配置文件指定已知类型。

ServiceKnownTypes work in a similar way, but are specified on the Service itself: ServiceKnownTypes以类似的方式工作,但在服务本身上指定:

[ServiceKnownType(typeof(Client))]
[ServiceContract()]
public interface IMyServiceContract
{

    [OperationContract]
    Contact GetContact();
}

This set up will let the DataContactSerializer know that any of the methods may return a type of type Client . 此设置将让DataContactSerializer知道任何方法都可以返回类型为Client的类型。 In a similar way to the known types you can also use a static method to provide the service known types. 以与已知类型类似的方式,您还可以使用静态方法来提供已知类型的服务。

WCF does not directly works on abstract classes. WCF不直接用于抽象类。 You shall use KnownType attributes on the datacontract or service class. 您应在datacontract或服务类上使用KnownType属性。 below are examples; 以下是例子;

[DataContract]
[KnownType(typeof(Client))]
public class Contact
{
   ...
}

[ServiceContract]
[ServiceKnownType(typeof(Client))]
public interface IMyService
{
    contact getcontact(Guid id);
}

使用[KnownType][ServiceKnownType]属性确保关系。

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

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