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

标签 c# wcf dll service

这与我之前提出的问题有关:

我有一个定义事务类的 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] header 标记 Transaction 类?

[更新]

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

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)

此处请求的是包含事务的 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;
            }
        }
}

谢谢。

最佳答案

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

关于c# - 在 WCF 服务中标记引用的库类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10375226/

相关文章:

c# - 如何禁用和启用点击接收...?

wcf - 无法从配置文件中找到具有名称和契约(Contract)的端点元素

c# - OperationContext.Current 中的 WCF MessageHeaders

excel - 使用 Excel VBA 从 Rust DLL 调用函数

c++ - DLL 的 std::set_terminate 吗?

c# - C# 中 COM 的松散耦合或如何避免 COMException 0x80040154

c# - 来自其 PropertyDescriptor 的 DataRow 的 PropertyInfo

c# - 在 SQL 或 C# 中对列求和

wcf - 添加服务引用正在生成消息契约

c++ - 在另一个 VS 项目中引用函数模板的显式实例化时出现 LNK2019 错误