难道这这种方式工作? [英] Is this supposed to work this way?

查看:164
本文介绍了难道这这种方式工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的服务器上的此code高达这里(是的,我知是ASMX一个坏主意,但WCF根本不工作,出于某种原因):

 <%@ WebService的语言=C#类=测试%GT;使用的System.Web;
使用System.Web.Services;[WebService的空间(namespace =htt​​p://smplsite.com/smplAccess)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)
公共类测试:System.Web.Services.WebService
{
    状态;    公开测试()
    {
        S =(会话[富] ??(会话[富] =新的国家()))的国家;
    }    [的WebMethod(EnableSession =真)]
    公共无效设置(诠释J){I = j的; }    [的WebMethod(EnableSession =真)]
    公众诠释的get(){回报我; }
}级别状态
{
    公众诠释我= 5;
}

当我运行folloing code:

 类节目
{
    静态无效的主要(字串[] args)
    {
        VAR SER =新ServiceReference1.TestSoapClient();        Console.WriteLine(ser.Get());
        ser.Set(3);
        Console.WriteLine(ser.Get());
    }
}

我希望得到的:

  5
3

但我回来

  5

我的解决方案


  1. USEE Wsdl.exe用来生成一个代理类

  2. 为需要得到它来编译添加引用

  3. 使用<一个href=\"http://stackoverflow.com/questions/808168/is-this-supposed-to-work-this-way/808219#808219\">Martin's解决方案


<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.services.webmethodattribute.enablesession.aspx\"相对=nofollow>这似乎与

编辑:新增State对象


解决方案

Web服务是无状态的,所以他们不存储多个电话之间的状态。每次你调用一个方法,将要创建的服务的一个新实例,其成员将再次有默认值。

你能做什么,是为了使会话状态(如您已完成),并存储在ASP.NET会话的状态。

事情是这样的:

  [的WebMethod(EnableSession =真)]
公共无效集(诠释J){会话[我] = j的; }[的WebMethod(EnableSession =真)]
公众诠释的get(){返回会话[我] == NULL? 5:(INT)会议[我]; }

这是什么是需要在服务器端。但你也不得不采取对客户端的护理:

由于一个ASP.NET会话由一个cookie识别,你必须确保你正在过相同的cookie到服务器与每个Web方法调用。要做到这一点,你必须实例化的CookieContainer,并将其分配给Web服务代理实例:

 静态无效的主要(字串[] args)
{
    VAR SER =新ServiceReference1.TestSoapClient();
    ser.CookieContainer =新System.Net.CookieContainer();
    // ...
}

I have this code up on my server here (Yes I known ASMX is a bad idea but WCF doesn't work at all for some reason):

<%@ WebService Language="C#" Class="Test" %>

using System.Web;
using System.Web.Services;

[WebService(Namespace = "http://smplsite.com/smplAccess")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Test : System.Web.Services.WebService
{
    State s;

    public Test()
    {
        s = (Session["foo"] ?? (Session["foo"] = new State())) as State ;
    }

    [WebMethod(EnableSession = true)]
    public void Set(int j) { i=j; }

    [WebMethod(EnableSession = true)]
    public int Get() { return i; }
}

class State
{
    public int i = 5;
}

when I run the folloing code:

class Program
{
    static void Main(string[] args)
    {
        var ser = new ServiceReference1.TestSoapClient();

        Console.WriteLine(ser.Get());
        ser.Set(3);
        Console.WriteLine(ser.Get());
    }
}

I expect to get back:

5
3

but I got back

5
5

My Solution

  1. Usee wsdl.exe to generate a proxy class
  2. Add references as needed to get it to compile
  3. Use Martin's solution


This Seems related

Edit: Added State object.

解决方案

Web services are stateless, so they do not store their state between multiple calls. Everytime you call a method, a new instance of the service will be created and its members will have the default values again.

What you can do, is to enable session state (as you have done) and store your state in the ASP.NET session.

Something like this:

[WebMethod(EnableSession = true)]
public void Set(int j) { Session["i"] = j; }

[WebMethod(EnableSession = true)]
public int Get() { return Session["i"] == null ? 5 : (int)Session["i"]; }

This was what is required on the server side. But you also have to take care on the client side:

Since an ASP.NET session is identified by a cookie, you have to make sure that you are passing the same cookie to the server with every web method call. To do so, you have to instantiate a CookieContainer and assign it to the web service proxy instance:

static void Main(string[] args)
{
    var ser = new ServiceReference1.TestSoapClient();
    ser.CookieContainer = new System.Net.CookieContainer();
    // ...
}

这篇关于难道这这种方式工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆