2012-04-29 13 views
5

これは前の質問との関連です。WCFサービスで参照されているライブラリクラスをマークする

私はトランザクションクラスを定義するDLLを持っています。これは、WCFサービスライブラリとクライアントアプリケーションによって参照されます。 DLLをシリアル化できないため、サービスライブラリをホストできないというエラーが表示されます。ここで

は、サービスコードです:

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

は、私は[属性]のヘッダーとここトランザクションクラスをマークする必要がありますか? DLL含むトランザクションをここに要求された

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)

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

おかげで、私は、このサービスをホストしようとすると、

[UPDATE]

は、ここで私は取得エラーメッセージです。

+0

C#/ WCFのどのバージョンをお使いですか? –

+0

.NET 4を使用しています。 – Lee

+0

'SharedLibrary.Transaction'の定義を追加できますか? –

答えて

1

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

いいえ、必要はありませんが、お勧めします。 Using Data Contractsを参照してください。


問題は、派生オブジェクトをList<object>に渡すことです。

あなたはServiceKnownType属性で処理するために、どのような種類のオブジェクトのサービスを指示する必要があります:

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

Nicholasありがとうございますが、私はまだ同じエラーが発生しています。 MarketService.Productをtypeof()として適用しました。これはオブジェクトのリストとなります。 – Lee

+0

次に試すべきことは、 'products'リストに項目がなく、' Transaction'オブジェクトを送ることです。また、他のエラーが出ていますか? –

+0

私は実際にオブジェクトを渡すことはできません。これらのエラーは、WCFサービスライブラリをビルドしようとしたときに発生し、Visual Studioによって自動的にホストされます。 SharedLibrary.Transactionクラスをシリアライズする以外の新しいエラーはありません。 – Lee

1

あなたが、私はこのことができます願ってい

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

以下のようにトランザクション・クラスを定義することもできます。

+0

これをIService名前空間に追加しましたが、エラーは残ります。助けてくれてありがとう。 – Lee

+0

これはトランザクションクラスレベルで行うことができます。それを試してみてください。 – RajN

関連する問題