Ho il seguente:WCF: Interfacce, Generics e ServiceKnownType
[ServiceContract]
[ServiceKnownType(typeof(ActionParameters))]
[ServiceKnownType(typeof(SportProgram))]
[ServiceKnownType(typeof(ActionResult<SportProgram>))]
public interface ISportProgramBl
{
[OperationContract]
IActionResult<ISportProgram> Get(IActionParameters parameters);
}
Quando eseguo il metodo Get ottengo il seguente errore:
There was an error while trying to serialize parameter http://tempuri.org/:GetResult . The InnerException message was 'Type 'PPS.Core.DomainModel.Support.Action.ActionResult`1[ [PPS.Core.DomainModel.SportProgram.ISportProgram, PPS.Core.DomainModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' with data contract name 'ActionResultOfanyType: http://schemas.datacontract.org/2004/07/PPS.Core.DomainModel.Support.Action ' is not expected. 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.'. Please see InnerException for more details.
Da questo errore posso vedere che si può risolvere ActionResult ma non può risolvere ISportProgram anche se ho ServiceKnownType (typeof (ActionResult < SportProgram>)) sulla mia interfaccia di servizio ...
Nota questo è lo stub di riferimento che viene generato assomiglia a questo, così posso vedere che i tipi noti vengono portati in tutto correttamente:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="SportProgramStb.ISportProgramBl")]
public interface ISportProgramBl {
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ISportProgramBl/Get", ReplyAction="http://tempuri.org/ISportProgramBl/GetResponse")]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(PPS.Core.DomainModel.SportProgram.SportProgram))]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(PPS.Core.DomainModel.Support.Action.ActionParameters))]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(PPS.Core.DomainModel.Support.Action.ActionResult<PPS.Core.DomainModel.SportProgram.SportProgram>))]
object Get(object parameters);
}
perché è questo che non va ???? Si noti che sta ricevendo correttamente il servizio WCF ... ma lancia l'eccezione quando viene restituito il risultato.
Infine ActionResult assomiglia a questo:
public interface IActionResult<T>
{
T Result { get; set; }
}
Acclamazioni Anthony
Il tipo 'ActionResult' che 'Serializer' si aspetta è diverso dal tipo' ActionResult 'fornito in' ServiceKnownType'! –
Lightman