当前位置: 代码迷 >> Web Service >> 请问WCF NetTcpBinding 传送文件流失败有关问题
  详细解决方案

请问WCF NetTcpBinding 传送文件流失败有关问题

热度:148   发布时间:2016-05-02 02:21:44.0
请教WCF NetTcpBinding 传送文件流失败问题
我是看了这篇文章:
http://blog.csdn.net/tcjiaan/article/details/8248543

里面用的是BasicHttpBinding ,我按照里面的测试成功。

但我改为NetTcpBinding,服务端启动正常,但客户端连上去调用发送文件方法时导常:

“System.ServiceModel.CommunicationException”类型的未经处理的异常在 mscorlib.dll 中发生 

其他信息: 套接字连接已中止。这可能是由于处理消息时出错或远程主机超过接收超时或者潜在的网络资源问题导致的。本地套接字超时是“00:00:59.9569975”

{"远程主机强迫关闭了一个现有的连接。"}
-----------------------------------------------------------------------------------------------------
我服务端用的是Console,开发环境为 windows7 + vs.net(2013) + .net4.5

服务端代码如下:

namespace Server
{
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        ResultMessage UpLoadFile(TransferFileMessage transferFileMessage);  
    } 
}

namespace Server
{  
    [MessageContract]
    public class TransferFileMessage
    {
        [MessageHeader] 
        public string File_Name;

        [MessageBodyMember]  
        public Stream File_Stream; 
    }

    [MessageContract]
    public class ResultMessage
    {
        [MessageHeader]
        public string ErrorMessage;
        [MessageBodyMember]
        public bool IsSuccessed;
    }

    class MyService : IService
    {
        public ResultMessage UpLoadFile(TransferFileMessage transferFileMessage)
        {
            ResultMessage resultMessage = new ResultMessage();
            resultMessage.IsSuccessed = false;          
            try
            {
                using (FileStream outputStream = new FileStream(transferFileMessage.File_Name, FileMode.OpenOrCreate, FileAccess.Write))
                {                  
                    transferFileMessage.File_Stream.CopyTo(outputStream);
                    outputStream.Flush();
                    resultMessage.IsSuccessed = true;
                    Console.WriteLine("在{0}接收到客户端发送的流,已保存到new.txt。", DateTime.Now.ToLongTimeString());
                }
            }
            catch(Exception ex)
            {
                resultMessage.IsSuccessed = false;
                resultMessage.ErrorMessage = ex.ToString();
            }
            return resultMessage;
        }  
    }
}




namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri baseAddress = new Uri("net.tcp://localhost:8001/Service");
      
            using (ServiceHost host = new ServiceHost(typeof(MyService), baseAddress))
            {
           
                NetTcpBinding binding = new NetTcpBinding();