需要服务器来调用客户端功能(NO CALLBACK!)

时间:2013-07-29 16:47:35

标签: c# wcf client

我一直在研究这个问题而且我什么都没得到。 我有一台服务器和客户端。

我的客户端确实向服务器请求,服务器运行一些回调。 这很好用。 但是现在,我需要从服务器调用的客户端有一些函数,而不是客户端调用的结果,所以我不能在那里使用回调。 我正在使用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()
        { }
    }
}

SERVER:

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

1 个答案:

答案 0 :(得分:1)

如果您想通知客户您正在寻找Duplex Service服务器上发生的事情。

在完整的.net中,您有2个绑定选项:

  • NetTcpBinding的
  • wsDualHttpBinding

netTcpBinding要好得多,因为它不需要客户端打开端口(wsDualHttpBinding确实需要它)。

老实说,最好的绑定是PollingDuplexHttpBinding,只适用于silverlight。但是,使用basicHttpBinding来模拟它并不困难。

主题非常广泛,所以我建议进一步阅读。

相关问题