如何在调用ASP时保持会话。来自Soap客户机的Net web服务

时间:2022-11-18 09:52:14

Here is how my system is set up:

我的系统是这样建立的:

  • A web service using ASP.Net web service
  • 使用ASP的web服务。净web服务
  • The web service has a web method with EnableSession=true
  • web服务有一个web方法,它的EnableSession=true
  • A client which refers to the web service using "Service References" (note: not "Web References")
  • 使用“服务引用”引用web服务的客户机(注意:不是“web引用”)
  • The app.config of the client has allowCookies=true
  • 客户机的app.config具有allowCookies=true

On the client side, I have the following code to upload a file to the service

在客户端,我有以下代码将文件上载到服务

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

On the server side, I have code that checks if the session id from the call to InitiateUpload is the same as AppendUpload.

在服务器端,我有代码检查从InitiateUpload调用的会话id是否与app136加载相同。

[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;
  }
}

The call to AppendUpload returns false, because of the mismatching session ids. Why is that? As far as I can see, I have the right attributes for the web method, the client has the correct config, and the same instance of the proxy is used. Am I missing something?

由于会话id不匹配,对app136加载的调用返回false。这是为什么呢?据我所知,我有正确的web方法属性,客户端有正确的配置,并且使用代理的相同实例。我遗漏了什么东西?

1 个解决方案

#1


3  

The trick is you need to make the service client aware it should care about cookies -- it doesn't by default.

诀窍在于,您需要让服务客户端意识到它应该关心cookie——它在默认情况下并不关心cookie。

Perhaps a better method would be for the service to pass back a transaction ID the client could use to reference the upload in the future. It would be cleaner in the long run and probably be less work than making the client work with cookies and therefore sessions.

也许一个更好的方法是为服务传递一个事务ID,客户端可以使用它来引用将来的上传。从长远来看,这比让客户端使用cookie和会话所做的工作要少得多。

#1


3  

The trick is you need to make the service client aware it should care about cookies -- it doesn't by default.

诀窍在于,您需要让服务客户端意识到它应该关心cookie——它在默认情况下并不关心cookie。

Perhaps a better method would be for the service to pass back a transaction ID the client could use to reference the upload in the future. It would be cleaner in the long run and probably be less work than making the client work with cookies and therefore sessions.

也许一个更好的方法是为服务传递一个事务ID,客户端可以使用它来引用将来的上传。从长远来看,这比让客户端使用cookie和会话所做的工作要少得多。