0

Client updates its object on getting callback from the server

//ATC_Slaves.cs, on client side
namespace ATCslave
{
      [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
   public  class ATC_Slaves :Iservercallback
    {
        static Airport a;
        static List<Airplane> b = new List<Airplane>();
       static List<AirRoute> c = new List<AirRoute>();
       public ATC_Slaves(Airport d, List<Airplane> e,List<AirRoute> f)
        {
            a = d;
            b = e;
            c = f;
        }
          public ATC_Slaves()
       {

       }
       public void do_update()
       {

           System.Console.Write(a.AirportID + a.AirportName);
           System.Console.ReadLine();
           //check which flight need to takeoff, increase time duration of every flight, and make sure the air route list is also changed 
           //check which flight have to land, check on the basis of fuel left.
           //check the outbound flights , update its fuel  if the flight is very close to the destination airport hand it over to the next airport

       }
    }

}

The Service object of the Master server

//Pogram.cs,, on client side
using Master;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace ATCslave
{
      [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
   public  class ATC_Slaves :Iservercallback
    {
        static Airport a;
        static List<Airplane> b = new List<Airplane>();
       static List<AirRoute> c = new List<AirRoute>();
       public ATC_Slaves(Airport d, List<Airplane> e,List<AirRoute> f)
        {
            a = d;
            b = e;
            c = f;
        }
          public ATC_Slaves()
       {

       }
       public void do_update()
       {

           System.Console.Write(a.AirportID + a.AirportName);
           System.Console.ReadLine();
           //check which flight need to takeoff, increase time duration of every flight, and make sure the air route list is also changed 
           //check which flight have to land, check on the basis of fuel left.
           //check the outbound flights , update its fuel  if the flight is very close to the destination airport hand it over to the next airport

       }
    }

}
// on server IMaster.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace Master
{
    //callback interface
    [ServiceContract]
    public interface Iservercallback
    {
        [OperationContract(IsOneWay=true)]
    void do_update();
    }

    [ServiceContract(CallbackContract=typeof(Iservercallback))]
   public interface IMaster
    {
        [OperationContract]
        Airplane get_airplane(int id);
        [OperationContract]
        AirRoute get_airroute(int id);
        [OperationContract]
        Airport addslave();
        [OperationContract]
        void do_long_work();
    }
}

// On server side,, MasterImpl.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace Master
{
    [ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Multiple,UseSynchronizationContext=false, InstanceContextMode=InstanceContextMode.Single)]
    class MasterImpl :IMaster
    {
        int i = 1;
      public  List<Iservercallback> list_of_slaves=new List<Iservercallback>();
        public Airplane get_airplane(int id)
        {
            Airplane air_p = new Airplane(i);
            return air_p;
        }
        public AirRoute get_airroute(int id)
        {
            AirRoute air_route = new AirRoute(id);
            return air_route;
        }
        public Airport addslave()
        {
            // get a reference to the client side callback object
            Iservercallback cb= OperationContext.Current.GetCallbackChannel<Iservercallback>();
            list_of_slaves.Add(cb);
            System.Console.WriteLine("hey**");
             Airport a1 = new Airport(i);
             i++;
            return a1;

        }
        public void do_long_work()
        {
            list_of_slaves[0].do_update();
        }

    }
}
// on server side, Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ATCDatabase;
using System.ServiceModel;
//create an interface and its implementation class
namespace Master
{
    class Program
    {
        public  delegate void Binaryop();

        static void Main(string[] args)
        {


            MasterImpl m = new MasterImpl();
            ServiceHost host;
            NetTcpBinding tcpBinding = new NetTcpBinding();
            host = new ServiceHost(m);
            host.AddServiceEndpoint(typeof(IMaster), tcpBinding,"net.tcp://localhost:8005/MasterService");
            host.Open();
            System.Console.WriteLine("press Enter to exit");
            int ip=0;
            List<Iservercallback> c = m.list_of_slaves;


          while(true)
          {
              if(c.Count!=0)
              {
                  if (c[ip] != null)
                      c[ip].do_update();
                      //System.Console.WriteLine(c.Count);
              }
          }


               // block waiting for client requests

            host.Close();
        }
    }
}

My question is when the server do callback with the list of slaves object, on the client, on the client side null reference exception occurs

 System.Console.Write(a.AirportID + a.AirportName);// on this line.

I am confused why the static variables are null.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Keya Patel
  • 29
  • 3
  • 1
    Well this can't be caught by eye ball neither by copy/paste. Place everything in one block in one name space so it can be easily replicated (Indicate all possible additional required references) Or narrow down your problem/code. – yazanpro May 06 '15 at 03:11
  • This looks like a bad design. Are you aware that the `static` fields will be shared across all instances of the class? You will overwrite `a` every time you create a new instance of `ATC_Slaves` using the constructor with the three parameters. – John Saunders May 06 '15 at 03:15
  • Yes, I know , but when the server callbacks using stored callback objects, the static fields intialized on the client side in the Program.cs are somehow lost. – Keya Patel May 06 '15 at 03:17
  • Please debug, using the advice provided in the duplicate question. When you can ask a more specific question than "why does NullReferenceException occur?", please post that as a new question, with the specific details of that question, in which you make clear what the underlying cause of the null reference is, and in which you are asking for _specific_ help resolve that underlying cause. – Peter Duniho May 06 '15 at 04:27

0 Answers0