如何使用ServiceHost部署WCF应用程序? [英] How can I deploy a WCF application with ServiceHost?

查看:77
本文介绍了如何使用ServiceHost部署WCF应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的客户端:

class Program
    {
        static void Main(string[] args)
        {
          IService service =  ChannelFactory<IService>.CreateChannel(new WSDualHttpBinding(),new EndpointAddress("http://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(), "http://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. 

此外,我不确定回调初始化是否正确.也许您应该看一下类似http://msdn.microsoft.com/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 this http://msdn.microsoft.com/en-us/magazine/cc163537.aspx


这篇关于如何使用ServiceHost部署WCF应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆