0

这是服务器代码:

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

        }
    }
}

这是客户的:

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






    }
}

回调 = OperationContext.Current.GetCallbackChannel(); 此行不断抛出 NullReferenceException,有什么问题?

谢谢!

4

1 回答 1

3

您不能只使用回调协定启动 WCF 服务并立即尝试执行客户端回调。还没有客户。

在您的代码中,我看到您手动实例化 aMyService并尝试执行回调方法。这根本行不通。如果您想使用该GetCallbackChannel方法,则必须在实际存在通道时完成 - 即在远程 WCF 客户端调用的实际操作的上下文中。否则,没有 currentOperationContext并且您将得到一个空引用异常,因为OperationContext.Current返回null

回调旨在用于长时间运行的服务操作。例如:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class MyService : IMyService
{
    // One-way method
    public void PerformLongRunningOperation()
    {
        var callback = 
            OperationContext.Current.GetCallbackChannel<IMyCallbackContract>();
        var result = DoLotsOfWork();
        callback.LongRunningOperationFinished(result);
    }
}

要对此进行测试,您必须实际创建一个客户端 - 启动一个新项目,添加对此服务的引用,实现导入器生成的回调,使用回调创建一个InstanceContext,使用该回调创建客户端代理InstanceContext,最后调用其PerformLongRunningOperation方法.

如果您正在尝试开发 pub/sub 实现,其中客户端实际上并不启动操作,而只是注册自己以接收一些回调,请查看此页面:Using Callback Contracts in WCF for Asynchronous Publish/Subscribe Event-Style Communication .

于 2010-04-11T18:10:54.897 回答