C# 需要服务器调用客户端函数(无回调!)

C# 需要服务器调用客户端函数(无回调!),c#,wcf,client,C#,Wcf,Client,我一直在做这方面的研究,结果一无所获。 我有一个服务器和客户端 我的客户端向服务器发出请求,服务器运行一些回调。 这个很好用。 但是现在,我需要从服务器调用一些来自客户端的函数,这些函数不是客户端调用的结果,因此我不能在那里使用回调。 我正在使用WCF和.NET4.0 有什么建议吗 客户: using System; using System.ServiceModel; using System.ServiceModel.Channels; using System.Runtime.Serial

我一直在做这方面的研究,结果一无所获。 我有一个服务器和客户端

我的客户端向服务器发出请求,服务器运行一些回调。 这个很好用。 但是现在,我需要从服务器调用一些来自客户端的函数,这些函数不是客户端调用的结果,因此我不能在那里使用回调。 我正在使用WCF和.NET4.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()
        { }
    }
}
使用系统;
使用System.ServiceModel;
使用System.ServiceModel.Channel;
使用System.Runtime.Serialization;
使用System.IO;
使用系统集合;
命名空间WCFClient
{
[ServiceContract(SessionMode=SessionMode.Required,
CallbackContract=typeof(ICallbacks))]
公共接口IMessageHandler
{
[经营合同]
无效HandleMessage();
}
公共接口ICallbacks
{
[运营合同(IsOneWay=true)]
void QueuePaths\u回调(字符串cPath、字符串EPath、字符串RPath、字符串IPath、字符串OPath);
}
公共类回调:ICallbacks
{
公共无效队列路径\u回调(字符串cPath)
{
WriteLine(“队列路径”\u回调:“+cPath”);
}
}
班级计划
{
静态void Main(字符串[]参数)
{
Callbacks myCallbacks=新回调();
双工管道工厂=
新双工工厂(
我的回电,
新建NetNamedPipeBinding(),
新端点地址(
“净。pipe://localhost/PipeReverse"));
IMessageHandler管道代理=
CreateChannel();
while(true)
{
string str=Console.ReadLine();
pipeProxy.HandleMessage();//例如发送类型
}
}
public void i希望调用此fromserver()
{ }
}
}
服务器:

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();
            }
        }
    }
}
命名空间WCFServer
{
[ServiceContract(SessionMode=SessionMode.Required,
CallbackContract=typeof(ICallbacks))]
公共接口IMessageHandler
{
[经营合同]
无效HandleMessage();
}
公共接口ICallbacks
{
[运营合同(IsOneWay=true)]
void QueuePaths\u回调(字符串cPath);
}
公共类StringReverser:IMessageHandler
{
public void HandleMessage()//处理类型并执行请求
{
ICallbacks callbacks=OperationContext.Current.GetCallbackChannel();
callbacks.QueuePaths_Callback(“path1”);
}
}
班级计划
{
静态void Main(字符串[]参数)
{
使用(ServiceHost主机=新ServiceHost主机)(
类型(桁条反向器),
新Uri[]{
新Uri(“net。pipe://localhost")
}))
{
host.AddServiceEndpoint(typeof(IMessageHandler),
新建NetNamedPipeBinding(),
“反向”);
主办
host.Open();
Console.WriteLine(“服务可用。”+
“按退出”。);
Console.ReadLine();
//呜呜呜呜
//调用IWANTTOCALLTHISFROMSERVER();
host.Close();
}
}
}
}

如果您想通知客户端您正在查找的服务器上发生了问题

在full.net中,您有两个绑定选项:

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

老实说,最好的绑定是只对silverlight可用。然而,使用basicHttpBinding模拟它并不难


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

因此,基本上您想通知客户端服务器上发生了什么事情?请看我已经在使用DuplexChannelFactory=new DuplexChannelFactory(myCallbacks,new NetNamedPipeBinding(),新端点地址(“net。pipe://localhost/PipeReverse"));有什么办法可以这样做吗?@AnnArbor87好的,你有回拨合同之类的东西吗?也许你应该发布你的代码,告诉我什么不起作用。我想也许我没有正确地解释它。我不想回电话。我不希望客户端调用服务器,服务器执行回调。我希望服务器从客户端调用函数。就像我的客户打电话给server@AnnArbor87回调是实现这类功能的标准方法—它有什么问题?唯一的限制是客户端必须进行第一次调用。如果您不希望回调,那么客户端应该像服务器一样,公开自己的服务。