WCF Windows窗体客户端对象引用错误

时间:2014-09-13 07:09:02

标签: c# .net wcf

我已经完成了一个wcflibrary并在Windows服务中托管了它。对于客户端我做了一个窗体。但现在,当我尝试运行Windows窗体时,它说:

Object reference not set to an instance of an object.

这是我客户的代码:

 public partial class Form1 : Form
{
  //  ICalculator calcService = null;
  //  IMyWorld myworldService = null;

   ServiceReference1.ICalculator channel = null;
   ServiceReference2.IMyWorld channeln = null;



    public Form1()
    {
        InitializeComponent();

    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        DuplexChannelFactory<ServiceReference1.ICalculator> factory = null;
        DuplexChannelFactory<ServiceReference2.IMyWorld> factoryn = null;
        var callbackClient = new CallBackClient();
        var callbackClientn = new CallBackClientn();
        var context = new InstanceContext(callbackClient);
        var contextn = new InstanceContext(callbackClientn);
        factory = new DuplexChannelFactory<ServiceReference1.ICalculator>(context, "ServiceTCPEndPoint");               
        factoryn = new DuplexChannelFactory<ServiceReference2.IMyWorld>(contextn, "ServiceTCPEndPoint1");
         channel = factory.CreateChannel();
         channeln = factoryn.CreateChannel();
    }
private void button1_Click(object sender, EventArgs e)
    {
        channel.AddTo(1.1);      **Here is where I'm getting the error**
        channeln.Hello();

        channel.SubtractFrom(2.0);
        channel.MultiplyBy(6.0);
        channel.DivideBy(2.0);
        channel.Clear();
    }

在当地人中,变量&#39;频道&#39;保持为null。为什么不在&#39; Form1_Load&#39;中分配。事件

任何线索。

0 个答案:

没有答案