Call (Consume) Web Service (ASMX) in Generic Handler using C# and VB.Net

Last Reply one year ago By kalpesh

Posted one year ago

I have a webservice 'Sample.asmx' and a generic handler 'SampleHandler.ashx'. How can I call a webservice's method from the generic handler.

Posted one year ago Modified on one year ago

Same way you can call Webserice method in Handeler like we do in application as same in this article Call (Consume) Web Service (ASMX) in ASP.Net web application using C# and VB.Net.

Add service reference (which will create a proxy class) and create an instance of Webservice proxy class and call this method in hander same way like we do in Normal project.

Also refer the below sample code for your reference.

WebService

CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{

    public WebService()
    {
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string GetCurrentTime(string name)
    {
        return string.Format("Hello {0} \nCurrent Time: {1}", name, DateTime.Now.ToString());
    }
}

VB

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.Services

<WebService([Namespace]:="http://tempuri.org/")>
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)>
Public Class WebService
    Inherits System.Web.Services.WebService

    Public Sub New()
    End Sub

    <WebMethod>
    Public Function GetCurrentTime(ByVal name As String) As String
        Return String.Format("Hello {0} " & vbLf & "Current Time: {1}", name, DateTime.Now.ToString())
    End Function
End Class

Handler.ashx

C#

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string name = context.Request.QueryString["name"];
        GetNameWithDate.WebService service = new GetNameWithDate.WebService();
        string namewithTime = service.GetCurrentTime(name);
        context.Response.ContentType = "text/plain";
        context.Response.Write(namewithTime);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

VB

<%@ WebHandler Language="VB" Class="Handler" %>

Imports System
Imports System.Web

Public Class Handler
    Inherits IHttpHandler

    Public Sub ProcessRequest(ByVal context As HttpContext)
        Dim name As String = context.Request.QueryString("name")
        Dim service As GetNameWithDate.WebService = New GetNameWithDate.WebService()
        Dim namewithTime As String = service.GetCurrentTime(name)
        context.Response.ContentType = "text/plain"
        context.Response.Write(namewithTime)
    End Sub

    Public Property IsReusable As Boolean
        Get
            Return False
        End Get
    End Property
End Class 

Default.aspx

Name&nbsp;&nbsp;<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
<br />
<asp:Button runat="server" ID="btnSubmit" Text="Submit" OnClick="Submit" />
<br />
<br />
<asp:Label ID="lblMessage" runat="server"></asp:Label>

C#

protected void Submit(object sender, EventArgs e)
{
    string getNameWithDateTime = "http://localhost:4635/142280/Handler.ashx?name=" + txtName.Text;
    string response = (new WebClient()).DownloadString(getNameWithDateTime);
    lblMessage.Text = response;
}

VB.Net

Protected Sub Submit(ByVal sender As Object, ByVal e As EventArgs)
    Dim getNameWithDateTime As String = "http://localhost:4635/142280/Handler.ashx?name=" & txtName.Text
    Dim response As String =(New WebClient()).DownloadString(getNameWithDateTime)
    lblMessage.Text = response
End Sub

Screenshot