来自 Sharepoint 的 WCF 流式传输
我的自定义 WCF 服务有一种从 Sharepoint 站点下载文件的方法。目标是调用 DownloadFile 然后接收流。
[OperationContract]
Stream DownloadFile( string uri );
从 Sharepoint 获取文件并返回流的代码是:
public Stream DownloadFile( string uri )
{
// NOTE! we cannot use a using statement as the stream will get closed.
var site = new SPSite( uri );
var web = site.OpenWeb();
var file = web.GetFile( uri );
// some custom authentication code...
// NOTE! do not close stream as we are streaming it.
return file.OpenBinaryStream();
}
我猜当流传输完成时,进行流传输的流将自动正确关闭并由 WCF 服务处理?
但是,我应该如何解决未正确处理的共享点对象(站点和网络)的问题?从长远来看这会成为问题吗?还有其他方法可用吗?我不想使用 Sharepoint 客户端对象模型,因为我有一些自定义身份验证代码,需要在从 Sharepoint 下载文件时执行。
有什么想法或想法可以为我指明正确的方向吗?
更新:
我可能已经通过在当前的OperationContext上使用OperationCompleted事件来解决这个问题,如下所示:
OperationContext clientContext = OperationContext.Current;
clientContext.OperationCompleted += delegate
{
if( stream != null )
stream.Dispose();
site.Close();
web.Close();
};
也许我不需要处置流?有人发现上述方法有问题吗?
My custom WCF service has a method for downloading a file from a Sharepoint site. The goal is to call DownloadFile and then receive a stream.
[OperationContract]
Stream DownloadFile( string uri );
The code for fetching the file from Sharepoint and return the Stream is:
public Stream DownloadFile( string uri )
{
// NOTE! we cannot use a using statement as the stream will get closed.
var site = new SPSite( uri );
var web = site.OpenWeb();
var file = web.GetFile( uri );
// some custom authentication code...
// NOTE! do not close stream as we are streaming it.
return file.OpenBinaryStream();
}
I guess the stream that gets streamed will automatically get properly closed and disposed by the WCF service as the streaming is complete?
But, how am I supposed to solve the problem with my sharepoint objects that are not disposed properly (site and web)? Will this be a problem in the long run? Is there any other approach available? I do not want to use the Sharepoint Client Object Model as I have some custom authentication code that needs to execute when downloading the file from Sharepoint.
Any thoughts or ideas that could point me in right direction?
UPDATE:
I might have resolved this by using the OperationCompleted event on current OperationContext like this:
OperationContext clientContext = OperationContext.Current;
clientContext.OperationCompleted += delegate
{
if( stream != null )
stream.Dispose();
site.Close();
web.Close();
};
Maybe I don't need to dispose the stream? Does anyone see something faulty with the above approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只要您仍然有对 SPSite 和 SPWeb 的引用,上面的内容就应该没问题,然后您就可以处理它们。
只是一个小问题,最佳实践是在 SPSite 和 SPWeb 对象上调用 Dispose() 而不是 Close()。
The above should be fine as long as you still have a reference to the SPSite and SPWeb then you can dispose of them.
Just a small one, best practice is to called Dispose() instead of Close() on the SPSite and SPWeb objects.