Asp.net web服务共享全局变量

Asp.net web服务共享全局变量,asp.net,vb.net,web-services,Asp.net,Vb.net,Web Services,我不擅长在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.S

我不擅长在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....
它并没有给我那个值,只是一个错误。如果我提供服务,似乎没有任何价值。。建议列表中没有任何内容

如何将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"

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

' 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#中为静态)。
这意味着该类的每个实例都将共享同一个变量(当然还有它的值)


你的第一个例子似乎不起作用。在代码svc.avIP=“0.0.0.0”中,它告诉我‘avIP不是‘服务’的成员。第一个示例基于问题中定义的服务类的未修改版本。那个类有一个公共的avIP,所以它应该在服务类声明中。你的更新会让事情变得更糟。您不需要在服务类内部创建服务类的实例。您需要在服务类之外创建服务类的实例,并使用该实例调用avIP属性,或者将avIP的生存期更改为共享。看看这个
Client.Connect(svc.avIP, 60128)
' 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)
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)