WCF尝试序列不与[数据成员]

问题描述:

鉴于以下原因DataContract成员:WCF尝试序列不与[数据成员]

[DataContract] 
public class GetGameSessionHistory : ValueBaseCasinoIT 
{ 
    [DataMember] 
    public GameSession[] GameSessions => GameSessionsValue.ToArray<GameSession>(); 

    public IEnumerable<GameSession> GameSessionsValue { get; set; } 
} 

我仍然得到错误信息GetGameSessionHistory是不可序列由于IEnumerable接口。

System.NotSupportedException: Cannot serialize member GetGameSessionHistory.GameSessionsValue of type System.Collections.Generic.IEnumerable`1[[GameSession, Common, Version=3.45.0.11, Culture=neutral, PublicKeyToken=null]] because it is an interface. 

我也尝试过删除整个[DataContract]属性,特别是忽略受影响的成员[IgnoreDataMember]的方法。但不幸的是,这导致了相同的结果。

更新1

在给定的代码lambda表达式只是一个步骤我打算这样做。使用以下DataContract时问题仍然相同:

[DataContract] 
public class GetGameSessionHistory : ValueBaseCasinoIT 
{ 
    [DataMember] 
    public GameSession[] GameSessions { get; set; } 

    public IEnumerable<GameSession> GameSessionsValue { get; set; } 
} 

当删除IEnumerable消息消失。所以这个问题不能来自GameSession类型。

当前我使用的是XmlSerializer

数据在下列服务使用:

IAdapterService:

[ServiceContract, XmlSerializerFormat(Style = OperationFormatStyle.Rpc, Use = OperationFormatUse.Encoded), DispatchByBodyBehavior] 
public interface IAdapterService 
{ 
    [OperationContract(Name = "GetGameSessionHistoryRequest", Action = ""), DispatchBodyElement("GetGameSessionHistoryRequest", "...")] 
    GetGameSessionHistory GetGameSessionHistory(AuthenticationToken authenticationToken, string playerId, DateTime fromDate, DateTime toDate); 
} 

AdapterService:

[SoapDocumentService(SoapBindingUse.Literal, RoutingStyle = SoapServiceRoutingStyle.RequestElement)] 
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)] 
public class AdapterService : IAdapterService 
{ 
    public GetGameSessionHistory GetGameSessionHistory(AuthenticationToken authenticationToken, string playerId, DateTime fromDate, DateTime toDate) 
    { ... } 
} 

Web.config文件:

<basicHttpBinding> 
    <binding name="SoapBinding"> 
     <security mode="None"/> 
     <readerQuotas maxDepth="4000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="32768"/> 
    </binding> 
</basicHttpBinding> 

<service name="...svc.AdapterService" behaviorConfiguration="svcBehavior" > 
    <endpoint binding="basicHttpBinding" bindingConfiguration="SoapBinding" contract="...IAdapterService" name="AdapterSoapBinding" behaviorConfiguration="withMessageInspector"/> 
</service> 

还有什么CA使用这个问题?

+0

嗯,抛开其他问题:它不能*反序列化*'GameSessions' –

+0

相关:http://stackoverflow.com/questions/2068897/cannot-serialize-parameter-of-type-system- linq-enumerable-when-using-wcf –

+2

你可以确切地**确认**哪个序列化器WCF被配置为使用?有'DataContractSerializer'和'NetDataContractSerializer',可以通过配置来选择。 (编辑:虽然我在我的答案中的代码尝试了'NetDataContractSerializer',它仍然工作正常) –

发现问题: 我正在使用XmlSerializer(IAdapterService归因于[XmlSerializerFormat]),这会导致所有公共属性被序列化。

DataContractSerializer(XmlObjectSerializer)会考虑序列化过程的[DataMember]属性,而XmlSerializer只是忽略它们。

问题依然存在,为什么[XmlIgnore]属性无法正常工作。

如果我尝试你的代码,序列化工作正常;反序列化失败:

System.Runtime.Serialization.SerializationException:类型为'GameSession []'的只读集合返回空值。输入流包含收集项目,如果实例为空,则不能添加。考虑在getter中初始化集合。

这是我所期望的,因为它没有办法给GameSessions赋值。如果我添加set ...一切正常。这是完整的工作代码:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Runtime.Serialization; 

class Program 
{ 
    static void Main() 
    { 
     var ser = new DataContractSerializer(typeof(GetGameSessionHistory)); 
     using (var ms = new MemoryStream()) 
     { 
      var orig = new GetGameSessionHistory { GameSessionsValue = 
       new List<GameSession>() { new GameSession { } } }; 
      ser.WriteObject(ms, orig); 
      ms.Position = 0; 
      var clone = (GetGameSessionHistory)ser.ReadObject(ms); 
      Console.WriteLine(clone?.GameSessionsValue.Count()); // prints 1 
     } 
    } 

} 
[DataContract] 
public class GetGameSessionHistory : ValueBaseCasinoIT 
{ 
    [DataMember] 
    public GameSession[] GameSessions 
    { 
     get { return GameSessionsValue?.ToArray(); } 
     set { GameSessionsValue = value; } 
    } 
    //[DataMember] // original version; fails with The get-only collection of type 
        // 'GameSession[]' returned a null value. 
    //public GameSession[] GameSessions => GameSessionsValue?.ToArray<GameSession>(); 

    public IEnumerable<GameSession> GameSessionsValue { get; set; } 
} 

[DataContract] 
public class ValueBaseCasinoIT 
{ 
} 

[DataContract] 
public class GameSession 
{ 
} 

我想你可能需要添加更多的信息来帮助我们识别问题,因为......它基本上工作。

+0

感谢您的提示,我会在事后考虑。请介意我的新更新。希望现在有足够的信息来解决潜在的问题。 – chrsi