2012-04-29 15 views
5

Esto es en relación con una pregunta anterior que hice:Marcado de una clase de biblioteca referenciada en el servicio WCF

Tengo un DLL que define una clase Transaction. Es referenciado por una biblioteca de servicios WCF, así como por una aplicación cliente. Recibo errores que indican que la biblioteca del servicio no se puede alojar porque no puede serializar la clase DLL.

Aquí está el código de servicio:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.Text; 
using ServerLibrary.MarketService; 
using SharedLibrary; // This is the DLL in question 

namespace ServerLibrary 
{ 
    [ServiceContract] 
    public interface IService 
    { 
     [OperationContract] 
     string GetData(int value); 

     [OperationContract] 
     CompositeType GetDataUsingDataContract(CompositeType composite); 

     [OperationContract] 
     bool ProcessTransaction(SharedLibrary.Transaction transaction); 
    } 

    [DataContract] 
    public class CompositeType 
    { 
     bool boolValue = true; 
     string stringValue = "Hello "; 

     [DataMember] 
     public bool BoolValue 
     { 
      get { return boolValue; } 
      set { boolValue = value; } 
     } 

     [DataMember] 
     public string StringValue 
     { 
      get { return stringValue; } 
      set { stringValue = value; } 
     } 
    } 
} 

¿Tengo que marcar la clase de transacción aquí con cabeceras [atributos]?

[ACTUALIZACIÓN]

Aquí está el mensaje de error que consigo cuando intento para acoger este servicio:

System.Runtime.Serialization.InvalidDataContractException: Type 'SharedLibrary.Transaction' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types. at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.ThrowInvalidDataContractException(String message, Type type) at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id, RuntimeTypeHandle typeHandle, Type type) at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type) at System.Runtime.Serialization.XsdDataContractExporter.GetSchemaTypeName(Type type) at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.ValidateDataContractType(Type type) at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.CreatePartInfo(MessagePartDescription part, OperationFormatStyle style, DataContractSerializerOperationBehavior serializerFactory) at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.CreateMessageInfo(DataContractFormatAttribute dataContractFormatAttribute, MessageDescription messageDescription, DataContractSerializerOperationBehavior serializerFactory) at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter..ctor(OperationDescription description, DataContractFormatAttribute dataContractFormatAttribute, DataContractSerializerOperationBehavior serializerFactory) at System.ServiceModel.Description.DataContractSerializerOperationBehavior.GetFormatter(OperationDescription operation, Boolean& formatRequest, Boolean& formatReply, Boolean isProxy) at System.ServiceModel.Description.DataContractSerializerOperationBehavior.System.ServiceModel.Description.IOperationBehavior.ApplyDispatchBehavior(OperationDescription description, DispatchOperation dispatch) at System.ServiceModel.Description.DispatcherBuilder.BindOperations(ContractDescription contract, ClientRuntime proxy, DispatchRuntime dispatch) at System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost) at System.ServiceModel.ServiceHostBase.InitializeRuntime() at System.ServiceModel.ServiceHostBase.OnBeginOpen() at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) at System.ServiceModel.Channels.CommunicationObject.Open() at Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfo info)

a lo solicitado aquí es la DLL que contiene la transacción:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace SharedLibrary 
{ 
    // Transaction class to encapsulate products and checkout data 
    public class Transaction 
    { 
      public int checkoutID; 
      public DateTime time; 
      public List<object> products; // Using object to avoid MarketService reference, remember to cast back! 
      public double totalPrice; 
      public bool complete; 

      public Transaction(int ID) 
      { 
       checkoutID = ID; 
      } 

      public void Start() 
      { 
       products = new List<object>(); 
       complete = false; 
      } 

      public void Complete() 
      { 
       time = DateTime.Now; 
       complete = true; 
      } 
     } 
} 

Gracias.

+0

¿Qué versión de C#/WCF está utilizando? –

+0

Estoy usando .NET 4 si eso ayuda. – Lee

+0

¿Puedes agregar la definición de 'SharedLibrary.Transaction'? –

Respuesta

1

Do I have to mark the Transaction class here with [attribute] headers?

No, no debería tener que hacerlo, pero se lo recomienda. Ver Using Data Contracts.


El problema es que usted está pasando objetos derivados en un List<object>.

usted tiene que decir lo que el servicio de objetos de texto para manejar con un atributo ServiceKnownType:

[OperationContract] 
[ServiceKnownType(typeof(MarketService.XXX))] 
bool ProcessTransaction(SharedLibrary.Transaction transaction); 
+0

Gracias Nicholas, pero sigo recibiendo el mismo error. Apliqué MarketService.Product como typeof() que es lo que será la lista de objetos. – Lee

+0

Lo siguiente que debe intentar es enviar un objeto 'Transaction' sin elementos en la lista' products'. Además, ¿recibe algún otro error? –

+0

No puedo llegar al punto de pasar objetos. Estos errores ocurren cuando intento construir la biblioteca de servicios WCF y Visual Studio los aloja automáticamente. No hay nuevos errores además de la falla al serializar la clase SharedLibrary.Transaction. – Lee

1

Es posible que desee definir la clase de transacción de la siguiente manera

[DataContract] 
[KnownType(typeof(MarketService.XXX))] 
public class Transaction 
{ 
} 

espero que esto ayude.

+0

Agregué esto al espacio de nombres de IService pero los errores permanecen. Gracias por intentar ayudar. – Lee

+0

Es posible que desee hacer esto en el nivel de clase Transaction. Intentalo. – RajN

Cuestiones relacionadas