回调客户端无法在wcf wsdualhttpbinding中工作

时间:2012-01-31 09:28:52

标签: wcf

我有一个wcf双工服务和两个客户端。方案是client1向wcf提交请求,wcf将其存储在DB.client2中处理从DB获取的请求,并将状态发送到wcf服务,从而通知client1状态。 client1的回调地址存储在静态变量中.client2通知wcf,但wcf不通知client1。任何解决方案。 提前致谢

service.cs:

public class Service1 : IService1
    {
        static List<IServiceCallback> list = new List<IServiceCallback>();
        static IServiceCallback Callbck;

        public bool GetData(int value)
        {
            int i = 0;
            string s= string.Format("You entered: {0}", value);
            Callbck = OperationContext.Current.GetCallbackChannel<IServiceCallback>();

            i++;
            list.Add(Callbck);
            Ret("sss...");
            return true;
        }

        public  void Ret(string s)
        {

            foreach (var c in list)
            {

                Callbck.display(s);
                            }
        }

            }

client1.cs:

class Program
    {

        static void Main(string[] args)
        {
                                       InstanceContext instance = new InstanceContext(new Handler());
                Service1Client client = new Service1Client(instance);


                                bool res = client.GetData(123);

                if (res)
                    Console.WriteLine("true");


                    }
    }
    public class Handler :IService1Callback
    {
        public void display(string s)
        {
            Console.WriteLine(s);

        }


    }

client2.cs:

class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            i++;
                        Display();
        }

        public static void Display()
        {
            Console.WriteLine("hello");
            InstanceContext context = new InstanceContext(new Handlerss());
            Service1Client client = new Service1Client(context);

            client.Ret("done");


        }
    }

    class Handlerss : IService1Callback
    {
        public void display(string s)
        {
            Console.WriteLine(s);
        }
    }

1 个答案:

答案 0 :(得分:0)

我没有看到为您的服务设置任何InstanceContextMode?你应该设置单一来让你的应用程序像你想要的那样工作。否则两个客户都会获得新服务。

对于debuging,您可以将以下内容添加到您的服务中。只是为了查看您的客户端回叫频道是否已关闭或出现故障

    public bool GetData(int value)
    {
        int i = 0;
        string s= string.Format("You entered: {0}", value);
        var callback= OperationContext.Current.GetCallbackChannel<IServiceCallback>();

        ICommunicationObject obj = (ICommunicationObject)callback;
        obj.Closed += SubscribedServiceClosed;//write log or console or something else 
        obj.Faulted += SubscribedServiceFaulted;//write log or console or something else

        i++;
        list.Add(callback);
        Ret("sss...");
        return true;
    }