http://msdn.microsoft.com/zh-cn/vbasic/bb960413.aspx

  Visual Basic MVP, ObjectSharp Consulting

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 Person 

    Private _id As Integer 
    
Private _name As String 
    
Private _birthDate As DateTime 

    
Public Sub New(ByVal id As Integer, _ 
        
ByVal name As StringByVal birthDate As DateTime) 
        _id 
= id 
        _name 
= name 
        _birthDate 
= birthDate 
    
End Sub 

    
Public Property Id() As Integer 
        
Get 
            
Return _id 
        
End Get 
        
Set(ByVal value As Integer
            _id 
= value 
        
End Set 
    
End Property 
 
    
Public Property Name() As String 
        
Get 
            
Return _name 
        
End Get 
        
Set(ByVal value As String
            _name 
= value 
        
End Set 
    
End Property 
 
    
Public Property BirthDate() As DateTime 
        
Get 
            
Return _birthDate 
        
End Get 
        
Set(ByVal value As DateTime) 
            _birthDate 
= value 
        
End Set 
    
End Property 
 
    
Public Overrides Function ToString() As String 
        
Return String.Format( _ 
           
"{0} [Id:{1}; Birth Date: {2}]", _ 
            _name, _id, _birthDate) 
    
End Function 

End Class 

 

Services 

 

 

 

 Business
 
<ServiceContract()> _
Public Interface IPersonService
    
<OperationContract()> Function GetPerson(ByVal id As IntegerAs Person
    
<OperationContract()> Function GetPeople() As Person()
    
<OperationContract()> Sub UpdatePerson(ByVal p As Person)
End Interface 
 
<ServiceBehavior(InstanceContextMode:=InstanceContextMode.Single)> _
Public Class PersonService
    
Implements IPersonService

    
Private _people As New Dictionary(Of Integer, Person)

    
Public Sub New()
        _people.Add(
1New Person(1"Frodo Baggins", #1/1/1380#))
        _people.Add(
2New Person(2"Sam Gamgee", #2/2/1385#))
        _people.Add(
3New Person(3"Merry Brandybuck", #3/3/1390#))
        _people.Add(
4New Person(4"Pippin Took", #4/4/1395#))
    
End Sub 
 
    
Public Function GetPerson(ByVal id As IntegerAs Person Implements IPersonService.GetPerson
        
Dim result As Person = Nothing 
        
If _people.ContainsKey(id) Then 
            result 
= _people(id)
        
End If 
        
Return result
    
End Function 

    
Public Function GetPeople() As Person() Implements IPersonService.GetPeople
        
Dim result(_people.Count - 1As Person
        _people.Values.CopyTo(result, 
0)
        
Return result
    
End Function 
 
    
Public Sub UpdatePerson(ByVal p As Person) Implements IPersonService.UpdatePerson
        
If _people.ContainsKey(p.Id) Then 
            _people(p.Id) 
= p
        
End If 
    
End Sub 
End Class 

 

Hosts  

 

 

 

 Module1
    Sub Main()
        
Using host As New ServiceHost(GetType(Services.PersonService))
            host.Open()
            Console.WriteLine(
"Service started")
            Console.ReadLine()
        
End Using 
    
End Sub 
End Module 

 


  <system.serviceModel>
    
<behaviors>
      
<serviceBehaviors>
        
<behavior name="MexEnabled">
          
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8081/PersonService" />
        
</behavior>
      
</serviceBehaviors>
    
</behaviors>
    
<services>
      
<service behaviorConfiguration="MexEnabled" name="Services.PersonService">
        
<endpoint 
          
address="http://localhost:8081/PersonService" 
          binding
="basicHttpBinding"
          bindingConfiguration
="" 
          contract
="Services.IPersonService" />
      
</service>
    
</services>
  
</system.serviceModel>
</configuration>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 Person

    Private _id As Integer 
    
Private _name As String 
    
Private _birthDate As DateTime
 
    
Public Sub New(ByVal id As IntegerByVal name As StringByVal birthDate As DateTime)
        _id 
= id
        _name 
= name
        _birthDate 
= birthDate
    
End Sub 
 
    
<DataMember()> _
    
Public Property Id() As Integer 
        
Get 
            
Return _id
        
End Get 
        
Set(ByVal value As Integer)
            _id 
= value
        
End Set 
    
End Property 
 
    
<DataMember()> _
    
Public Property Name() As String 
        
Get 
            
Return _name
        
End Get 
        
Set(ByVal value As String)
            _name 
= value
        
End Set 
    
End Property 
 
    
<DataMember()> _
    
Public Property BirthDate() As DateTime
        
Get 
            
Return _birthDate
        
End Get 
        
Set(ByVal value As DateTime)
            _birthDate 
= value
        
End Set 
    
End Property 
 
    
Public Overrides Function ToString() As String 
        
Return String.Format("{0} [Id:{1}; Birth Date: {2}]", _name, _id, _birthDate)
    
End Function 
 
End Class 

 

经过这些修改,我们的服务应该可以使用了。右键单击 

 

 

 

 

 

 

 

 

 

 

 

 

 

),  _
     System.Runtime.Serialization.DataContractAttribute([Namespace]:="http://schemas.datacontract.org/2004/07/Business"),  _
     System.SerializableAttribute()
>  _
    
Partial Public Class Person
        
Inherits Object 
        
Implements System.Runtime.Serialization.IExtensibleDataObject
       
        
<System.NonSerializedAttribute()>  _
        
Private extensionDataField As System.Runtime.Serialization.ExtensionDataObject
        
        
<System.Runtime.Serialization.OptionalFieldAttribute()>  _
        
Private BirthDateField As Date 
        
        
<System.Runtime.Serialization.OptionalFieldAttribute()>  _
        
Private IdField As Integer 
        
        
<System.Runtime.Serialization.OptionalFieldAttribute()>  _
        
Private NameField As String 
        
        
Public Property ExtensionData() As System.Runtime.Serialization.ExtensionDataObject Implements System.Runtime.Serialization.IExtensibleDataObject.ExtensionData
            
Get 
                
Return Me.extensionDataField
            
End Get 
            
Set 
                
Me.extensionDataField = value
            
End Set 
        
End Property 
        
        
<System.Runtime.Serialization.DataMemberAttribute()>  _
        
Public Property BirthDate() As Date 
            
Get 
                
Return Me.BirthDateField
            
End Get 
            
Set 
                
Me.BirthDateField = value
            
End Set 
        
End Property 
        
        
<System.Runtime.Serialization.DataMemberAttribute()>  _
        
Public Property Id() As Integer 
            
Get 
                
Return Me.IdField
            
End Get 
            
Set 
                
Me.IdField = value
            
End Set 
        
End Property 
        
        
<System.Runtime.Serialization.DataMemberAttribute()>  _
        
Public Property Name() As String 
            
Get 
                
Return Me.NameField
            
End Get 
            
Set 
                
Me.NameField = value
            
End Set 
        
End Property 

    
End Class

 

 

 

 

 

 

 

 

 

 

.Load
        LoadListBox()
End Sub 
 
Private Sub LoadListBox()
    
Using ws As New PersonServiceProxy.PersonServiceClient
        PeopleListBox.DataSource 
= ws.GetPeople()
        PeopleListBox.DisplayMember 
= "Name" 
        PeopleListBox.ValueMember 
= "Id" 
    
End Using 
    PeopleListBox_SelectedIndexChanged(
Me, EventArgs.Empty)
    
AddHandler PeopleListBox.SelectedIndexChanged, AddressOf PeopleListBox_SelectedIndexChanged
End Sub 

 

现在,当用户选择 

 

 

 System.EventArgs)
        Using ws As New PersonServiceProxy.PersonServiceClient
            _person 
= ws.GetPerson(CInt(PeopleListBox.SelectedValue))
            IdTextBox.Text 
= _person.Id
            NameTextBox.Text 
= _person.Name
            BirthDateTextBox.Text 
= _person.BirthDate.ToShortDateString()
        
End Using 
End Sub

 

最后,在 

 

 

 SaveButton.Click
        _person.Id = IdTextBox.Text
        _person.Name 
= NameTextBox.Text
        _person.BirthDate 
= CDate(BirthDateTextBox.Text)

        
Using ws As New PersonServiceProxy.PersonServiceClient
            ws.UpdatePerson(_person)
        
End Using 

        LoadListBox()
    
End Sub 

 

 

 

 

 

 

 

 

 

 

 

 

 _
    Public ReadOnly Property Id() As Integer 
        
Get 
            
Return _id
        
End Get 
    
End Property 

 

清理解决方案,并右键单击 

 

 

 

 

 

 

 

 

 

 

 Person

    <DataMember()> Private _id As Integer 
    
<DataMember()> Private _name As String 
    
<DataMember()> Private _birthDate As DateTime
 
    
Public Sub New(ByVal id As IntegerByVal name As StringByVal birthDate As DateTime)
        _id 
= id
        _name 
= name
        _birthDate 
= birthDate
    
End Sub 
 
    
Public ReadOnly Property Id() As Integer 
        
Get 
            
Return _id
        
End Get 
    
End Property 
 
    
Public Property Name() As String 
        
Get 
            
Return _name
        
End Get 
        
Set(ByVal value As String)
            _name 
= value
        
End Set 
    
End Property 
 
    
Public Property BirthDate() As DateTime
        
Get 
            
Return _birthDate
        
End Get 
        
Set(ByVal value As DateTime)
            _birthDate 
= value
        
End Set 
    
End Property 
 
    
Public Overrides Function ToString() As String 
        
Return String.Format("{0} [Id:{1}; Birth Date: {2}]", _name, _id, _birthDate)
    
End Function 
 
End Class 

 

既然我们更改了 

 

 

 

 

 

 

 

 

 

 

 

 Form1
 
    Private _person As Business.Person = Nothing 
 
    
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadListBox()
    
End Sub 
 
    
Private Sub LoadListBox()
        
Using ws As New PersonServiceProxy.PersonServiceClient
            PeopleListBox.DataSource 
= ws.GetPeople()
            PeopleListBox.DisplayMember 
= "Name" 
            PeopleListBox.ValueMember 
= "Id" 
        
End Using 
 
        PeopleListBox_SelectedIndexChanged(
Me, EventArgs.Empty)
        
AddHandler PeopleListBox.SelectedIndexChanged, AddressOf PeopleListBox_SelectedIndexChanged
    
End Sub 
 
    
Private Sub PeopleListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        
Using ws As New PersonServiceProxy.PersonServiceClient
            _person 
= ws.GetPerson(CInt(PeopleListBox.SelectedValue))
            IdTextBox.Text 
= _person.Id
            NameTextBox.Text 
= _person.Name
            BirthDateTextBox.Text 
= _person.BirthDate.ToShortDateString()
        
End Using 
    
End Sub 
 
    
Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
        _person.Name 
= NameTextBox.Text
        _person.BirthDate 
= CDate(BirthDateTextBox.Text)
        
Using ws As New PersonServiceProxy.PersonServiceClient
            ws.UpdatePerson(_person)
        
End Using 
        LoadListBox()
    
End Sub 
End Class 

 

你现在应该能够运行和测试完成的程序了。 

 

 

 

 

 

 ToStringButton.Click
        Dim p As Business.Person
        p 
= CType(PeopleListBox.SelectedItem, Business.Person)
        
MsgBox(p.ToString())
End Sub 

 

当你单击按钮后,你看到的结果像如下样子: 

 

 

 

 

 

 

 

相关文章: