# WCF流模式(Streamed)传输文件之遇到文件被其他进程占用的问题

FunctionGHW posted @ 2014年11月02日 22:24 in C#与.Net with tags dispose WCF Streamed 文件传输 IDisposable , 4196 阅读

        <!-- Endpoint Binding Configuration -->
<basicHttpBinding>
<binding name="FileTransferHttpBinding"
transferMode="Streamed"
closeTimeout="00:00:10"
openTimeout="00:00:10"
sendTimeout="00:20:00">
<security mode="None"/>
</binding>
</basicHttpBinding>


    // 文件请求对象，下载文件时可以作为参数使用
[MessageContract]
public class FileRequestObject
{
public string FileName { get; set; }

// 省略其他属性
}

// 文件传输对象，Stream必须作为唯一主体
[MessageContract]
public class FileTransportObject
{
public string FileName { get; set; }

public long FileSize { get; set; }

// 省略其他属性

[MessageBodyMember]
public Stream FileStream { get; set; }
}


    [ServiceContract]
public interface IFileService
{
[OperationContract]

[OperationContract]
}


## 发现问题

        public FileTransportObject DownloadFile(FileRequestObject request)
{
// 这里不能使用using或者try-finally之类的代码去保证关闭fileStream，
// 因为该对象是要被返回给客户端的，一旦被文件流被关闭了，连接就断了。
// 这也就导致了问题的出现。
......
long fileSize = fileStream.Length;
FileTransportObject fto = new FileTransportObject()
{
FileName = fileName,
FileSize = fileSize,
FileStream = fileStream
......
};
......
return fto;
}


## 解决方案

This property determines whether the service disposes all disposable parameters (input, output or reference parameters) that were created while processing a message. The default value is true. Set this property to false if you want to prevent the system from disposing of resources and cache them if required. For example, if AutoDisposeParameters is false, then the sender is responsible for closing the stream on the sending side.

If you wrap the Stream in MessageContract (so you could sent more information in headers), beware that the Stream would not be disposed automatically!

As the name of attribute OperationBehavior.AutoDisposeParameters suggests, WCF automatically disposes input/output parameters and thus you have to implement IDisposable on your MessageContract class and close the stream there

    // 实现IDisposable接口并在Dispose方法中释放文件流，
// 配合OperationBehaviorAttribute.AutoDisposeParameters属性，
// 保证WCF可以自动释放文件流
[MessageContract]
public class FileTransportObject : IDisposable
{
public string FileName { get; set; }

public long FileSize { get; set; }

// 省略其他属性

[MessageBodyMember]
public Stream FileStream { get; set; }

public void Dispose()
{
if (FileStream != null)
{
//Console.WriteLine("disposing: {0}", FileName);
FileStream.Dispose();
}
}
}


• 无匹配
jackjohnny 说:
2021年7月14日 17:29

ikinci el cikma enjektör bosch kullanilmis oto enjektör satin al kullanilmis enjektör

mega888 apk 说:
2021年8月30日 00:03

This is very significant, and yet necessary towards just click this unique backlink:

Satta king 说:
2021年9月25日 05:14

The most interesting text on this interesting topic that can be found on the net ...

Kerala +1 Model Pape 说:
2022年8月16日 22:09

The Keralan government offers top-notch instruction, top-notch facilities, and a clean learning atmosphere. As a result of the state board's extensive provision of educational facilities, there has been a yearly increase in education at all levels in the state. On the official website, the Kerala +1 Important Question Paper 2023 was posted. Kerala +1 Model Paper 2023 The Kerala +1 Important Question Paper 2023 will be released as soon as it is ready, and the Kerala 11th Model Question Paper of the exams was posted fairly quickly on the official website. By utilising their registration number and Subjects-wise, they may check their Kerala +1 Important Question Paper 2023.

NCERT Mathematics Qu 说:
2022年9月19日 01:42

Mathematics is one of the subjects which plays a key role in everyone’s life and it’s very important to each student. NCERT Mathematics Question Paper Class 2 Mathematics is not a certain time helping subject, it is along with the people for their whole life at any time and at anywhere. It should begin from the foundation of Education to enable 2nd students to understand mathematics easily. Downloading NCERT Mathematics Sample Paper 2023 Class 2 for all formats of exams conducted under Term-1, Term-2 and other types of exams has available for every candidate who wants to get a keen clarity on the question pattern of the exam paper along with revised important questions.

(输入验证码)
or Ctrl+Enter