我的客户端:
class Program { static void Main(string[] args) { IService service = ChannelFactory<IService>.CreateChannel(new WSDualHttpBinding(),new EndpointAddress("localhost:8000/ServiceImpl")); if (service.NotifyClient()) { //CallBack initialization IClientCallBack callback = new ClientImpl(); InstanceContext callbackInstance = new InstanceContext(new ServiceHost(typeof(IClientCallBack)), callback); } Console.ReadLine(); } }public class ClientImpl:IClientCallBack { public string GetClientName() { return "OK"; } }
我的房东:
[ServiceContract(CallbackContract=typeof(IClientCallBack))] public interface IService { [OperationContract] bool NotifyClient(); } public interface IClientCallBack { [OperationContract] string GetClientName(); }public class ServiceImpl:IService { public bool NotifyClient() { return true; } }
class Program { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(ServiceImpl))) { host.AddServiceEndpoint(typeof(IService), new WSDualHttpBinding(), "localhost:8000/ServiceImpl"); host.Open(); Console.WriteLine("Client Is:" + OperationContext.Current.GetCallbackChannel<IClientCallBack>().GetClientName()); Console.ReadLine(); } } }
当我运行Host应用程序时,在带下划线的部分会引发空引用"错误.
When I run Host application, it throws error of "Null reference at the underlined part……
有什么理由吗?
如何解决纯代码……对WCF感到困惑....:(
How to fix in pure codes……puzzled at WCF....:(
推荐答案
Hi
不是使用ServiceHost的专家,但是我会说操作上下文为null,因为客户端尚未连接.当您调用host.Open()时,它将开始侦听以等待客户端连接.
Not an expert in using ServiceHost but I would say that the operation context is null because the client has not yet connected. When you call host.Open() it just starts listening waiting for a client to connect.
如果仅在NotifyClient中包含该行,则应该有一个操作上下文.
If you just include that line inside the NotifyClient, then there should be an operation context available.
此外,我不确定回调初始化是否正确.也许您应该看一下类似msdn.microsoft/zh-CN/magazine/cc163537.aspx
Also, I am not sure if the callback initialization is right. Maybe you should take a look at some example like thismsdn.microsoft/en-us/magazine/cc163537.aspx
更多推荐
如何使用ServiceHost部署WCF应用程序?
发布评论