5

これは、私が尋ねた以前の質問に関連しています。

Transaction クラスを定義する 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; }
        }
    }
}

ここで [attribute] ヘッダーを使用して Transaction クラスをマークする必要がありますか?

[アップデート]

このサービスをホストしようとすると、次のエラー メッセージが表示されます。

System.Runtime.Serialization.InvalidDataContractException: タイプ 'SharedLibrary.Transaction' をシリアル化できません。これを DataContractAttribute 属性でマークし、シリアル化するすべてのメンバーを DataMemberAttribute 属性でマークすることを検討してください。型がコレクションの場合は、CollectionDataContractAttribute でマークすることを検討してください。サポートされているその他の型については、Microsoft .NET Framework のドキュメントを参照してください。System.Runtime.Serialization.DataContract.DataContractCriticalHelper.ThrowInvalidDataContractException (文字列メッセージ、型の種類) で System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract (Int32 id、RuntimeTypeHandle typeHandle、型の型) で System.Runtime.Serialization.DataContract で。 DataContractCriticalHelper.

ここで要求されているのは、トランザクションを含む DLL です。

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

ありがとう。

4

2 に答える 2

1

ここで [attribute] ヘッダーを使用して Transaction クラスをマークする必要がありますか?

いいえ、する必要はありませんが、お勧めします。データ コントラクトの使用を参照してください。


問題は、派生オブジェクトをList<object>.

ServiceKnownType属性を使用して、どのタイプのオブジェクトを処理するかをサービスに伝える必要があります。

[OperationContract]
[ServiceKnownType( typeof( MarketService.XXX ) )]
bool ProcessTransaction(SharedLibrary.Transaction transaction);
于 2012-04-29T19:52:31.620 に答える
1

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

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

これが役立つことを願っています。

于 2012-04-29T21:12:25.477 に答える