如何使用ServiceHost部署WCF应用程序?

编程入门 行业动态 更新时间:2024-10-11 01:17:54
本文介绍了如何使用ServiceHost部署WCF应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的客户端:

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应用程序?

本文发布于:2023-11-13 04:42:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583461.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   应用程序   ServiceHost   WCF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!