首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

WCF学习中碰到的有关问题

2013-04-21 
WCF学习中碰到的问题小弟初学WCF,看书上一段代码,怎么运行都报错,代码如下:using Systemusing System.Col

WCF学习中碰到的问题
小弟初学WCF,看书上一段代码,怎么运行都报错,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace Sender
{
    class sender
    {
        static void Main(string[] args)
        {
            try
            {
                NetTcpBinding binding = new NetTcpBinding();
                using (Message message = Message.CreateMessage(binding.MessageVersion, "sendMessage", "Message Body"))
                {
                    //创建ChannelFactory
                    IChannelFactory<IDuplexChannel> factory = binding.BuildChannelFactory<IDuplexChannel>(new BindingParameterCollection());
                    factory.Open();
                    //这里创建IRequestChannel
                    IDuplexChannel duplexChannel = factory.CreateChannel(new EndpointAddress("net.tcp://localhost:9090/DuplexService/Point2"));
                    duplexChannel.Open();
                    //发送消息
                    duplexChannel.Send(message);
                    Console.WriteLine("已经成功发送消息!");
                    duplexChannel.Close();
                    factory.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.Read();
            }
        }
    }


}
报错信息为:
此通道管理器不支持指定的通道类型 System.ServiceModel.Channels.IDuplexChannel。
参数名: TChannel
请高人指点迷津,不胜感激! WCF?
[解决办法]
使用IDuplexSessionChannel
[解决办法]
参考微软官方说明:http://msdn.microsoft.com/ZH-CN/library/vstudio/system.servicemodel.transfermode(v=vs.110).aspx

引用
将传输模式从 Buffered 更改为 Streamed 还会更改 TCP 和命名管道传输协议的本机通道形状。对于缓冲传输模式,本机通道形状为 IDuplexSessionChannel。对于流传输模式,本机通道为 IRequestChannel 和 IReplyChannel。因此,会话服务协定不能与传输流一起使用。

因此WCF自带的Binding只能支持这三种形状的通道,并且受那个TransferMode的影响,由于默认是Buffered,因此只能使用IDuplexSessionChannel。

热点排行