Configuring ASP.NET Web Service


Hi,

Well today i created a webservice which would fetch me some data from oracle database. The method signature was something like this

public DataSet getEmpInfo

{,,,,,,,,,,,,,}

I was calling this webservice within form’s onLoad inside the a custom entity of Microsoft Dyanmics CRM3,0. 

I deployed it in my machine and when i was opening the my custom entity form inside CRM in my machine, i was able to see the values fetched using ajax calls to webservice.

But for other machines i was getting an internal server error for my ajax call.

Finally the error got resolved when i included this inside the web.config for my webservice

<webServices>
    <protocols>     
        <add name=”HttpPost”/> 
        <add name=”HttpGet”/>  

 </protocols>
</webServices>

By the way, I was using POST in my xmlHttpRequest object Open method.

Plzz check out these links to better understand web services.

Configuration Options for XML Web Services Created Using ASP.NET 

http://msdn2.microsoft.com/en-us/library/b2c0ew36(vs.80).aspx

How to: Disable Protocol Support for Web Services 

http://msdn2.microsoft.com/en-us/library/9hdd3w8c(VS.80).aspx

Top Five ASP.NET Web Services Tips

http://www.ondotnet.com/pub/a/dotnet/2002/10/07/webservices.html

Bye

Advertisement

Calling Asp.NET web service from javascript (Ajax)-(Passing Parameter-POST)


Hi,

Now we will modify the application that we developed in the previous post to work with POST parameter

https://nishantrana.wordpress.com/2007/10/22/calling-aspnet-web-service-from-javascript-ajax-passing-parameter/

We have changed the get to post as we are now not passing the value for Name by appending it in the url.

Only make changes to the function getMessage()

function getMessage()

{

var name=’Nishant’;

xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);

xmlHttp.open(“POST“, “http://localhost/WebService1/Service1.asmx/HelloWorld&#8221;, true);

xmlHttp.onreadystatechange=doUpdate;

xmlHttp.setRequestHeader(“Content-Type”,”application/x-www-form-urlencoded”);

xmlHttp.send(“name=”+escape(name));

return false;

}

Let’s understand the changes that we have made

First of all we have change the method of passing data to POST from GET.

But still we need to pass the name’s value which our web service method needs.

For that we’ll modify our Send() method

xmlHttp.send(“name=”+escape(name));

We are using the same name/value pair in send()  which we used at the end of the request URL in the GET version

escape()-It makes sure that the values are valid values i.e. no tricky characters are there.

But this is not enough that is why we added this line

xmlHttp.setRequestHeader(“Content-Type”,”application/x-www-form-urlencoded”);

Here the server doesn’t know what kind of data it can expect or is coming from our application.

We are making use of setRequestHeader function to tell the server about the content type we are going to send.

The server can get this information from request’s header that it recieves.

Content-Type  -> Name of the header

application/x-www-form-urlencoded -> http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1

Bye

Calling Asp.NET web service from javascript (Ajax)-(Passing Parameter-GET)


Hi,

Now we will modify the application that we developed in the previous post to work with parameters

https://nishantrana.wordpress.com/2007/10/18/calling-aspnet-webservice-from-javascript-ajax/

1) First we will modify the default Hello World service to accept a parameter like this

[WebMethod]

public string HelloWorld(string Name)

{

return “Hello “+Name+”!”;

}

2) Add the html textbox control in the webform

<INPUT type=”text” id=”Info”>

3) Add the following in the body

<BODY onload=”getMessage()”>

4) Put the following script in our webpage.

<script language=”javascript”>

var xmlHttp;

function getMessage()

{

xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);

xmlHttp.open(“get”, “http://localhost/WebService1/Service1.asmx/HelloWorld? name=’Nishant'”, true);

xmlHttp.onreadystatechange=doUpdate;

xmlHttp.send(); return false;

}

function doUpdate()

{

if(xmlHttp.readyState==4)

{

var xmlDoc=xmlHttp.responseXML;

var responseElement=xmlDoc.getElementsByTagName(“string”)[0];

var respText=responseElement.firstChild.nodeValue;

document.forms[0].elements[‘Info’].value=respText;

}

}

</script>

5) We have changed the method of passing the data to server from post to get as we are passing the value for Name by appending it in the url.

6) Right now if we run the application it will give us error as “object not found”.

7) The reason for the error is that we have to make our web service configured for get method.

8- We need to open web.config of our web service and add the following line inside system.web section

<webServices>

<protocols>

<add name=”HttpGet”/>

</protocols>

</webServices>

9) We need to build the service again

10) This time our application should work without any error.

Bye

Calling Asp.NET web service from javascript (Ajax)


Here we will be calling the default helloworld webservice that is already created for us when we open a asp.net webservice project template in Visual Studio.

We’ll just have a textbox(html control) which will display us the Hello World! response returned from the web service.

This is the step we need to follow

1) Create a new ASP.NET WebApplication

2) Put a html textbox control on the form

<input type=”text” id=”info”/>

3) Put this script code in the head section of the aspx page

<script language=”javascript”>

var xmlHttp;

function getMessage()

{

xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);

xmlHttp.open(“post”, “http://localhost/WebService1/Service1.asmx/HelloWorld&#8221;, false);

xmlHttp.onreadystatechange=doUpdate;

xmlHttp.send();

return false;

}

function doUpdate()

{

if(xmlHttp.readyState==4)

{

var startTag = “<string xmlns=\”http://tempuri.org/\”>”;

var endTag = “</string>”;

var exch;

var valueStart = 0;

var valueEnd = 0;

valueStart = xmlHttp.responseXML.xml.indexOf(startTag, valueEnd) + startTag.length;

valueEnd = xmlHttp.responseXml.xml.indexOf(endTag, valueEnd+1);

exch = xmlHttp.responseXML.xml.substring(valueStart, valueEnd);

document.forms[0].elements[‘Info’].value=exch;

}

</script>

4) Call the getMessage function in the body’s onLoad eventHandler

<BODY onload=”getMessage()”>

5) Run the applicaton. We’ll see the HelloWorld! text in our textbox(‘info’)

Now let us understand what is happening inside the javascript code

xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”)

This line of code creates the XMLHttpRequest object. This object sends request to the server and processes the responses from it.

The above code creates the object specific to Internet Explorer( <=6.o).

It is implemented as Active X for IE. However in IE 7 XMLHttpRequest will come as native JavaScript object.

For other browsers we can write

xmlHttp=new XMLHttpRequest();

or best we can write this

if(window.ActiveXObject)

{

xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);

}

else if (window.XmlHttpRequest)

{

xmlHttp=new XMLHttpRequest();

}

xmlHttp.open(“post”, “http://localhost/WebService1/Service1.asmx/HelloWorld&#8221;, false);

The open method initializes the connection to the server and informs the xmlHttp object how to connect to the server.

post- it indicates how we want to send the data. It can be “get” as well

url- comes the url where we are connecting

false- this means we are making a synchronous call. To make asychronous call simply set it to true

xmlHttp.onreadystatechange=doUpdate;

This specifies the name of the function to be called whenever the state of the xmlHttpRequest changes

xmlHttp.send();

Send method than makes the request to server. This method would return immediately in case of asynchronous call and it would block until the synchronous response is received from the server.

if(xmlHttp.readyState==4)

It tells about the current state of the request

0- uninitialized

1- loading

2- loaded

3-interactive

4-complete

xmlHttp.responseXML.xml

It returns the current response from server in XML=

<?xml version=”1.0″?>

<string xmlns=”http://tempuri.org/”>Hello World !</string>

Than we are using some javascript functions to get the Hello World! and remove everything else.

document.forms[0].elements[‘Info’].value=exch;

Finally assigning the value to our textBox.

The function doUpdate() can be written differently so that we don’t have to make use of any string functions.

function doUpdate()

{

if(xmlHttp.readyState==4)

{

// Here the server is returning us XML in response so we can make use of responseXML

// Here the browser creates a DOM tree to represent that XML and puts a reference to that DOM tree

// in the request’s (xmlHttp) object’s responseXML

var xmlDoc=xmlHttp.responseXML;

var responseElement=xmlDoc.getElementsByTagName(“string”)[0];

var respText=responseElement.firstChild.nodeValue;

document.forms[0].elements[‘Info’].value=respText;

}

 

However creating a ASP.NET AJAX webservice is little different than our normal ASP.NET web service, and calling them using the ASP.NET AJAX client library is also a bit different

https://nishantrana.wordpress.com/2007/11/06/creating-and-calling-aspnet-ajax-web-service/

Bye

Finding no of users online ASP.NET


To do this,
1) Add a new item global.asax in your website.
2) Put the following code in it

void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application[“UsersOnline”] = 0;
}

void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Application.Lock();
Application[“UsersOnline”] = (int)Application[“UsersOnline”] + 1;
Application.UnLock();
}

void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
Application.Lock();
Application[“UsersOnline”] = (int)Application[“UsersOnline”] – 1;
Application.UnLock();
}

3) In the webpage where the no of online users have to be displayed make use of this application object

protected void Page_Load(object sender, EventArgs e)
{
Response.Write(“The no of users online are ” + Application[“UsersOnline”].ToString());
}



That’s it

%d bloggers like this: