首页新闻找找看学习计划

求救大神。wcf传递Stream为空

0
悬赏园豆:200 [已解决问题] 解决于 2012-10-27 08:49

2个一模一样的wcf。部署在2台服务器上。同时调用的时候,后调用的数据传递Stream为null。

代码:

[ServiceContract]     public interface IService1     {    

     [OperationContract]        

void UploadFile(FileUploadMessage request);   

  }    

[MessageContract]    

public class FileUploadMessage     {          

  // 文件在根目录下的子文件夹名            

[MessageHeader(MustUnderstand = true)]        

public string SavePath;        

   ///文件名           

   [MessageHeader(MustUnderstand = true)]      

   public string FileName; 

   ///文件内容             

  [MessageBodyMember(Order = 1)]      

   public Stream FileData;       

    }

 

 

   public class Service1 : IService1     {       

  public void UploadFile(FileUploadMessage request)         {         

               string uploadFolder = AppDomain.CurrentDomain.BaseDirectory;                 string savaPath = request.SavePath;         

               string fileName = request.FileName;         

        Stream sourceStream = request.FileData;            

     FileStream targetStream = null;             

    if (!savaPath.EndsWith("\\")) savaPath += "\\";                                                uploadFolder = uploadFolder + savaPath;

                // 路径是否存在                

if (!Directory.Exists(uploadFolder))               

  {                     Directory.CreateDirectory(uploadFolder);             

    }            

     string filePath = Path.Combine(uploadFolder, fileName);     

            using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))         

        {                     try                     {

                        const int bufferLen = 4096;                 

        byte[] buffer = new byte[bufferLen];                    

     int count = 0;                     

    while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)                         {                             targetStream.Write(buffer, 0, count);                         }                         targetStream.Close();                  

       sourceStream.Close();                       

                  }                     catch (Exception e)                     {

                    }                 }                    }

 

配置文件::

  <system.serviceModel>     <bindings>       <basicHttpBinding>         <binding name="TransferService"            sendTimeout="00:10:00"                  transferMode="Streamed"                  messageEncoding="Text"                  textEncoding="utf-8"                  maxReceivedMessageSize="9223372036854775807">          

          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"                maxArrayLength="2147483647" maxBytesPerRead="2147483647"                maxNameTableCharCount="2147483647"/>         </binding>       </basicHttpBinding>     </bindings>     <services>       <service behaviorConfiguration="DefaultBehavior" name="WebApplication1.Service1">         <endpoint address="" binding="basicHttpBinding"               bindingConfiguration="TransferService"               contract ="WebApplication1.IService1">         </endpoint>         <host>           <baseAddresses>             <add baseAddress="http://localhost:15246/Service1.svc" />           </baseAddresses>         </host>       </service>     </services>     <behaviors>       <serviceBehaviors>         <behavior name="DefaultBehavior">           <serviceMetadata httpGetEnabled="true" />           <serviceDebug includeExceptionDetailInFaults="true" />           <serviceThrottling maxConcurrentCalls="100" maxConcurrentInstances="100" maxConcurrentSessions="100"/>         </behavior>       </serviceBehaviors>     </behaviors>     <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />   </system.serviceModel>

 

 

 

上传文件:

//A服务器

  s1.Service1Client ser1 = new s1.Service1Client();         
                s1.FileUploadMessage f1 = new s1.FileUploadMessage();
                //文件的名称
                f1.FileName = FileUpload1.FileName;
                //路径
                f1.SavePath = "Product";
                //文件
                f1.FileData = FileUpload1.FileContent;
                // //上传文件
                s1.IService1 channel = ser1.ChannelFactory.CreateChannel();
                channel.UploadFile(f1);

//b服务器器

 s2.Service2Client ser2 = new s2.Service2Client();
           
                s2.FileUploadMessage f2 = new s2.FileUploadMessage();
                //文件的名称
                f2.FileName = FileUpload1.FileName;
                //路径
                f2.SavePath = "Product";
                //文件
                f2.FileData = FileUpload1.FileContent;
                // //上传文件
                s2.IService2 channel2 = ser2.ChannelFactory.CreateChannel();
                channel2.UploadFile(f2);

 

问题:b服务器的文件上传上去为空。后创建的对象传递过去的Stream为空。什么原因。

WCF
jiangcheng的主页 jiangcheng | 初学一级 | 园豆:3
提问于:2012-10-14 21:44
< >
分享
最佳答案
0

可以考虑将上传的文件复制到MemoryStream,然后每次上传到服务器时都复制一份出来上传,或者也可以考虑直接传Byte[],而不是传Stream

收获园豆:100
I,Robot | 大侠五级 |园豆:9461 | 2012-10-15 01:10
其他回答(4)
1
收获园豆:20
chenping2008 | 园豆:9836 (大侠五级) | 2012-10-15 09:31
0

建议使用byte[]方式进行数据传输

收获园豆:40
az235 | 园豆:8283 (大侠五级) | 2012-10-15 09:42
0

看看你Stream.Position 这个属性,如果值=Stream.length, 把它赋值为0. 我没有仔细看你的代码,我以前遇到过这个问题,每次Stream的读取都是从Position 的位置开始读的,所以如果值=Stream.length,是读不到任何数据的

收获园豆:20
鸿鹄_A | 园豆:222 (菜鸟二级) | 2012-10-16 15:50
0

messageEncoding="MTOM" 试试

收获园豆:20
sym_cn | 园豆:798 (小虾三级) | 2012-10-26 17:53

WCF传输的时候需要序列化  序列化之后需要Encoding  Encoding的方式有三种 binary, Text,MTOM,文件流比较大建议用MTOM

支持(0) 反对(0) sym_cn | 园豆:798 (小虾三级) | 2012-10-26 17:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册