2010-09-14 155 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