【问题标题】:web service sharing global variableWeb 服务共享全局变量
【发布时间】:2012-10-06 21:30:27
【问题描述】:

我是在 ASP.net/VB.net 中创建 Web 服务的新手。我在 App_Code 文件夹中的 Service.vb 中设置了一个公共变量:

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Diagnostics
Imports System.Web.Script.Services

<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class Service
    Inherits System.Web.Services.WebService

    Public avIP As String = "0.0.0.0"
etc etc....

现在我又创建了一个Class,我想获得avIP的价值。但是,当我这样做时:

Client.Connect(Service.avIP, 60128)

它并没有给我这个值,只是一个错误。如果我做 Service.,我似乎没有任何价值。建议列表中没有任何内容。

如何将 Service.vb 中的值获取到我的其他类中?

更新

我在 Service.vb 文件中有以下内容:

<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class Service
    Inherits System.Web.Services.WebService
    Public svc As Service = New Service
    Dim avIp As String = "0.0.0.0"

在 avReceiver.vb 我有:

Client.Connect(svc.avIP, 60128)

【问题讨论】:

    标签: asp.net vb.net web-services


    【解决方案1】:

    要引用在像您这样的类中声明的公共变量,您需要创建该类的实例。
    这意味着每个创建的类实例都有其变量的副本。
    这是每一种面向对象语言的一个非常基本的功能。

    ' Create a new instance of the Service class
    Dim svc As Service = new Service()
    ' Set the value of a Public Property 
    svc.avIP = "192.168.1.0"
    ' Use the instance value of that property
    Client.Connect(svc.avIP, 60129)
    
    ' Create another instance of the Service class
    Dim svc1 As Service = new Service()
    ' Set the value of a Public Property 
    svc1.avIP = "192.168.1.1"
    ' Use the instance value of that property
    Client.Connect(svc1.avIP, 60129)
    

    如果您想使用类的属性成员而不声明该类的实例,您需要将该成员声明为Shared(C# 中为静态)。
    这意味着该类的每个实例都将共享同一个变量(当然还有它的值)。

    Public Class Service   
         Inherits System.Web.Services.WebService   
    
         Public Shared avIP As String = "0.0.0.0"   
         ....
    End Class
    
    ' Set the one and only avIP for every instance
    Service.avIP = "192.168.1.0"
    ' Use directly the value
    Client.Connect(Service.avIP, 60129)
    
    ' Create an instance of the Service class
    Dim svc As Service = new Service()
    ' Pass the same value used directly with the class name (192.168.1.0)
    Client.Connect(svc.avIP, 60129)
    

    【讨论】:

    • 您的第一个示例似乎不起作用。在代码 svc.avIP = "0.0.0.0" 上它告诉我 'avIP 不是'Service'的成员
    • 第一个示例基于您问题中定义的服务类的未修改版本。该类有一个Public avIP,所以它应该在Service类声明中。
    • 您的更新让事情变得更糟。您不需要在 Service 类中创建 Service 类的实例。您需要在 Service 类之外创建 Service 类的实例并使用该实例调用 avIP 属性,或者将 avIP 的生命周期更改为 Shared。看这个question and relative answers
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    • 2019-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多