0

私はこれについていくつかの研究を行ってきましたが、何も得られませんでした。サーバーとクライアントがあります。

私のクライアントはサーバーにリクエストを行い、サーバーはいくつかのコールバックを実行します。これはうまくいきます。しかし今、サーバーから呼び出す必要があるクライアントの関数がいくつかありますが、それらはクライアント呼び出しの結果ではないため、そこでコールバックを使用することはできません。私はWCFと.net 4.0を使用しています

助言がありますか?

クライアント:

using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Runtime.Serialization;
using System.IO;
using System.Collections;

namespace WCFClient
{
    [ServiceContract(SessionMode = SessionMode.Required,
      CallbackContract = typeof(ICallbacks))]
    public interface IMessageHandler
    {
        [OperationContract]
        void HandleMessage();
    }

    public interface ICallbacks
    {
        [OperationContract(IsOneWay = true)]
        void QueuePaths_Callback(string cPath, string EPath, string RPath, string IPath, string OPath);
    }

    public class Callbacks : ICallbacks
    {
        public void QueuePaths_Callback(string cPath)
        {
            Console.WriteLine("QueuePaths_Callback: " + cPath);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Callbacks myCallbacks = new Callbacks();

            DuplexChannelFactory<IMessageHandler> pipeFactory =
               new DuplexChannelFactory<IMessageHandler>(
                  myCallbacks,
                  new NetNamedPipeBinding(),
                  new EndpointAddress(
                     "net.pipe://localhost/PipeReverse"));

            IMessageHandler pipeProxy =
              pipeFactory.CreateChannel();

            while (true)
            {
                string str = Console.ReadLine();
                pipeProxy.HandleMessage();//send the type for example
            }
        }

        public void IWANTTOCALLTHISFROMSERVER()
        { }
    }
}

サーバ:

namespace WCFServer
{

    [ServiceContract(SessionMode = SessionMode.Required,
       CallbackContract = typeof(ICallbacks))]
    public interface IMessageHandler
    {
        [OperationContract]
        void HandleMessage();
    }

    public interface ICallbacks
    {
        [OperationContract(IsOneWay = true)]
        void QueuePaths_Callback(string cPath);
    }
    public class StringReverser : IMessageHandler
    {
        public void HandleMessage()//handle the type and do the request
        {
            ICallbacks callbacks = OperationContext.Current.GetCallbackChannel<ICallbacks>();
            callbacks.QueuePaths_Callback("path1");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(
              typeof(StringReverser),
              new Uri[]{
          new Uri("net.pipe://localhost")
        }))
            {

                host.AddServiceEndpoint(typeof(IMessageHandler),
                  new NetNamedPipeBinding(),
                  "PipeReverse");
                host.
                host.Open();

                Console.WriteLine("Service is available. " +
                  "Press <ENTER> to exit.");
                Console.ReadLine();
                //BLA BLA BLA 
                //CALL IWANTTOCALLTHISFROMSERVER();
                host.Close();
            }
        }
    }
}
4

1 に答える 1

1

サーバーで何かが起こったことをクライアントに通知したい場合は、 Duplex Serviceを探しています。

完全な .net では、バインディングに 2 つのオプションがあります。

  • netTcpバインディング
  • wsDualHttpBinding

netTcpBinding は、クライアントがポートを開く必要がないため、はるかに優れています (wsDualHttpBinding では必要です)。

正直なところ、最良のバインディングは、 Silverlightでのみ使用できるPollingDuplexHttpBindingです。ただし、basicHttpBinding を使用してエミュレートすることはそれほど難しくありません。

トピックはかなり広いので、さらに読むことをお勧めします。

于 2013-07-29T17:00:00.190 に答える