代码之家  ›  专栏  ›  技术社区  ›  Joh

如何跨呼叫保留会话ASP.Net来自Soap客户机的web服务

  •  3
  • Joh  · 技术社区  · 14 年前

    我的系统是这样设置的:

    • 使用ASP.Netweb服务
    • web服务具有EnableSession=true的web方法
    • 使用“服务引用”引用web服务的客户机(注意:不是“web引用”)

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

    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
    

    [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方法属性,客户机拥有正确的配置,并且使用了相同的代理实例。我错过什么了吗?

    2 回复  |  直到 14 年前
        1
  •  3
  •   Wyatt Barnett    14 年前

    诀窍是您需要让服务客户机意识到它应该关心cookies——默认情况下它不会。

        2
  •  0
  •   Arifur Rahman Khan    6 年前

    私人System.Net.CookieContainer文件夹CK=新System.Net.CookieContainer文件夹();

    然后将其用作服务对象的CookieContainer。