我正在学习为我在VB中编写的表单创建一个新的服务引用。当我像这样输入引用时:
ServiceReference.Operation
我收到错误消息“'操作'不是'服务参考'的成员” 我究竟做错了什么? 这是代码。 服务
Public Class Service
Implements IService
Public Sub New()
End Sub
Public Function GetChargeAmount(ByRef d As Double) As Double Implements IService.GetChargeAmount
Return 200.0 * (d * 60)
End Function
End Class
接口
<ServiceContract()>
Public Interface IService
<OperationContract()>
Function GetChargeAmount(ByRef d As Double) As Double
End Interface
<DataContract()>
Public Class CompositeType
<DataMember()>
Public Property BoolValue() As Boolean
<DataMember()>
Public Property StringValue() As String
End Class
致电代码
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
If (tbGroup.Text = "" Or tbMinutes.Text = "") Then
MessageBox.Show("Please fill out all necesary information")
ClearText()
Else
Dim dMoney As Double = 0.0
dMoney = ChargeReference.GetChargeAmount(Double.Parse(tbMinutes.Text))
tbTotalCharge.Text = dMoney.ToString("C")
End If
End Sub
答案 0 :(得分:0)
看起来您正在尝试使用服务引用的命名空间而不是ServiceClient
类来调用服务操作的方法 - 即ChargeReference.GetChargeAmount
而不是ChargeReference.ServiceClient.GetChargeAmount
。
请改为尝试:
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
If (tbGroup.Text = "" Or tbMinutes.Text = "") Then
MessageBox.Show("Please fill out all necessary information")
ClearText()
Else
Dim dMoney As Double = 0.0
Dim serviceClient = New ChargeReference.ServiceClient() ' Instantiate the service client.
dMoney = serviceClient.GetChargeAmount(Double.Parse(tbMinutes.Text))
tbTotalCharge.Text = dMoney.ToString("C")
End If
End Sub
如果查看为ChargeReference生成的代码,区别非常明显。为了更加清晰,我添加了一些评论:
'------------------------------------------------------------------------------
' <auto-generated>
' This code was generated by a tool.
' Runtime Version:4.0.30319.18408
'
' Changes to this file may cause incorrect behavior and will be lost if
' the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------
Option Strict On
Option Explicit On
'
' NOTE: namespace named the same as the service reference in Solution Explorer
'
Namespace ChargeReference
<System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0"), _
System.ServiceModel.ServiceContractAttribute(ConfigurationName:="ChargeReference.IService")> _
Public Interface IService
<System.ServiceModel.OperationContractAttribute(Action:="http://tempuri.org/IService/GetChargeAmount", ReplyAction:="http://tempuri.org/IService/GetChargeAmountResponse")> _
Function GetChargeAmount(ByRef d As Double) As Double
End Interface
<System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")> _
Public Interface IServiceChannel
Inherits ChargeReference.IService, System.ServiceModel.IClientChannel
End Interface
'
' NOTE: generated ServiceClient class through which to use the service reference
'
<System.Diagnostics.DebuggerStepThroughAttribute(), _
System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")> _
Partial Public Class ServiceClient
Inherits System.ServiceModel.ClientBase(Of ChargeReference.IService)
Implements ChargeReference.IService
Public Sub New()
MyBase.New
End Sub
Public Sub New(ByVal endpointConfigurationName As String)
MyBase.New(endpointConfigurationName)
End Sub
Public Sub New(ByVal endpointConfigurationName As String, ByVal remoteAddress As String)
MyBase.New(endpointConfigurationName, remoteAddress)
End Sub
Public Sub New(ByVal endpointConfigurationName As String, ByVal remoteAddress As System.ServiceModel.EndpointAddress)
MyBase.New(endpointConfigurationName, remoteAddress)
End Sub
Public Sub New(ByVal binding As System.ServiceModel.Channels.Binding, ByVal remoteAddress As System.ServiceModel.EndpointAddress)
MyBase.New(binding, remoteAddress)
End Sub
'
' NOTE: generated service-client (instance) method through which to call the service operation
'
Public Function GetChargeAmount(ByRef d As Double) As Double Implements ChargeReference.IService.GetChargeAmount
Return MyBase.Channel.GetChargeAmount(d)
End Function
End Class
End Namespace