2010-09-14 160 views
3

這裏保留到以後的呼叫會話一個ASP.Net Web服務是如何在我的系統設置:如何從SOAP客戶端

  • 使用ASP.Net Web服務
  • 的Web服務的Web服務有EnableSession一個Web方法=真正的
  • 它指的是Web服務使用「服務引用」(注意:不是「Web引用」)客戶
  • 客戶端的app.config中有allowCookies =真

在客戶端,我有以下的代碼到一個文件上傳到服務

bool res = service.InitiateUpload(); 
if (res) { 
    do { 
     read = stream.Read(buffer, 0, BLOCK_SIZE); 
     if (read == BLOCK_SIZE) 
      res = res && service.AppendUpload(buffer); 
     else if (read > 0) 
      // other call to AppendUpload, after buffer manipulation 

在服務器端,我來檢查,如果從調用InitiateUpload會話ID是一樣的AppendUpload代碼。

[WebMethod(EnableSession=true)] 
public bool InitiateUpload() { 
    lock (theLock) { 
    if (IsImportGoingOn) 
     return false; 

    theImportDataState = new ImportDataState(Session.SessionID); 
    } 
    return true; 
} 

[WebMethod(EnableSession=true)] 
public bool AppendUpload(byte[] data) { 
    lock (theLock) { 
    if (!IsImportGoingOn) 
     return false; 

    if (theImportDataState.session != Session.SessionID) 
     return false; 

    theImportDataState.buffer.AddRange(data); 
    return true; 
    } 
} 

由於會話ID不匹配,對AppendUpload的調用返回false。這是爲什麼? 就我所見,我擁有Web方法的正確屬性,客戶端具有正確的配置,並且使用了同一個代理實例。我錯過了什麼嗎?

回答

3

訣竅是你需要讓服務客戶端意識到它應該關心cookie - 它不會默認。

也許更好的方法是服務返回一個事務ID,客戶可以使用它來引用未來的上傳。從長遠來看,它會更乾淨,並且可能比讓客戶端使用cookie和因此會話更少。

+0

+1 this it it it。 – eglasius 2010-09-14 22:07:00