Method Overloading in WCF using VB.NET

In this article you will learn how to do methods overloading by set two methods in the Service Contract attribute in WCF.
  • 4146

Introduction: Method Overloading in WCF.
WCF solves so many problems of distributed computing, but overloading of methods is not performed directly in WCF. So I have created a simple Service Contract with two methods using method overloading. I have added a property 'Name' in the operation contract attribute.
Step 1: Open visual studio and click on file menu.
  • Go to new -> project.

     
  • New project dialog box will appear.

     
  • Select WCF -> WCF Service Application.

     
  • Give the name as you desire.

     
  • Press ok as shown below on figure:

vb1.gif
 
  • Service1.svc.vb will be open.

Step 2: Go to solution explorer.
  • open the Iservice1.vb.

     
  • write a code like:

Code:
' NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
<ServiceContract()> _
Public Interface IService1
 
    <OperationContract(Name:="Addnumber")> _
    Function add(a As Integer, b As Integer) As Integer
 
    <OperationContract(Name:="ConcatenateStrings")> _
    Function add(s1 As String, s2 As String) As String
 
 
    ' TODO: Add your service operations here

End Interface

 Step 3: Now, open the Service1.svc.vb.

  • write the code like:

Code:
' NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
Public Class Service1
    Implements IService1

    Public Function add(a As Integer, b As Integer) As Integer
        Return a + b
    End Function   
   
    Public
Function add(s1 As String, s2 As String) As String
        Return s1 & s2
    End Function

End Class

Step 4: Start Debugging by pressing F5.

  • A dialog box will appear i.e 'WCF Test Client'.

  • Copy the highlighted address as shown below in figure:



vb3.gif
 

Step 5: Open the visual studio and go to the file menu.

  • Select new -> project.

  • Select any type of application. Suppose we add 'ASP.NET Web Application' as shown in the figure below:
     



 vb2.gif

Step 6: Go to Solution Explorer.

  • Right click on 'References' and select 'Add Service Reference...'

  • 'Add Service Reference' dialog box will appear.

  • Paste the address which is copied in step 4.

  • Press OK button as shown in the figure below


vb4.gif 

Step 7: Now, take controls in the div tag on Form design.
 

  • Write the code on 'WebForm1.aspx.vb' as:

Code:

Partial Public Class WebForm1
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As EventArgs)
 
    End Sub
    Private obj As New ServiceReference1.Service1Client()
    Protected Sub Button1_Click(sender As Object, e As EventArgs)
        Label1.Visible = True
        Label2.Text = (obj.Addnumber(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text)).ToString())
    End Sub
 
    Protected Sub Button2_Click(sender As Object, e As EventArgs)
        Label1.Visible = True
        Label2.Text = (obj.ConcatenateStrings(TextBox1.Text, TextBox2.Text))
    End Sub

End Class

Step 8: Now we press F5 and run this application and result shows like figure below:

vb5.gif
vb6.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.