将类添加到 WCF 服务库

本文关键字:服务 WCF 添加 | 更新日期: 2023-09-27 18:32:32

我有一个类,它定义了需要在两个单独的应用程序之间共享的事务。它们都具有对此库的引用,并且可以将类用作数据类型,但不能调用其任何方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using ServerLibrary.MarketService;
namespace ServerLibrary
{
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        string GetData(int value);
        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
        [OperationContract]
        bool ProcessTransaction(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; }
        }
    }
    // Transaction class to encapsulate products and checkout data
    [DataContract]
    public class Transaction
    {
        [DataMember]
        public int checkoutID;
        [DataMember]
        public DateTime time;
        [DataMember]
        public List<Product> products;
        [DataMember]
        public double totalPrice;
        [DataMember]
        public bool complete;
        public void Start(int ID)
        {
            checkoutID = ID;
            products = new List<Product>();
            complete = false;
        }
        public void Complete()
        {
            time = DateTime.Now;
            complete = true;
        }
    }
}

我做错了什么?

[更新] 我错过了其余的服务。

谢谢。

将类添加到 WCF 服务库

为了从客户端和服务器使用相同的 .NET 类型,您需要做的是将数据协定类添加到客户端和主机都使用的共享程序集。然后,当主机启动并运行时,并且您执行添加服务引用时,应该有一个复选框,指出重用现有程序集中的类型。

这将使 WCF 使用您期望的方法和数据重新创建您的对象。