views:

35

answers:

1

Here is how my system is set up:

  • A web service using ASP.Net web service
  • The web service has a web method with EnableSession=true
  • A client which refers to the web service using "Service References" (note: not "Web References")
  • The app.config of the client has 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.

[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?

+2  A: 

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

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.

Wyatt Barnett
+1 this it it .
eglasius