2012-04-29 24 views
5

Điều này liên quan đến câu hỏi trước tôi đã hỏi:Đánh dấu một lớp thư viện tham chiếu trong dịch vụ WCF

Tôi có một DLL xác định một lớp Giao dịch. Nó được tham chiếu bởi một thư viện dịch vụ WCF cũng như một ứng dụng khách. Tôi nhận được lỗi nói rằng thư viện dịch vụ không thể được lưu trữ bởi vì nó không thể serialise lớp DLL.

Dưới đây là các mã dịch vụ:

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; } 
     } 
    } 
} 

Tôi có phải đánh dấu các lớp giao dịch ở đây với [Thuộc tính] tiêu đề?

[UPDATE]

Dưới đây là các thông báo lỗi tôi nhận được khi tôi cố gắng để tổ chức dịch vụ này:

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)

Theo yêu cầu ở đây là các DLL chứa giao dịch:

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; 
      } 
     } 
} 

Cảm ơn.

+0

Bạn đang sử dụng phiên bản C#/WCF nào? –

+0

Tôi đang sử dụng .NET 4 nếu có ích. – Lee

+0

Bạn có thể thêm định nghĩa 'SharedLibrary.Transaction' không? –

Trả lời

1

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

Không, bạn không cần phải, nhưng được khuyến nghị. Xem Using Data Contracts.


Vấn đề là bạn đang truyền các đối tượng có nguồn gốc trong một List<object>.

Bạn phải nói với các dịch vụ mà đối tượng loại để xử lý với một thuộc tính ServiceKnownType:

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

Cảm ơn Nicholas nhưng tôi vẫn nhận được lỗi tương tự. Tôi đã áp dụng MarketService.Product là typeof() là danh sách các đối tượng sẽ là gì. – Lee

+0

Điều tiếp theo cần thử là gửi đối tượng 'Giao dịch' thông qua không có mục nào trong danh sách' sản phẩm'. Ngoài ra, bạn có nhận được bất kỳ lỗi nào khác không? –

+0

Tôi không thể đi đến điểm thực sự truyền các vật thể. Những lỗi này xảy ra khi tôi cố gắng xây dựng thư viện dịch vụ WCF và studio trực quan tự động lưu trữ nó. Không có lỗi mới nào khác ngoài việc không nối tiếp lớp SharedLibrary.Transaction. – Lee

1

Bạn có thể muốn xác định lớp giao dịch của bạn như sau

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

Tôi hy vọng điều này sẽ giúp.

+0

Tôi đã thêm điều này vào không gian tên IService nhưng vẫn còn lỗi. Cảm ơn bạn đã cố gắng để giúp đỡ. – Lee

+0

Bạn có thể muốn thực hiện việc này ở cấp độ Giao dịch. Thử nó. – RajN

Các vấn đề liên quan