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为空。什么原因。
可以考虑将上传的文件复制到MemoryStream,然后每次上传到服务器时都复制一份出来上传,或者也可以考虑直接传Byte[],而不是传Stream
建议使用byte[]方式进行数据传输
看看你Stream.Position 这个属性,如果值=Stream.length, 把它赋值为0. 我没有仔细看你的代码,我以前遇到过这个问题,每次Stream的读取都是从Position 的位置开始读的,所以如果值=Stream.length,是读不到任何数据的
messageEncoding="MTOM" 试试
WCF传输的时候需要序列化 序列化之后需要Encoding Encoding的方式有三种 binary, Text,MTOM,文件流比较大建议用MTOM