服务器回调期间的NullReferenceException

时间:2015-05-06 02:35:31

标签: c# wcf nullreferenceexception

客户端在从服务器获取回调时更新其对象

//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

       }
    }

}

主服务器的服务对象

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

我的问题是当服务器使用slaves对象列表进行回调时,在客户端上,在客户端发生空引用异常

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

我很困惑为什么静态变量为null。

0 个答案:

没有答案