Duplex Contract GetCallbackChannel always returns a null-instance

Posted by Yaroslav on Stack Overflow See other posts from Stack Overflow or by Yaroslav
Published on 2010-04-11T18:04:15Z Indexed on 2010/04/11 18:13 UTC
Read the original article Hit count: 455

Hi!

Here is the server code:

    using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
   using System.ServiceModel;
  using System.Runtime.Serialization;
  using System.ServiceModel.Description;



   namespace Console_Chat
{


    [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IMyCallbackContract))]
    public interface IMyService
    {
        [OperationContract(IsOneWay = true)]
        void NewMessageToServer(string msg);

        [OperationContract(IsOneWay = false)]
        bool ServerIsResponsible();

    }



    [ServiceContract]
    public interface IMyCallbackContract
    {
        [OperationContract(IsOneWay = true)]
        void NewMessageToClient(string msg);

        [OperationContract(IsOneWay = true)]
        void ClientIsResponsible();



    }


    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class MyService : IMyService
    {

       public IMyCallbackContract callback = null;

    /*
    {
            get
            {
                return OperationContext.Current.GetCallbackChannel<IMyCallbackContract>();
            }
        }
      */  

        public MyService()
        {
            callback = OperationContext.Current.GetCallbackChannel<IMyCallbackContract>();
        }

        public void NewMessageToServer(string msg)

        {
            Console.WriteLine(msg);
        }

        public void NewMessageToClient( string msg)
        {

            callback.NewMessageToClient(msg);
        }


        public bool ServerIsResponsible()
        {
            return true;
        }

    }




    class Server
    {
        static void Main(string[] args)
        {
            String msg = "none";


            ServiceMetadataBehavior behavior = new
            ServiceMetadataBehavior();


            ServiceHost serviceHost = new
            ServiceHost(
            typeof(MyService),
            new Uri("http://localhost:8080/"));

            serviceHost.Description.Behaviors.Add(behavior);

            serviceHost.AddServiceEndpoint(
                typeof(IMetadataExchange),
                MetadataExchangeBindings.CreateMexHttpBinding(),
                "mex");


            serviceHost.AddServiceEndpoint(
                typeof(IMyService),
                new WSDualHttpBinding(),
                "ServiceEndpoint"
                );

            serviceHost.Open();
            Console.WriteLine("Server is up and running");

            MyService server = new MyService();
            server.NewMessageToClient("Hey client!");
/*

             do
            {
                msg = Console.ReadLine();

               // callback.NewMessageToClient(msg);


            } while (msg != "ex");

  */
            Console.ReadLine();     

        }
    }
}

Here is the client's:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.ServiceModel.Description;
using Console_Chat_Client.MyHTTPServiceReference;



namespace Console_Chat_Client
{
    [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IMyCallbackContract))]
    public interface IMyService
    {
        [OperationContract(IsOneWay = true)]
        void NewMessageToServer(string msg);

        [OperationContract(IsOneWay = false)]
        bool ServerIsResponsible();

    }



    [ServiceContract]
    public interface IMyCallbackContract
    {
        [OperationContract(IsOneWay = true)]
        void NewMessageToClient(string msg);

        [OperationContract(IsOneWay = true)]
        void ClientIsResponsible();

    }




    public class MyCallback : Console_Chat_Client.MyHTTPServiceReference.IMyServiceCallback
    {

       static InstanceContext ctx = new InstanceContext(new MyCallback());

       static MyServiceClient client = new MyServiceClient(ctx);



        public void NewMessageToClient(string msg)
        {
            Console.WriteLine(msg);
        }

        public void ClientIsResponsible()
        {

        }


                class Client
                {
                    static void Main(string[] args)
                    {
                        String msg = "none";





                        client.NewMessageToServer(String.Format("Hello server!"));


                        do
                        {
                           msg = Console.ReadLine();
                           if (msg != "ex")
                               client.NewMessageToServer(msg);
                           else client.NewMessageToServer(String.Format("Client terminated"));
                        } while (msg != "ex");
                    }
                }






    }
}

callback = OperationContext.Current.GetCallbackChannel(); This line constanly throws a NullReferenceException, what's the problem?

Thanks!

© Stack Overflow or respective owner

Related posts about wcf

Related posts about nullreferenceexception