Web方法执行后Web服务类字段为空?

发布于 2024-10-11 14:30:30 字数 1163 浏览 1 评论 0原文

我的解决方案中有一个 Web 服务项目和一个 WPF 项目(充当客户端)。

表示 Web 服务的类非常简单,如下所示:

public class CardsGameService : System.Web.Services.WebService
{
    private Card magicCard;

    // Acts as a getter for magicCard field
    [WebMethod]
    public Card GetMagicCard()
    {
        return magicCard;
    }

    // Creates some random cards and sets the magicCard field
    [WebMethod]
    public Card[] GetNewCardSet()
    {
        Card[] cardSet = new Card[4];

        for (int i = 0; i < 4; i++)
        {
            cardSet[i] = GetRandomCard();
        }

        Random random = new Random();
        int index = random.Next(0, 3);

        // This field does get its value and 
        // is not null when this method is executed
        magicCard = cardSet[index];

        return cardSet;
    }
}

在我的客户端 WPF 代码隐藏类中,我实例化 CardsGameServiceSoapClient 服务 并调用 GetNewCardSet() Web 方法返回卡片组。

问题是这样的:
如果我稍后想调用 Web 方法 GetMagicCard(),它总是返回 null为什么?我仅实例化 CardsGameServiceSoapClient 服务一次,并且只要我运行客户端应用程序,该对象就处于活动状态

I have a Web Service project and a WPF project (which serves as a client) in my solution.

The class representing the web service is quite simple and looks like this:

public class CardsGameService : System.Web.Services.WebService
{
    private Card magicCard;

    // Acts as a getter for magicCard field
    [WebMethod]
    public Card GetMagicCard()
    {
        return magicCard;
    }

    // Creates some random cards and sets the magicCard field
    [WebMethod]
    public Card[] GetNewCardSet()
    {
        Card[] cardSet = new Card[4];

        for (int i = 0; i < 4; i++)
        {
            cardSet[i] = GetRandomCard();
        }

        Random random = new Random();
        int index = random.Next(0, 3);

        // This field does get its value and 
        // is not null when this method is executed
        magicCard = cardSet[index];

        return cardSet;
    }
}

In my client WPF code-behind class, I am instantiating CardsGameServiceSoapClient service and calling the GetNewCardSet() web method which returns the set of cards.

Here's the problem:
If I want to call the web method GetMagicCard() later on, it always returns null. Why? I am instantiating CardsGameServiceSoapClient service only once and this object is alive for as long as I run my client application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

或十年 2024-10-18 14:30:30

因为 WebService 实例是根据每次调用创建和丢弃的。如果您想在多个 Web 服务调用之间共享状态,请使用静态字段。但请注意,您需要担心与处理任何其他多线程代码时相同的问题。两个调用可能同时进行,因此您需要在适当的情况下使用锁。

另外值得一提的是,IIS 可以并且将会随时出于任何原因终止您的应用程序域,因此您不应该依赖该域将无限期地挂起这一事实。如果您需要持久存储,请使用数据库或其他持久存储。

Because the WebService instance is created and discarded on a per-call basis. If you want to share state between multiple web service calls, then use a static field. But beware that you need to worry about the same kind of issues you would when dealing with any other multi-threaded code. Two calls could be in progress at the same time so you need to use locks where appropriate.

Also worth mentioning is that IIS can and will terminate your application domain for any reason at any time so you shouldn't rely on the fact that the field will hang around indefinitely. If you need it to persist then use a database or other persistent storage.

ゝ杯具 2024-10-18 14:30:30

问题是,虽然您的代理客户端对象(CardsGameServiceSoapClient)在两次调用之间处于活动状态,但对服务的每次调用都是一个全新的请求,并且服务器对象在每个请求上实例化......您需要创建和初始化每个 Web 方法的对象(也许在构造函数中初始化它?)

The problem is that, while your proxy client object (CardsGameServiceSoapClient) is alive between the two calls, each call to your service is a brand new request and the server object is instantiated on each request.... you would need to create and initialize your object for each web method (perhaps init it in a constructor?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文