none
web service 属性 返回值 RRS feed

  • 问题

  • 建了一个web service,里面有个属性返回一个值。在windows application这里每次调用一个web service 的function之后就去取属性值。

    原来一直很正常,突然就不能用了,跟进去发现get属性的时候返回的字符串变成了nothing,又仔细跟了一下,发现在web service这边New()每次都被调用了。

     

    代码如下:

    web service

    Code Snippet

     

    Imports System.Web.Services

    Public Class Service1
        Inherits System.Web.Services.WebService
        Public Class Service1
        Inherits System.Web.Services.WebService
        Public sMessage As String

    #Region " Web Services Designer Generated Code "

        Public Sub New()
            MyBase.New()

            'This call is required by the Web Services Designer.
            InitializeComponent()

            'Add your own initialization code after the InitializeComponent() call

        End Sub

        'Required by the Web Services Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Web Services Designer
        'It can be modified using the Web Services Designer. 
        'Do not modify it using the code editor.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            components = New System.ComponentModel.Container()
        End Sub

        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            'CODEGEN: This procedure is required by the Web Services Designer
            'Do not modify it using the code editor.
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

    #End Region

     

     

        <WebMethod()> _
        Public Function HelloWorld() As String
            sMessage = "123"
            Return "Hello World"
        End Function

     

        <WebMethod()> _
        Public Function Calculator(ByVal pdX As Double, ByVal pdY As Double) As Double
            sMessage = "new"
            Return pdX + pdY
        End Function

     

        <WebMethod()> _
            Public Function Bool(ByVal plX As Boolean, ByVal plY As Boolean) As Boolean
            Return plX And plY
        End Function

     

        Public Property _Property() As String
            <WebMethod()> Get
                Return sMessage
            End Get
            <WebMethod()> Set(ByVal Value As String)
                sMessage = Value
            End Set
        End Property

      
      End Class

     

     

    Code Snippet

    Public Class Form1
        Inherits System.Windows.Forms.Form

    #Region " Windows Form Designer generated code "

        Public Sub New()
            MyBase.New()

            'This call is required by the Windows Form Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call

        End Sub

        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer. 
        'Do not modify it using the code editor.
        Friend WithEvents txtX As System.Windows.Forms.TextBox
        Friend WithEvents txtY As System.Windows.Forms.TextBox
        Friend WithEvents txtRslt As System.Windows.Forms.TextBox
        Friend WithEvents Label1 As System.Windows.Forms.Label
        Friend WithEvents Label2 As System.Windows.Forms.Label
        Friend WithEvents txtReturn As System.Windows.Forms.TextBox
        Friend WithEvents chkX As System.Windows.Forms.CheckBox
        Friend WithEvents chkY As System.Windows.Forms.CheckBox
        Friend WithEvents chkRslt As System.Windows.Forms.CheckBox
        Friend WithEvents btnCal As System.Windows.Forms.Button
        Friend WithEvents btnGet As System.Windows.Forms.Button
        Private WithEvents btnAnd As System.Windows.Forms.Button
        Friend WithEvents txtMessage As System.Windows.Forms.TextBox
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.btnCal = New System.Windows.Forms.Button
            Me.btnGet = New System.Windows.Forms.Button
            Me.btnAnd = New System.Windows.Forms.Button
            Me.txtX = New System.Windows.Forms.TextBox
            Me.txtY = New System.Windows.Forms.TextBox
            Me.txtRslt = New System.Windows.Forms.TextBox
            Me.Label1 = New System.Windows.Forms.Label
            Me.Label2 = New System.Windows.Forms.Label
            Me.txtReturn = New System.Windows.Forms.TextBox
            Me.chkX = New System.Windows.Forms.CheckBox
            Me.chkY = New System.Windows.Forms.CheckBox
            Me.chkRslt = New System.Windows.Forms.CheckBox
            Me.txtMessage = New System.Windows.Forms.TextBox
            Me.SuspendLayout()
            '
            'btnCal
            '
            Me.btnCal.Location = New System.Drawing.Point(328, 40)
            Me.btnCal.Name = "btnCal"
            Me.btnCal.Size = New System.Drawing.Size(56, 24)
            Me.btnCal.TabIndex = 0
            Me.btnCal.Text = "sum"
            '
            'btnGet
            '
            Me.btnGet.Location = New System.Drawing.Point(272, 96)
            Me.btnGet.Name = "btnGet"
            Me.btnGet.Size = New System.Drawing.Size(56, 24)
            Me.btnGet.TabIndex = 1
            Me.btnGet.Text = "get"
            '
            'btnAnd
            '
            Me.btnAnd.Location = New System.Drawing.Point(136, 160)
            Me.btnAnd.Name = "btnAnd"
            Me.btnAnd.Size = New System.Drawing.Size(56, 24)
            Me.btnAnd.TabIndex = 2
            Me.btnAnd.Text = "And"
            '
            'txtX
            '
            Me.txtX.Location = New System.Drawing.Point(24, 32)
            Me.txtX.Name = "txtX"
            Me.txtX.Size = New System.Drawing.Size(72, 20)
            Me.txtX.TabIndex = 3
            Me.txtX.Text = "0"
            '
            'txtY
            '
            Me.txtY.Location = New System.Drawing.Point(120, 32)
            Me.txtY.Name = "txtY"
            Me.txtY.Size = New System.Drawing.Size(80, 20)
            Me.txtY.TabIndex = 3
            Me.txtY.Text = "0"
            '
            'txtRslt
            '
            Me.txtRslt.Location = New System.Drawing.Point(232, 40)
            Me.txtRslt.Name = "txtRslt"
            Me.txtRslt.Size = New System.Drawing.Size(80, 20)
            Me.txtRslt.TabIndex = 3
            Me.txtRslt.Text = "0"
            '
            'Label1
            '
            Me.Label1.Location = New System.Drawing.Point(96, 40)
            Me.Label1.Name = "Label1"
            Me.Label1.Size = New System.Drawing.Size(24, 16)
            Me.Label1.TabIndex = 4
            Me.Label1.Text = "+"
            '
            'Label2
            '
            Me.Label2.Location = New System.Drawing.Point(200, 40)
            Me.Label2.Name = "Label2"
            Me.Label2.Size = New System.Drawing.Size(24, 16)
            Me.Label2.TabIndex = 5
            Me.Label2.Text = "="
            '
            'txtReturn
            '
            Me.txtReturn.Location = New System.Drawing.Point(16, 96)
            Me.txtReturn.Name = "txtReturn"
            Me.txtReturn.Size = New System.Drawing.Size(192, 20)
            Me.txtReturn.TabIndex = 6
            Me.txtReturn.Text = ""
            '
            'chkX
            '
            Me.chkX.Location = New System.Drawing.Point(24, 160)
            Me.chkX.Name = "chkX"
            Me.chkX.Size = New System.Drawing.Size(32, 16)
            Me.chkX.TabIndex = 7
            Me.chkX.Text = "X"
            '
            'chkY
            '
            Me.chkY.Location = New System.Drawing.Point(88, 160)
            Me.chkY.Name = "chkY"
            Me.chkY.Size = New System.Drawing.Size(32, 24)
            Me.chkY.TabIndex = 8
            Me.chkY.Text = "Y"
            '
            'chkRslt
            '
            Me.chkRslt.Location = New System.Drawing.Point(224, 168)
            Me.chkRslt.Name = "chkRslt"
            Me.chkRslt.Size = New System.Drawing.Size(64, 16)
            Me.chkRslt.TabIndex = 9
            Me.chkRslt.Text = "Result"
            '
            'txtMessage
            '
            Me.txtMessage.Location = New System.Drawing.Point(224, 216)
            Me.txtMessage.Name = "txtMessage"
            Me.txtMessage.Size = New System.Drawing.Size(160, 20)
            Me.txtMessage.TabIndex = 10
            Me.txtMessage.Text = ""
            '
            'Form1
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(392, 269)
            Me.Controls.Add(Me.txtMessage)
            Me.Controls.Add(Me.chkRslt)
            Me.Controls.Add(Me.chkY)
            Me.Controls.Add(Me.chkX)
            Me.Controls.Add(Me.txtReturn)
            Me.Controls.Add(Me.Label2)
            Me.Controls.Add(Me.Label1)
            Me.Controls.Add(Me.txtX)
            Me.Controls.Add(Me.btnAnd)
            Me.Controls.Add(Me.btnGet)
            Me.Controls.Add(Me.btnCal)
            Me.Controls.Add(Me.txtY)
            Me.Controls.Add(Me.txtRslt)
            Me.Name = "Form1"
            Me.Text = "Form1"
            Me.ResumeLayout(False)

        End Sub

    #End Region
        Private objsc As MSSOAPLib30.SoapClient30


        Public Const sURL As String = "
    http://localhost/WebService_Sample/WebService/Service_Sample.asmx?wsdl"

     

        Private Sub btnCal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCal.Click
            objsc = New MSSOAPLib30.SoapClient30
            objsc.MSSoapInit(sURL)

            Me.txtRslt.Text = objsc.Calculator(Double.Parse(Me.txtX.Text.Trim), Double.Parse(Me.txtY.Text.Trim)).ToString
            Me.txtMessage.Text = objsc.Get__Property
        End Sub

     

        Private Sub btnGet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGet.Click
            objsc = New MSSOAPLib30.SoapClient30
            objsc.MSSoapInit(sURL)

            Me.txtReturn.Text = objsc.HelloWorld
            Me.txtMessage.Text = objsc.Get__Property
        End Sub

     

        Private Sub btnAnd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnd.Click
            objsc = New MSSOAPLib30.SoapClient30
            objsc.MSSoapInit(sURL)

            Me.chkRslt.Checked = objsc.Bool(Me.chkX.Checked, Me.chkY.Checked)
            Me.txtMessage.Text = objsc.Get__Property

        End Sub


    End Class

     

     

    2008年2月29日 3:49

答案

  • 不小的您是怎么想的。您的目的是在方法调用纸或返回一个状态信息。那为何不多添加一个引用参数呢?可以把方法改成这样的。

    Code Snippet
      _
        Public Function Calculator(ByVal pdX As Double, ByVal pdY As Double,ByRef state String ) As Double
            state = "new"
            Return pdX + pdY
        End Function

     

     

     

    调用方法可以改为

    Code Snippet

    Private Sub btnCal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCal.Click
            objsc = New MSSOAPLib30.SoapClient30
            objsc.MSSoapInit(sURL)

    Dim state as string

    state =""

            Me.txtRslt.Text = objsc.Calculator(Double.Parse(Me.txtX.Text.Trim), Double.Parse(Me.txtY.Text.Trim),Byref state ).ToString
            Me.txtMessage.Text =
    state    

     End Sub

     

     

     

     

    2008年3月17日 11:40