C# 向WCF服务库添加类
我有一个类,它定义了需要在两个单独的应用程序之间共享的事务。它们都有对此库的引用,并且可以将该类用作数据类型,但不能调用其任何方法:C# 向WCF服务库添加类,c#,wcf,service,C#,Wcf,Service,我有一个类,它定义了需要在两个单独的应用程序之间共享的事务。它们都有对此库的引用,并且可以将该类用作数据类型,但不能调用其任何方法: using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; using ServerLibrary.MarketService;
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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Runtime.Serialization;
使用System.ServiceModel;
使用系统文本;
使用ServerLibrary.MarketService;
命名空间服务器库
{
[服务合同]
公共接口设备
{
[经营合同]
字符串GetData(int值);
[经营合同]
CompositeType GetDataUsingDataContract(CompositeType composite);
[经营合同]
bool ProcessTransaction(事务处理);
}
[数据合同]
公共类复合类型
{
布尔布尔值=真;
string stringValue=“Hello”;
[数据成员]
公共布尔布尔值
{
获取{返回布尔值;}
设置{boolValue=value;}
}
[数据成员]
公共字符串字符串值
{
获取{return stringValue;}
设置{stringValue=value;}
}
}
//用于封装产品和签出数据的事务类
[数据合同]
公共类事务
{
[数据成员]
公共int签出ID;
[数据成员]
公共日期时间;
[数据成员]
公开上市产品;
[数据成员]
公共双总价;
[数据成员]
公共布尔完成;
公共无效开始(int ID)
{
checkoutID=ID;
产品=新列表();
完整=错误;
}
公开作废完成()
{
时间=日期时间。现在;
完整=正确;
}
}
}
我做错了什么
[更新]我错过了剩下的服务
谢谢。为了从客户端和服务器使用相同的.NET类型,您需要做的是将数据协定类添加到客户端和主机都使用的共享程序集中。然后,当您的主机启动并运行时,并且您执行添加服务引用时,应该有一个复选框,表示重用现有程序集中的类型
这将使WCF使用您期望的方法和数据重新创建您的对象。产品是否也标记了DataContract属性?产品是来自另一个名为MarketService的服务引用的类。托管服务时是否会给您带来某种错误?问题是什么?DataMember也被添加到您希望WCF序列化的每个字段或属性中。您正在尝试将属性添加到类中吗?托管服务没有问题,就在我尝试在使用服务的客户端中使用事务类时,它声称数据成员和方法不存在。就像让客户端和服务器引用本地DLL,共享类在其中一样?是的,没错,它们都会引用共享合约DLL,当您在客户端添加服务引用时,请确保要检查是否重用现有的typesOK,谢谢。我曾想过这样做,但认为它在某种程度上击败了对象,使用WCF,但如果这是一种方式,那就太好了!