I am want to program a simple "Hello world" example for a communication between WCF service and an html page.
To program the WCF server I use the code below:
namespace ConsoleHelloWorldServiceClient
{
class Program
{
static void Main(string[] args)
{
var adrs = new Uri[1];
adrs[0] = new Uri("http://localhost:6464");
using (ServiceHost host = new ServiceHost(typeof(HelloWorld.HelloWorldService ),adrs))
{
host.Open();
Console.WriteLine("Server is open");
Console.WriteLine("Press Enter to close server");
Console.ReadLine();
}
}
}
}
Hello world Interface
namespace HelloWorld
{
[ServiceContract]
public interface IHelloWorldService
{
[OperationContract]
string SayHello();
}
}
Hello world class
namespace HelloWorld
{
[DataContract]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class HelloWorldService : IHelloWorldService
{
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "SayHello")]
public string SayHello()
{
return "Hello World!";
}
}
}
Now in my HTML page i would like to click on a button and display the text.
Then I use JQuery to communicate with the service:
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript"
src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.1.min.js">
</script>
<script type="text/javascript">
var url = 'http://localhost:6464/HelloWorldService';
$(document).ready(function () {
$("#get").click(function () {
var urlGet = url + 'SayHello';
$.getJSON(urlGet, function (data) {
$('#text').val(data);
});
});
});
</script>
</head>
<body>
<input id="text" type="text" value="Hello" />
<input id="get" type="button" value="Get" />
</body>
</html>
But I am the feeling that this client is used only for webservers... How can I do it?
Thanks for your help.