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 サービスを開始し、すぐにクライアント コールバックを実行しようとすることはできません。まだクライアントはありません。

あなたのコードでは、手動で をインスタンス化しMyService、コールバック メソッドを実行しようとしていることがわかります。これは単に機能しません。メソッドを使用する場合GetCallbackChannelは、実際にチャネルが存在するときに実行する必要があります。つまり、リモート WCF クライアントによって呼び出される実際の操作のコンテキストで実行する必要があります。そうしないと、 current がなくOperationContext、 null 参照例外が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 実装を開発しようとしている場合は、このページを参照してください。.

于 2010-04-11T18:10:54.897 に答える