WCF streaming from Sharepoint

本文关键字:Sharepoint from streaming WCF | 更新日期: 2023-09-27 18:03:49

我的自定义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对象不正确处置(网站和web)?从长远来看,这将是一个问题吗?有没有其他可行的方法?我不想使用Sharepoint客户端对象模型,因为我有一些自定义的身份验证代码需要在从Sharepoint下载文件时执行。

有什么想法或想法可以为我指出正确的方向吗?

更新:

我可能已经解决了这个问题,通过使用OperationCompleted事件在当前的OperationContext像这样:

OperationContext clientContext = OperationContext.Current;
clientContext.OperationCompleted += delegate
                                    {                                                                  
                                        if( stream != null )                                                                                                  
                                            stream.Dispose();
                                        site.Close();
                                        web.Close();
                                    };

也许我不需要处理流?有人看到上面的方法有什么问题吗?

WCF streaming from Sharepoint

只要您仍然有对SPSite和SPWeb的引用,那么您就可以处理它们。

只是一个小的,最佳实践是在SPSite和SPWeb对象上调用Dispose()而不是Close()。