Comparison between webHttpBinding, basicHttpBinding and wsHttpBinding
-
webHttpBinding is the REST-style binding, where you basically just hit a URL and get back a truckload of XML or JSON from the web service
-
basicHttpBinding and wsHttpBinding are two SOAP-based bindings which is quite different from REST. SOAP has the advantage of having WSDL and XSD to describe the service, its methods, and the data being passed around in great detail (REST doesn't have anything like that - yet). On the other hand, you can't just browse to a wsHttpBinding endpoint with your browser and look at XML - you have to use a SOAP client, e.g. the WcfTestClient or your own app
- Program.cs
- IService.cs
- Service.cs
- app.config
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WCFLearning2
{
class Program
{
static void Main(string[] args)
{
using (System.ServiceModel.ServiceHost host = new System.ServiceModel.ServiceHost(typeof(WCFLearning2.Service)))
{
host.Open();
Console.WriteLine("Service started. press any key to stop it");
Console.ReadLine();
host.Close();
}
}
}
}
2. IService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace WCFLearning2
{
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "users/{value}")]
string GetData(string value);
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json)]
string[] GetUser(string Id);
}
}
3.Service.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WCFLearning2
{
public class Service : IService
{
public string GetData(string value)
{
Console.WriteLine("You entered: {0}", value);
return string.Format("You entered: {0}", value);
}
public string[] GetUser(string Id)
{
return new User().GetUser(Convert.ToInt32(Id));
}
}
public class User
{
Dictionary<int, string> users = null;
public User()
{
users = new Dictionary<int, string>();
users.Add(1, "pranay");
users.Add(2, "Krunal");
users.Add(3, "Aditya");
users.Add(4, "Samir");
}
public string[] GetUser(int Id)
{
var user = from u in users
where u.Key == Id
select u.Value;
return user.ToArray<string>();
}
}
}
4. app.config
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<service name="WCFLearning2.Service"
behaviorConfiguration="WcfServiceLibrary.Service1Behavior">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8080/Service1/" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<!--<endpoint address="net.tcp://localhost:9000/myservice" binding="netTcpBinding" contract="WcfServiceLibrary.IService1">-->
<!--</endpoint>-->
<endpoint
address =""
binding="webHttpBinding"
contract="WCFLearning2.IService"
behaviorConfiguration="web">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- Metadata Endpoints -->
<!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
<!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WcfServiceLibrary.Service1Behavior">
<!-- To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
Result:
"You entered: good"