【发布时间】:2011-09-07 18:47:31
【问题描述】:
我有一个 WCF 服务,它公开了一个通用接口(并且该服务有一个实现该接口的通用类)。
然后我尝试在托管控制台应用程序中托管此服务(现在仅用于测试目的)。 ThreadStart 行导致错误提示 type for T not found。
现在我不能通过执行 Main(string[] args) where T: IComparable 来使 Main 通用,因为它说,找不到 Main 入口点。
我的问题是一般如何处理这种情况?
// Service Hosting app
static void Main(string[] args)
{
new Thread(new ThreadStart(StartBSTService<T>)).Start();
}
static void StartBSTService<T>() where T : IComparable<T>
{
string baseAddress = "http://localhost:8080/bst";
StartAService(typeof(BSTService<T>), baseAddress);
}
编辑:同时添加服务类
[ServiceContract(Namespace = "http://Microsoft.Samples.GettingStarted")]
public interface IBSTService<T> where T : IComparable<T> //: ICollection<T>
{
[OperationContract]
void Add(T toAdd);
// For brevity, not providing all other methods
// but they are similar IColleciton methods.
}
public class BSTService<T> : IBSTService<T> where T : IComparable<T>
{
BinarySearchTree<T> tree = new BinarySearchTree<T>();
public void Add(T toAdd)
{
tree.Add(toAdd);
}
}
客户端会像使用任何泛型类型一样使用它:
BSTService<string> client = new BSTService<string>;
// OR
BSTService<int> client = new BSTService<int>;
EDIT2: @asawyer 的观点似乎合乎逻辑,Main 是泛型类的消费者,所以它应该提供类型,但是我是否必须为每种类型启动一个新端点?以及如何处理。就像我可以编写一个服务包装器,它只公开一种方法,比如 INIT(Type typeOfBST)。客户端调用它来告诉服务他想要启动 int 或 string BST。然后客户端调用具有给定类型的真实方法,并且服务将这些调用引导到不同的端点,每个端点都暴露了不同类型的 BST。
一般情况下如何处理?
【问题讨论】: