none
WCF Windows form client object reference error RRS feed

  • Question

  • I've done a wcflibrary and hosted this in a windows service. For the client I did a windows form. But now when I try to run the windows form it says:

    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();
        }

    And in the locals the variable 'channel' stays as null. Why doesn't it get assigned in the 'Form1_Load' event.

    Any clues.

    Saturday, September 13, 2014 10:51 AM

Answers

  • Any clues.

    I suggest that you start debugging the service. That's the only thing you can do. The service may have blown up, and you'll never know it, because you are not making calls to the service from the client to trap any error thrown by the service across boundaries.

    try
    {
        ...
        client.Close();
    }
    catch (CommunicationException e)
    {
        ...
        client.Abort();
    }
    catch (TimeoutException e)
    {
        ...
        client.Abort();
    }
    catch (Exception e)
    {
        ...
        client.Abort();
        throw;
    }

    The example above is from the link above and making calls to a typed client, such as used by a WCF client.

    http://msdn.microsoft.com/en-us/library/aa355056(v=vs.110).aspx

    Sunday, September 14, 2014 9:49 PM