.NET : 在实现WCF的双工服务时可能遇到的问题

今天继续在讲WCF方面的课程。期间演示双工服务的时候,总是遇到超时的问题,代码怎么看都是正确的,硬是没有找到原因。回到酒店,再次打开确又是正常的。总结下来,原因应该是我在IE中设置了连接代理的原因。

image

1. 合约

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;

namespace Contracts
{
    [
        ServiceContract(
            CallbackContract=typeof(ICallbackContract))
    ]
    public interface IChat
    {
        [OperationContract]
        void SendMessage(string msg);
    }

    [ServiceContract]
    public interface ICallbackContract {
        [OperationContract]
        void ReplyMessage(string msg);
    }
}

2. 服务

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using Contracts;

namespace HelloWorldServiceLib
{
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
    public class ChatService:IChat
    {
        #region IChat 成员

        public void SendMessage(string msg)
        {
            Console.WriteLine("时间:{0},收到消息:{1}",
                DateTime.Now.ToString(),
                msg);

            ICallbackContract callback = OperationContext.Current.GetCallbackChannel
  
  
   
   ();

            callback.ReplyMessage(
   
   "服务器回复消息:我已收到"+msg);

        }

        
   
   #endregion
    }
}

  
  
3. 宿主
         using (ServiceHost host =
                new ServiceHost(
                    typeof(HelloWorldServiceLib.ChatService),
                    new Uri("http://localhost:8000/ChatRoom")
                    )) {
                        host.AddServiceEndpoint(
                            "Contracts.IChat",
                            new WSDualHttpBinding(),
                            "");
                        host.Open();
                        Console.WriteLine("聊天室已经准备好");
                        Console.Read();
            }

4. 客户端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using Contracts;
using System.ServiceModel.Channels;

namespace ChatClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Binding binding = new WSDualHttpBinding();
            EndpointAddress address = new EndpointAddress("http://localhost:8000/ChatRoom");
            CallBackHandler callback = new CallBackHandler();

            ChatClient client = new ChatClient(
                new InstanceContext(callback),
                binding,
                address);

            client.SendMessage("Hello,World");
            
            Console.Read();

        }
    }


    class ChatClient : ClientBase
  
  
   
   , IChat {

        
   
   public ChatClient(InstanceContext callback,Binding binding, EndpointAddress address) : 
   
   base(callback,binding, address) { }

        
   
   #region IChatContract 成员

        
   
   public 
   
   void SendMessage(
   
   string message)
        {
            Channel.SendMessage(message);
        }

        
   
   #endregion
    }

    
   
   class CallBackHandler : ICallbackContract {

        
   
   #region ICallbackContract 成员

        
   
   public 
   
   void ReplyMessage(
   
   string msg)
        {
            Console.WriteLine(msg);
        }

        
   
   #endregion
    }
}

  
  

 

效果如下

1. 服务器初始化

image

2. 服务器收到消息

image

3. 客户端收到消息

image

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值