WebService

Calling Webservice Asyncronously in windows application

Calling webservice async in Windows Application
1.Create a web service.
2.Add web reference to your windows app. In order to do this Click add service reference -> advanced setting>add web reference.
This is needed bc Async methods will not be generated if you add a normal web reference.
The remaining can be done by below article


Web Service Completed Events Firing Twice

For this initialize your webservice object to null in beginning. And later when you use its method instantiate it.
eg-
Webservice
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string SendData()
{
  return "Data Sent";
}

WebMethod referred in  windows app

public class TestClass
{

<ServiceNameGiven>. Service1 objService=null; //intialize it to null intially
 public void TestMethod()
{
//now instantiate it.
 <ServiceNameGiven>. Service1 objService=new <ServiceNameGiven>. Service1();
}


Timeout issue in webservice 

Error message-
 

The request channel timed out while waiting for a reply after 00:00:02.1500000. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout



This issue is caused because of some operation that takes longer time than that configured in your webservice and your client setting . to fix this –make the following setting

1.In your webservice add the following settings-

The setting is in web.config file
 
<configuration>
 <system.web>
    <httpRuntime maxRequestLength="8096"
        useFullyQualifiedRedirectUrl="true"
        executionTimeout="1800"/> //set for 30 min
  </system.web>
</configuration>

2.On your client side(the application that is consuming the webservice)
Change the sendTimeout = "00:30:00" //desired time you want.
This wil be in app.config file
Returning value from a asyn webservice

Below is the webservice example-
Webservice
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string SendData()
{
  return "Data Sent";
}
}

Calling app--windows app
1.Add webreference to the given service.
2. intialialize the webservice object in the onload

<AddedwebServiceName>.Service1 obj= new <AddedwebServiceName>.Service1()

Create a event handler for the completed function exposed by webmethod
<AddedwebServiceName>.SendDataCompleted += EventHandler

3.Call the asyn function in any event like button click
<AddedwebServiceName>.SendDataAsync

4.Add the functions which will be invoked when a function is completed

  public void EventHandler(object sender, localhost.SendDataCompletedEventArgs e)
{
 //this will give you the result.since webmethod returns a string this functions will give string value.
string result =e.result

}

Steps to take care when changing the default class name of webservice

1. suppose your newly added webservice generated the below class for you


public class Service1 : System.Web.Services.WebService
{
}


change the class name to desired class name eg-

public class MyWebservice: System.Web.Services.WebService
{
}

2. Search for "Codebehind in your solution. it openes up Service1.asmx with below line
"<%@ WebService Language="C#" CodeBehind="service1.asmx.cs" Class="<namespace>.service1" %>"

Change the Class in the above tag to <Namespace>.Mywebservice(or as desired)




The test form is only available for requests from the local machine 

If you want to test your simple webservice by clicking on "Invoke" button  it should be enabled for remote access.
The invoke button wont be available if your webservice is accessed remotely however it will be available locally.
To enable this setting add foll to your config file-

<configuration>
    <system.web>
    <webServices>
        <protocols>
            <add name="HttpGet"/>
            <add name="HttpPost"/>
        </protocols>
    </webServices>
    </system.web>
</configuration>



Method overloading in webservice

Remember webservice cannot have two webservice with same name and same parameters(this cannot be don’t anyhow).

 However method overloading is possible.The methods that are being overloaded should have an external name given by “MessageName”

[WebMethod( MessageName = "AddInt")]
Public void Add(int a,int b)()
{
}

[WebMethod( MessageName = "AddFloat")]
Public void Add(float a,float b)()
{
}

This solution will work in older version of dot net. But in .Net 2.0 it will give an error saying the webservice does not confirm to (WS-I) Baisc Profile 1.1..

To solve this set 

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

At the beginning of the code to

[WebServiceBinding(ConformsTo = WsiProfiles.None)] 


How do you call a same webservice when its hosted in multiple server dynamically at runtime

When a webservice is added to the client WSDL is generated. This WSDL will have a URL of the webservice. So the added webreference is associated with a single URL.

Now if the same webservice present in some other server needs to be called then this URL needs to be changed. This can be done dynamically at run time.
Before calling the webservice change the URL to the URL where the calling webservice will be located.
eg-
string path = "http://" + IpAddress + WebServiceURlPath;
 <WebserviceObject>.Url = path;


Windows Authentication in WebService
In order to achieve this perform the following steps

1.Change the authentication mode to “Windows” in web.config file in the webservice.

<authentication mode="Windows" />

2.Just before calling any webservice methods on the client side set the “Credentials” property of webservice.

Eg-
<webserviceName>.Credentials = new System.Net.NetworkCredential(UserName, Password);

3.Make changes in IIS to enable Windows Authentication.
Navigate to the website where your service is hosted in IIS Manage and disable “Anonymous Authentication “ and enable “Windows Authentication”.


1.What is WebService?
Webservice are functionalities or web APIs exposed over the web.
It can be developed in any language. It is platform independent
Webservice communication takes place via SOAP(Simple Object Access Protocol).So any language capable of communicating via SOAP can consume webservice.
Webservice are used in SOA architecture.

2. What are different components in Webservice ?

ASMX file- This file provides access to the webservice.

DISCO(Discovery of Webservice) files- This are used to locate the webservice. They contain the path where the webservice is located.

WSDL(Webservice Description Language)- these are used to describe the webservice. They give details about the methods exposed by the webservice and the parameters they take.
They can be accessed by appending “?WSDL” to the webservice path.

UDDI(Universal Description, Discovery and Integration)  -It is a repository where different webservice can be located.


SOAP(Simple Object Access Protocol) –Its a XML based protocol which is used be send request and get response back from the webservice.


4.How do you deploy a webservice ?
Webservice are deployed in IIS either manually or through a set up project.

Manual deployment
1.Create a new virtual directory in IIS. Point the path of the virtual directory to a <Sample >folder sotred in the hard disk.

2.The <Sample>folder will contain bin folder,asmx file and web.config of the webserive.

3.The service can now be accessed by typing following in the browser
http://<ip of the machine where service is hosted>/<virtual directory name>/<asmx file name>.asmx
 



5 comments:

  1. Informative post you have shared about web services and i got clear information through this post.Website Hosting

    ReplyDelete
  2. IT's very informative blog and useful article thank you for sharing with us , keep posting learn more about Microsoft do net
    .NET Online Training

    ReplyDelete
  3. Wonderful bloggers like yourself who would positively reply encouraged me to be more open and engaging in commenting.So know it's helpful.
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete
  4. Actually I read it yesterday but I had some thoughts about it and today I wanted to read it again because it is very well written.
    <a href="https://360digitmg.com/india/professional-certification-in-business-analytics business analytics online course</a>

    ReplyDelete
  5. Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.data science training in coimbatore

    ReplyDelete