类型“System.Web.HttpInputStream"无法序列化 [英] Type 'System.Web.HttpInputStream' cannot be serialized

查看:26
本文介绍了类型“System.Web.HttpInputStream"无法序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试设计 WCF 文件上传服务,但出现以下错误在我的网络应用程序中:

I have been trying to design a WCF file upload service and am getting the following error in my web application:

输入System.Web.HttpInputStream"无法序列化.考虑标记它与 DataContractAttribute属性,并标记其所有你想要序列化的成员DataMemberAttribute 属性.看微软 .NET 框架其他支持的文档类型.

Type 'System.Web.HttpInputStream' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. See the Microsoft .NET Framework documentation for other supported types.

基于此错误,我尝试重新标记我的 FileTransfer具有 DataContractAttribute 和 DataMemberAttribute 的类但这没有做任何事情:

Based on this error, I have tried re-marking my FileTransfer class with DataContractAttribute and DataMemberAttribute but that didn't do anything:

[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;
}

我尝试使用服务跟踪查看器访问我的服务跟踪日志以看看我是否可以获得有关此错误的更多详细信息.我发现了一些错误带有以下消息:

I have tried accessing my Service Trace Log with Service Trace Viewer to see if I could get some more detail on this error. I found a number of errors with the following message:

带有 To 的消息'http://localhost:1242/WebProj/filetransfer.svc/mex/mex'无法在接收方处理,由于地址过滤器不匹配EndpointDispatcher.检查一下发送者和接收者的EndpointAddresses 同意.

The message with To 'http://localhost:1242/WebProj/filetransfer.svc/mex/mex' cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher. Check that the sender and receiver's EndpointAddresses agree.

这对我有用,但我也发现了同样的错误:

Which would have been useful to me but I also found the same error for:

'http://localhost:1242/WebProj/auth.svc/mex/墨西哥'

在同一个跟踪中,我能够很好地进行身份验证而没有任何错误,这让我想知道这个错误是否是我应该担心的事情(如果任何机构对为什么有 mex/mex 在我的终点,那会很棒).

in the same trace and I was able to authenticate just fine without any errors which made me wonder if this error is something that I should be worrying about (if any body has a suggestion as to why there is a mex/mex at my endpoint, that would be great).

那么,为什么System.Web.HttpInputStream"不能被序列化?我在下面提供了代码的其他重要方面.也许外面的人可以看到我错过的东西?

So, why can't 'System.Web.HttpInputStream' be serialized? I have provided the other important aspects of my code below. Maybe somebody out there can see something that I have missed?

[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 方法,它返回错误.

Here is my UploadFile method that is returning the error.

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>  

这是我的网络应用程序中调用上传方法的代码:

Here is the code in my web application that calls the upload method:

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

感谢您的帮助.

推荐答案

您不能使用 Stream 成员定义数据协定.您可以采用或返回 Stream 作为单个参数,也可以定义一个消息协定,该协定具有一个类型为 Stream 的 MessageBodyMember,但可以具有多个 MessageHeaders.有关 WCF 中大数据流的更多信息,请参阅 MSDN 的这一部分.

You cannot define a data contract with a Stream member. You can either take or return a Stream as single parameter or define a message contract which has a single MessageBodyMember of type Stream, but can have multiple MessageHeaders. For more on large data streaming in WCF, please refer to this section of MSDN.

这篇关于类型“System.Web.HttpInputStream"无法序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆