2012-11-28 26 views
4

我對WCF服務相當陌生,希望對此有所幫助。我試圖運行WCF作爲一項服務,並讓另一臺機器上的ASP.net客戶端能夠通過連接到WCF服務將文件上傳到它。UploadFile:此操作在WCF測試客戶端不支持,因爲它使用類型ClientFileInfo

我正在測試它與一個簡單的上傳設置(從here),它工作正常,如果我只是引用WCF服務作爲「DLL」,但如果我嘗試運行在它作爲WCF服務,它給了我一個錯誤爲「UploadFile」方法指出它不受支持。

在方法名稱上帶有紅色X的確切消息:此操作在WCF Test Client中不受支持因爲它使用了FileUploadMessage類型。

我開始通過創建在Visual Studio 2012 WCF服務的應用程序,並在我的界面如下(IUploadService.cs):

[ServiceContract] 
public interface IUploadService 
{ 
    [OperationContract(IsOneWay = true)] 
    void UploadFile(FileUploadMessage request); 
} 

[MessageContract] 
public class FileUploadMessage 
{ 
    [MessageBodyMember(Order = 1)] 
    public Stream FileByteStream; 
} 

其實現像這樣(UploadService.svc.cs):

public void UploadFile(FileUploadMessage request) 
{ 
    Stream fileStream = null; 
    Stream outputStream = null; 

    try 
    { 
     fileStream = request.FileByteStream; 

     string rootPath = ConfigurationManager.AppSettings["RootPath"].ToString(); 

     DirectoryInfo dirInfo = new DirectoryInfo(rootPath); 
     if (!dirInfo.Exists) 
     { 
      dirInfo.Create(); 
     } 

     // Create the file in the filesystem - change the extension if you wish, 
     // or use a passed in value from metadata ideally 

     string newFileName = Path.Combine(rootPath, Guid.NewGuid() + ".jpg"); 

     outputStream = new FileInfo(newFileName).OpenWrite(); 
     const int bufferSize = 1024; 
     byte[] buffer = new byte[bufferSize]; 

     int bytesRead = fileStream.Read(buffer, 0, bufferSize); 

     while (bytesRead > 0) 
     { 
      outputStream.Write(buffer, 0, bufferSize); 
      bytesRead = fileStream.Read(buffer, 0, bufferSize); 
     } 
    } 
    catch (IOException ex) 
    { 
      throw new FaultException<IOException>(ex, new FaultReason(ex.Message)); 
    } 
    finally 
    { 
     if (fileStream != null) 
     { 
      fileStream.Close(); 
     } 
     if (outputStream != null) 
     { 
      outputStream.Close(); 
     } 
    } 
} // end UploadFile 

從它的外觀應該工作,但從我看到幾個stackoverflow和其他論壇問題了解,似乎WCF不支持流,即使我們可以有一個類型流的綁定。我對此感到困惑,以及我做錯了什麼。

謝謝你的幫助。

回答

1

與亞當在他的blog溝通後,我從中得到了代碼,我意識到我正在測試這個錯誤的方式。一切正常實施,但使用WCF測試客戶端搞砸了事情。只需啓動項目並將其添加爲「Service.svc」的Web引用即可完美運行。

+0

感謝@M汗我自己讀了你的答案之後就發現了這個錯誤:) –

+0

這是否意味着我無法使用WCF測試客戶端測試我的上傳方法,因爲Stream成員? – user3818229

+0

當時我在處理這個問題時,無法使用WCF測試客戶端進行測試。我不確定它是否已經更新。如果不能使用測試客戶端進行測試,那麼另一個顯而易見的方法就是使用測試客戶端並查看它是否有效。 –

相關問題