开发者

DataContractSerializer C# Can serialize, cannot deserialize, why?

开发者 https://www.devze.com 2022-12-09 09:02 出处:网络
I have application created using WCF and C#, it\'s architecture requires to add KnownTypes through App.config. I have services going like this:

I have application created using WCF and C#, it's architecture requires to add KnownTypes through App.config. I have services going like this: Client -> CentralServer -> DataServer (where -> is WCF connection) Now, I've added KnownTypes to both CentralServer App.config and DataServer App.config this way:

  <add
 type="Odra.Server.CentralServer.SBQL.AbstractSyntax.Declarations.Declaration,
 Odra.Server.CentralServer.SBQL"> 
 <knownType
 type="Odra.Server.CentralServer.SBQL.AbstractSyntax.Declarations.MethodDeclaration,
 Odra.Server.CentralServer.SBQL" /> 
 </add>

My problem is that, calling method whitch takes argument of type MethodDeclaration on DataServer from CentralServer throws an exception that service cannot deserialize this parameter, although CentralServer CAN serialize it (I know, because when I remove KnownType i get exception that service cannot SERIALIZE). In addition, I have plenty of such methods defined same way, but accepting different type as an argument, and they work perfectly. Do you have any idea why this is (not) working like this?

Exception:

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:val. 
The InnerException message was 'Element 'http://tempuri.org/:val' contains data of the 'http://schemas.datacontract.org/2004/07/Odra.Server.CentralServer.SBQL.AbstractSyntax.Declarations:MethodDeclaration' data contract. The deserializer has no knowledge of any type that maps to this contract. 
Add the type corresponding to 'MethodDeclaration' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passe开发者_如何学编程d to DataContractSerializer.'.  Please see InnerException for more details.


Possible mismatch of the types between the server and the client?

Maybe if you post the exception details we can be of more help.


What I would try:

  1. Check if MethodDeclaration has DataContractAttribute and properties DataMemberAttribute
  2. Add [ServiceKnownType(typeof(MethodDeclaration))] to service class

Pseudo code:

[ServiceContract]
public interface IService
{
    [ServiceKnownType(typeof(MethodDeclaration))]
    void ProcessMethodDeclaration(Declaration val);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消