在WCF服务中标记引用的库类

时间:2012-04-29 19:27:28

标签: c# wcf dll service

这跟我提出的上一个问题有关:

我有一个定义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类?

[UPDATE]

以下是我尝试托管此服务时收到的错误消息:

  

System.Runtime.Serialization.InvalidDataContractException:Type   'SharedLibrary.Transaction'无法序列化。考虑标记它   使用DataContractAttribute属性,并标记其所有内容   要使用DataMemberAttribute属性序列化的成员。   如果类型是集合,请考虑使用   CollectionDataContractAttribute。请参阅Microsoft .NET Framework   其他支持类型的文档。在   System.Runtime.Serialization.DataContract.DataContractCriticalHelper.ThrowInvalidDataContractException(字符串   消息,类型类型)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(类型   输入)   System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.ValidateDataContractType(类型   输入)   System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.CreatePartInfo(MessagePartDescription   部分,OperationFormatStyle风格,   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   操作,布尔& formatRequest,Boolean& formatReply,Boolean   isProxy)at   System.ServiceModel.Description.DataContractSerializerOperationBehavior.System.ServiceModel.Description.IOperationBehavior.ApplyDispatchBehavior(OperationDescription   描述,DispatchOperation dispatch)at   System.ServiceModel.Description.DispatcherBuilder.BindOperations(ContractDescription   合同,ClientRuntime代理,DispatchRuntime调度)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(时间跨度   System.ServiceModel.Channels.CommunicationObject.Open()的超时)   在Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfo   信息)

这里要求的是包含Transaction:

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

感谢。

2 个答案:

答案 0 :(得分:1)

  

我是否必须使用[attribute]标题在此处标记Transaction类?

不,你不应该这样做,但建议你这样做。请参阅Using Data Contracts


问题是您在List<object>中传递派生对象。

您必须告诉服务使用ServiceKnownType属性处理哪些类型的对象:

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

答案 1 :(得分:1)

您可能希望将交易类定义如下

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

我希望这会有所帮助。