2016-07-04 8 views
1

에 기인하지 않는 멤버를 직렬화하려고합니다.WCF는 다음 DataContract 감안할 [DataMember를]

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

주어진 코드의 람다 표현식 내가 할 계획 단지 단계이었다. 문제는 다음 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> 

다른 무엇을 할 수 캘리포니아 이 문제를 사용합니까?

+0

음, 다른 것들은 제외하고 *'GameSessions'를 역 직렬화 할 수 없습니다 –

+0

관련 항목 : http://stackoverflow.com/questions/2068897/cannot-serialize-parameter-of-type-system- linq-enumerable-when-using-wcf –

+2

** 어떤 serializer WCF가 사용되도록 구성되었는지 ** 정확하게 확인할 수 있습니까? 구성에 의해 선택 될 수있는'DataContractSerializer'와'NetDataContractSerializer'가 있습니다. (편집 : 내 대답에있는 코드에서'NetDataContractSerializer'를 시도했지만 여전히 정상적으로 작동했습니다.) –

답변

0

문제가 발견되었습니다. 모든 공용 속성이 serialize되도록하는 XmlSerializer (IAdapterService는 [XmlSerializerFormat]와 함께 사용됩니다)를 사용하고있었습니다.

DataContractSerializer (XmlObjectSerializer)는 직렬화 프로세스의 [DataMember] 특성을 고려하지만 XmlSerializer는 해당 특성을 무시합니다.

[XmlIgnore] 특성이 작동하지 않는 이유는 무엇입니까?

1

코드를 사용해 본다면 직렬화가 정상적으로 작동합니다. deserialization 함께 실패합니다 :

System.Runtime.Serialization.SerializationException : 'GameSession []'형식의 가져 오기 전용 컬렉션은 null 값을 반환했습니다. 입력 스트림에는 인스턴스가 null 인 경우 추가 할 수없는 컬렉션 항목이 들어 있습니다. 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