2011-07-29 96 views
3

我的自定義WCF服務有一個用於從Sharepoint網站下載文件的方法。目標是調用DownloadFile然後接收流。從Sharepoint WCF流式傳輸

[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客戶端對象模型,因爲我在從Sharepoint下載文件時需要執行一些自定義驗證代碼。

任何想法或想法可能指向正確的方向?

UPDATE:

我可能會通過使用當前的OperationContext這樣的OperationCompleted事件已經解決了這個:

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

也許我並不需要處置流?有沒有人用上述方法看到有問題?

+0

你的問題幫了我很多。與代表清理好主意。謝謝! – kenchilada

回答

0

只要你仍然有一個SPSite和SPWeb的參考,那麼你可以處理它們,上面應該沒問題。

只是一個小例子,最佳實踐是在SPSite和SPWeb對象上調用Dispose()而不是Close()。