WCF异步回调 [英] WCF asynchronous callback

查看:247
本文介绍了WCF异步回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地在我的code实现的WCF回调格局,现在我要实现异步回调。这里是我的接口code:

  [的ServiceContract(名称=IMessageCallback)]
公共接口IMessageCallback
{
  [OperationContract的(IsOneWay =真)]
  无效OnMessageAdded(字符串消息,日期戳);
}[的ServiceContract(名称=IMessageCallback)]
公共接口IAsyncMessageCallback
{
  [OperationContract的(AsyncPattern =真)]
  IAsyncResult的BeginOnMessageAdded(string信息,日期戳,AsyncCallback的回调,对象asyncState);
  无效EndOnMessageAdded(IAsyncResult的结果);
}[的ServiceContract(CallbackContract = typeof运算(IMessageCallback))]
公共接口即时聊天
{
  [OperationContract的]
  无效方法addMessage(字符串消息);
}

要使用像,所以我宣布我的频道同步回调和端点:

  DuplexChannelFactory<即时聊天> DCF =新DuplexChannelFactory<即时聊天>(新的InstanceContext(本),WSDualHttpBinding_IMessage);
<端点地址=的net.tcp://本地主机:8731 /消息/
            绑定=NetTcpBinding的
            合同=WCFCallbacks.IMessageNAME =WSDualHttpBinding_IMessage>

我有麻烦终端和渠道的正确组合,以利用异步回调。有人能指出我朝着正确的方向吗?

另外,当执行code以下行:

  OperationContext.Current.GetCallbackChannel< IAsyncMessageCallback>();

我收到以下错误:

 无法施展透明代理键入WCFCallbacks.IAsyncMessageCallback


解决方案

您需要服务合同即时聊天的CallbackContract属性更改为该类型(IAsyncMessageCallback)。下面的例子与异步回调运行。

 公共类StackOverflow_5979252
{
    [的ServiceContract(名称=IMessageCallback)]
    公共接口IAsyncMessageCallback
    {
        [OperationContract的(AsyncPattern =真)]
        IAsyncResult的BeginOnMessageAdded(string信息,日期戳,AsyncCallback的回调,对象asyncState);
        无效EndOnMessageAdded(IAsyncResult的结果);
    }
    [的ServiceContract(CallbackContract = typeof运算(IAsyncMessageCallback))]
    公共接口即时聊天
    {
        [OperationContract的]
        无效方法addMessage(字符串消息);
    }
    [ServiceBehavior(IncludeExceptionDetailInFaults = TRUE,ConcurrencyMode = ConcurrencyMode.Multiple)
    公共类服务:即时聊天
    {
        公共无效方法addMessage(字符串消息)
        {
            IAsyncMessageCallback回调= OperationContext.Current.GetCallbackChannel< IAsyncMessageCallback>();
            callback.BeginOnMessageAdded(消息,DateTime.Now,委托(IAsyncResult的AR)
            {
                callback.EndOnMessageAdded(AR);
            }, 空值);
        }
    }
    类MyClientCallback:IAsyncMessageCallback
    {
        公众的IAsyncResult BeginOnMessageAdded(string信息,日期戳,AsyncCallback的回调,对象asyncState)
        {
            动作<字符串,日期> ACT =(TXT,时间)=> {Console.WriteLine([{0}] {1},时间,TXT); };
            返回act.BeginInvoke(MSG,时间戳,回调,asyncState);
        }        公共无效EndOnMessageAdded(IAsyncResult的结果)
        {
            动作&LT;字符串,日期&GT; ACT = (Action<string,DateTime>)((System.Runtime.Remoting.Messaging.AsyncResult)result).AsyncDelegate;
            act.EndInvoke(结果);
        }
    }
    静态绑定GetBinding()
    {
        返回新NetTcpBinding的(SecurityMode.None);
    }
    公共静态无效测试()
    {
        字符串baseAddress =的net.tcp://+ Environment.MachineName +:8000 /服务;
        ServiceHost的主机=新的ServiceHost(typeof运算(服务),新的URI(baseAddress));
        host.AddServiceEndpoint(typeof运算(即时聊天),GetBinding(),);
        host.Open();
        Console.WriteLine(主机开);        的InstanceContext的InstanceContext =新的InstanceContext(新MyClientCallback());
        DuplexChannelFactory&LT;即时聊天&GT;厂=新DuplexChannelFactory&LT;即时聊天&GT;(的InstanceContext,GetBinding(),新的EndpointAddress(baseAddress));
        即时聊天代理= factory.CreateChannel();
        proxy.AddMessage(世界,你好);        Console.Write(preSS ENTER关闭主机);
        到Console.ReadLine();
        ((IClientChannel)代理).Close();
        factory.Close();
        host.Close();
    }
}

I have successfully implemented the WCF callback pattern in my code and now I want to implement an asynchronous callback. Here is my interface code:

[ServiceContract(Name = "IMessageCallback")]
public interface IMessageCallback
{
  [OperationContract(IsOneWay = true)]
  void OnMessageAdded(string message, DateTime timestamp);
}

[ServiceContract(Name="IMessageCallback")]
public interface IAsyncMessageCallback
{
  [OperationContract(AsyncPattern = true)]
  IAsyncResult BeginOnMessageAdded(string msg, DateTime timestamp, AsyncCallback callback, object asyncState);
  void EndOnMessageAdded(IAsyncResult result);
}

[ServiceContract(CallbackContract = typeof(IMessageCallback))]
public interface IMessage
{
  [OperationContract]
  void AddMessage(string message);
}

To use the synchronous callback I declared my channel and endpoint like so:

DuplexChannelFactory<IMessage> dcf = new DuplexChannelFactory<IMessage>(new InstanceContext(this), "WSDualHttpBinding_IMessage");
<endpoint address="net.tcp://localhost:8731/Message/"
            binding="netTcpBinding"
            contract="WCFCallbacks.IMessage" name="WSDualHttpBinding_IMessage">

I am having trouble getting the right combination of endpoint and channel to utilize the asynchronous callback. Can someone point me in the right direction?

In addition when the following line of code is executed:

OperationContext.Current.GetCallbackChannel<IAsyncMessageCallback>();

I get the following error:

Unable to cast transparent proxy to type 'WCFCallbacks.IAsyncMessageCallback'

解决方案

You need to change the CallbackContract property of the service contract IMessage to that type (IAsyncMessageCallback). The example below runs with the async callback.

    public class StackOverflow_5979252
{
    [ServiceContract(Name = "IMessageCallback")]
    public interface IAsyncMessageCallback
    {
        [OperationContract(AsyncPattern = true)]
        IAsyncResult BeginOnMessageAdded(string msg, DateTime timestamp, AsyncCallback callback, object asyncState);
        void EndOnMessageAdded(IAsyncResult result);
    }
    [ServiceContract(CallbackContract = typeof(IAsyncMessageCallback))]
    public interface IMessage
    {
        [OperationContract]
        void AddMessage(string message);
    }
    [ServiceBehavior(IncludeExceptionDetailInFaults = true, ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class Service : IMessage
    {
        public void AddMessage(string message)
        {
            IAsyncMessageCallback callback = OperationContext.Current.GetCallbackChannel<IAsyncMessageCallback>();
            callback.BeginOnMessageAdded(message, DateTime.Now, delegate(IAsyncResult ar)
            {
                callback.EndOnMessageAdded(ar);
            }, null);
        }
    }
    class MyClientCallback : IAsyncMessageCallback
    {
        public IAsyncResult BeginOnMessageAdded(string msg, DateTime timestamp, AsyncCallback callback, object asyncState)
        {
            Action<string, DateTime> act = (txt, time) => { Console.WriteLine("[{0}] {1}", time, txt); };
            return act.BeginInvoke(msg, timestamp, callback, asyncState);
        }

        public void EndOnMessageAdded(IAsyncResult result)
        {
            Action<string,DateTime> act = (Action<string,DateTime>)((System.Runtime.Remoting.Messaging.AsyncResult)result).AsyncDelegate;
            act.EndInvoke(result);
        }
    }
    static Binding GetBinding()
    {
        return new NetTcpBinding(SecurityMode.None);
    }
    public static void Test()
    {
        string baseAddress = "net.tcp://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        host.AddServiceEndpoint(typeof(IMessage), GetBinding(), "");
        host.Open();
        Console.WriteLine("Host opened");

        InstanceContext instanceContext = new InstanceContext(new MyClientCallback());
        DuplexChannelFactory<IMessage> factory = new DuplexChannelFactory<IMessage>(instanceContext, GetBinding(), new EndpointAddress(baseAddress));
        IMessage proxy = factory.CreateChannel();
        proxy.AddMessage("Hello world");

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        ((IClientChannel)proxy).Close();
        factory.Close();
        host.Close();
    }
}

这篇关于WCF异步回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆