我正在修改 the code in this tutorial构建一些基本的订阅推送 wcf 客户端/服务器类,我刚刚遇到了一些困难。
本教程中的服务器类是使用以下代码创建的:
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(
typeof(StringReverser),
new Uri[]{
new Uri("net.pipe://localhost")
}))
{
host.AddServiceEndpoint(typeof(IStringReverser),
new NetNamedPipeBinding(),
"PipeReverse");
host.Open();
Console.WriteLine("Service is available. " +
"Press <ENTER> to exit.");
Console.ReadLine();
host.Close();
}
}
}
我假设它发布了一个 StringReverser 的实例,我的问题是我需要对该实例的引用,以便我可以调用它的方法将数据推送回客户端。
在本教程中,服务器仅使用回调方法回复客户端,而我将对客户端的引用存储在订阅者列表中。当我需要将数据推送回客户端时,我需要对服务对象的引用,这样我才能真正利用回调。
有没有一种方法可以使用 WCF 发布服务,让您可以引用服务对象?或者我可以从 host 对象获取对服务对象的引用吗?
任何帮助将不胜感激...
最佳答案
您可以在 StringReverser 类中使用单例模式并将其实例传递给 ServiceHost 构造函数:
ServiceHost host = new ServiceHost(
StringReverser.Instance,
new Uri[]{new Uri("net.pipe://localhost")}
);
关于c# - 如何从创建它的代码中获取对 WCF 服务对象的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/586827/