编程语言
首页 > 编程语言> > c#-如何在Soap客户端对ASP.Net Web服务的调用之间保留会话

c#-如何在Soap客户端对ASP.Net Web服务的调用之间保留会话

作者:互联网

这是我的系统设置方式:

>使用ASP.Net Web服务的Web服务
> Web服务具有带有EnableSession = true的Web方法
>使用“服务引用”引用Web服务的客户端(注意:不是“ Web引用”)
>客户端的app.config具有allowCookies = true

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

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方法的正确属性,客户端具有正确的配置,并且使用了相同的代理实例.我想念什么吗?

解决方法:

诀窍是,您需要使服务客户端知道它应该关心cookie-默认情况下不这样做.

也许更好的方法是让服务回传一个事务ID,以便将来客户可以使用该事务ID引用上传.从长远来看,它将变得更加干净,并且可能比使客户端使用Cookie以及因此使用会话的工作量少.

标签:asmx,web-services,wcf,asp-net,c
来源: https://codeday.me/bug/20191209/2096669.html