ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Call (Consume) Web Service (ASMX) in Generic Handler using C# and VB.Net<p>Same way you can call Webserice method in Handeler like we do in application as same in this article <a title="Call (Consume) Web Service (ASMX) in ASP.Net web application using C# and VB.Net'a" href="https://www.aspsnippets.com/Articles/Call-Consume-Web-Service-ASMX-in-ASPNet-web-application-using-C-and-VBNet.aspx">Call (Consume) Web Service (ASMX) in ASP.Net web application using C# and VB.Net</a>.</p> <p><strong>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.</strong></p> <p>Also refer the below sample code for your reference.</p> <p><span style="text-decoration: underline;"><strong>WebService</strong></span></p> <p><strong>CS</strong></p> <pre class="brush: csharp">using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; /// &lt;summary&gt; /// Summary description for WebService /// &lt;/summary&gt; [WebService(Namespace = &#34;http://tempuri.org/&#34;)] [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(&#34;Hello {0} \nCurrent Time: {1}&#34;, name, DateTime.Now.ToString()); } }</pre> <p><strong>VB</strong></p> <pre class="brush: vb">Imports System Imports System.Collections.Generic Imports System.Linq Imports System.Web Imports System.Web.Services &lt;WebService([Namespace]:=&#34;http://tempuri.org/&#34;)&gt; &lt;WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)&gt; Public Class WebService Inherits System.Web.Services.WebService Public Sub New() End Sub &lt;WebMethod&gt; Public Function GetCurrentTime(ByVal name As String) As String Return String.Format(&#34;Hello {0} &#34; &amp; vbLf &amp; &#34;Current Time: {1}&#34;, name, DateTime.Now.ToString()) End Function End Class </pre> <p><span style="text-decoration: underline;"><strong>Handler.ashx</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">&lt;%@ WebHandler Language=&#34;C#&#34; Class=&#34;Handler&#34; %&gt; using System; using System.Web; public class Handler : IHttpHandler { public void ProcessRequest(HttpContext context) { string name = context.Request.QueryString[&#34;name&#34;]; GetNameWithDate.WebService service = new GetNameWithDate.WebService(); string namewithTime = service.GetCurrentTime(name); context.Response.ContentType = &#34;text/plain&#34;; context.Response.Write(namewithTime); } public bool IsReusable { get { return false; } } }</pre> <p><strong>VB</strong></p> <pre class="brush: vb">&lt;%@ WebHandler Language=&#34;VB&#34; Class=&#34;Handler&#34; %&gt; Imports System Imports System.Web Public Class Handler Inherits IHttpHandler Public Sub ProcessRequest(ByVal context As HttpContext) Dim name As String = context.Request.QueryString(&#34;name&#34;) Dim service As GetNameWithDate.WebService = New GetNameWithDate.WebService() Dim namewithTime As String = service.GetCurrentTime(name) context.Response.ContentType = &#34;text/plain&#34; context.Response.Write(namewithTime) End Sub Public Property IsReusable As Boolean Get Return False End Get End Property End Class </pre> <p><span style="text-decoration: underline;"><strong>Default.aspx</strong></span></p> <pre class="brush: html">Name&amp;nbsp;&amp;nbsp;&lt;asp:TextBox ID=&#34;txtName&#34; runat=&#34;server&#34;&gt;&lt;/asp:TextBox&gt;&lt;br /&gt; &lt;br /&gt; &lt;asp:Button runat=&#34;server&#34; ID=&#34;btnSubmit&#34; Text=&#34;Submit&#34; OnClick=&#34;Submit&#34; /&gt; &lt;br /&gt; &lt;br /&gt; &lt;asp:Label ID=&#34;lblMessage&#34; runat=&#34;server&#34;&gt;&lt;/asp:Label&gt;</pre> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Submit(object sender, EventArgs e) { string getNameWithDateTime = &#34;http://localhost:4635/142280/Handler.ashx?name=&#34; + txtName.Text; string response = (new WebClient()).DownloadString(getNameWithDateTime); lblMessage.Text = response; }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Submit(ByVal sender As Object, ByVal e As EventArgs) Dim getNameWithDateTime As String = &#34;http://localhost:4635/142280/Handler.ashx?name=&#34; &amp; txtName.Text Dim response As String =(New WebClient()).DownloadString(getNameWithDateTime) lblMessage.Text = response End Sub </pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><span style="text-decoration: underline;"><strong><img src="https://i.imgur.com/bMi7qGp.gif" alt="" width="290" height="184" /></strong></span></p> <p>&nbsp;</p> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/142280/Call-Consume-Web-Service-ASMX-in-Generic-Handler-using-C-and-VBNet/https://www.aspforums.net:443/Threads/142280/Call-Consume-Web-Service-ASMX-in-Generic-Handler-using-C-and-VBNet/Mon, 26 Feb 2018 01:20:59 GMT