我一直在尝试设计WCF文件上传服务,但收到以下错误 在我的网络应用程序中:
输入'System.Web.HttpInputStream' 无法序列化。考虑标记 它与DataContractAttribute 属性,并标记其所有 你想用序列化的成员 DataMemberAttribute属性。看到 Microsoft .NET Framework 其他支持的文档 类型。
基于此错误,我尝试重新标记我的FileTransfer 带有DataContractAttribute和DataMemberAttribute的类 但那没有做任何事情:
[DataContractAttribute(Namespace = "FileTransfer")]
public class FileTransfer
{
[DataMemberAttribute]
public string GetUploadStatus;
[DataMemberAttribute]
public Tuple<string, int> DoUpload;
[DataMemberAttribute]
public int UploadFile;
[DataMemberAttribute]
public FileTransferInfo FileInfo;
[DataMemberAttribute]
public Stream FileByteStream;
}
我尝试使用服务跟踪查看器访问我的服务跟踪日志 看看我是否可以获得有关此错误的更多详细信息。我发现了一些错误 以下消息:
带有To的消息 'http://localhost:1242/WebProj/filetransfer.svc/mex/mex' 不能在接收器处理, 由于AddressFilter不匹配 EndpointDispatcher。检查一下 发送者和接收者 EndpointAddresses同意。
这对我有用,但我也发现了同样的错误:
在相同的痕迹中,我能够正常验证没有任何错误,这让我想知道这个错误是否是我应该担心的事情(如果有任何机构有关于为什么有mex / mex的建议我的终点,那会很棒)。
那么,为什么'System.Web.HttpInputStream'不能被序列化?我在下面提供了我的代码的其他重要方面。也许有人可以看到我错过的东西?
[DataContract(Namespace = "FileTransfer")]
public class FileTransferInfo
{
private string _guid;
private int _flag;
private long _fileSize;
private string _fileName;
private DateTime _lastUpdate;
private FileTypeEnum _fileType;
//REMOVED GETTERS AND SETTERS FOR SPACE
}
[ServiceContract(Namespace = "FileTransfer")]
public interface IFileTransferService
{
[OperationContract(Name = "DoUpload")]
Tuple<string, int> DoUpload(List<FileTransferInfo> request);
[OperationContract(Action="UploadFile", Name="UploadFile")]
int UploadFile(FileTransfer request);
}
这是我的UploadFile方法,它返回错误。
int IFileTransferService.UploadFile(FileTransfer request)
{
string uploadFolder = @"C:\TempMultiFileUploads\";
int errCode = default(int);
// parameters validation omitted for clarity
try
{
string filename = request.FileInfo.FileName;
string filePath = Path.Combine(uploadFolder, filename);
using (FileStream outfile = new FileStream(filePath, FileMode.Create))
{
const int bufferSize = 65536; // 64K
Byte[] buffer = new Byte[bufferSize];
int bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
outfile.Write(buffer, 0, bytesRead);
bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize);
}
}
}
catch (IOException e)
{
//System.IOException
errCode = 800;
}
return errCode;
}
And, below is the endpoint, binding, and bahavior of my FileTransferService:
<endpoint name="MyFileTransferEP"
address=""
binding="basicHttpBinding"
behaviorConfiguration="BasicHttpEPBehavior"
bindingConfiguration="httpLargeDataStream"
contract="FileTransfer.IFileTransferService" />
<binding name="httpLargeDataStream"
closeTimeout="00:01:00"
openTimeout="00:01:00"
receiveTimeout="00:10:00"
sendTimeout="00:01:00"
maxBufferSize="65536"
maxReceivedMessageSize="2147483647"
messageEncoding="Mtom"
transferMode="StreamedRequest">
<behavior name="BasicHttpEPBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483646" />
</behavior>
以下是我的Web应用程序中调用上传方法的代码:
FileTransferServiceClient upload = new FileTransferServiceClient();
HttpPostedFile m_objFile = default(HttpPostedFile);
FileTransfer transmit = new FileTransfer();
transmit.FileByteStream = m_objFile.InputStream;
transmit.FileInfo = new FileTransferInfo();
transmit.FileInfo.Guid = Guid.NewGuid().ToString();
transmit.FileInfo.Flag = default(int);
transmit.FileInfo.FileSize = m_objFile.ContentLength;
transmit.FileInfo.FileName = m_objFile.FileName;
transmit.FileInfo.LastUpdate = DateTime.Now;
int retParam = upload.UploadFile(transmit); // THROWS ERROR HERE
感谢您的帮助。
答案 0 :(得分:2)
您无法使用Stream成员定义数据协定。您可以将Stream作为单个参数获取或返回,也可以定义具有Stream类型的单个MessageBodyMember但可以具有多个MessageHeader的消息协定。有关WCF中大数据流的更多信息,请参阅this section of MSDN。