In one of previous WCF Tutorial for beginners, I discussed about the ABC’s of WCF Service Endpoint. An Endpoint of Service has following three parts also known as ABC (Address, Binding, Contract):
- Address defines Where: Where is the location of WCF Service?
- Binding defines How: How we can access the WCF Service?
- Contract defines What: What a WCF Service expose?
As Binding in Windows Communication Foundation defines that how to communicate with a WCF Service? It specifies the follows details:
- Communication protocol
- Encoding method
- Other optional factors as transactions, reliable sessions and security etc.
In order to get in-depth and detailed understanding of WCF Binding, you can follow other WCF Tutorial on “Understanding WCF Binding and Channel Stack“.
In this WCF Tutorial, we are going to discuss that how we can configure WCF Binding using two different approaches? We can configure our WCF Service administratively (through configuration file) as well as Programmatically.
Consider we have a WCF Service (say StudentService) and it’s hosted in IIS. There are a number of built-in WCF Bindings and for this implementation we are using “wsHttpBinding” as binding. All built-in bindings have it’s default settings and we normally don’t change them. But for a specific requirement, we can modify it accordingly.
WCF Binding Configuration – Configuration file Approach:
While configuring WCF through configuration file, we can easily understand that how above discussed concepts are configured for a WCF Service? How ABC’s of an Service endpoint are configured? How WCF Binding defined to communicate a Service? etc.
<services>
<service name=”StudentService”>
<endpoint
address=”http://localhost/StudentIISHost/MyStudentHost.svc”
binding=”wsHttpBinding”
contract=”IStudentService”>
<dns value=”localhost”/>
</identity>
</endpoint>
<endpoint address=”mex”
binding=”mexHttpBinding”
contract=”IMetadataExchange”/>
</service>
</services>
Using above configuration, wsHttpBinding will be used with it’s default settings but if we need to change these default settings, we can do that by adding “bindingName” parameter (bindingName=”studentWSConfiguration”) to endpoint and provide following configuration under .
<wsHttpBinding>
<binding name=”studentWSConfiguration”
</bindings>
WCF Binding Configuration – Programmatical Approach:
Although it’s recommended to use the Configuration file approach for WCF binding configuration because we don’t need to change the code and compile again during deployment but still we can achieve the above programmatically as follows:
try
{
//Base Address for StudentService
Uri httpBaseAddress = new Uri(“http://localhost:4321/StudentService”);
studentServiceHost = new ServiceHost(typeof(StudentService.StudentService),
httpBaseAddress);
WSHttpBinding studentWSConfiguration = new WSHttpBinding();
wshttpbind.MaxReceivedMessageSize = 70000;
wshttpbind.MessageEncoding = WSMessageEncoding.Text;
studentServiceHost.AddServiceEndpoint(typeof(StudentService.IStudentService), studentWSConfiguration, “”);
ServiceMetadataBehavior serviceBehavior = new ServiceMetadataBehavior();
serviceBehavior.HttpGetEnabled = true;
studentServiceHost.Description.Behaviors.Add(serviceBehavior);
studentServiceHost.Open();
Console.WriteLine(“Service is live now at : {0}”, httpBaseAddress);
Console.ReadKey();
}
catch (Exception ex)
{
studentServiceHost = null;
Console.WriteLine(“There is an issue with StudentService” + ex.Message);
}
This is how we can configure WCF Bindings but remember WCF (Windows Communication Foundation) is extensible so we can define our custom binding also for a very specific need.
Other Related Articles:
- WCF Hosting (Console | Windows Service | IIS | WAS )
- WCF Tutorial Step by Step
- 5 simple steps to create your first WCF RESTful Service
- WCF Vs ASMX Service
- MVC3 Vs MVC4 Vs MVC5
- Difference betweeen ASP.NET WebForms and ASP.NET MVC
- 3 ways to generate proxy for WCF Service
- Post JSON data to WCF RESTful Service using jQuery
- Consuming a WCF RESTful Service using jQuery
Top 10 Interview Questions and Answers Series:
- Top 10 WCF Interview Questions
- Comprehensive Series of WCF Interview Questions
- Top 10 HTML5 Interview Questions
- Top 10 ASP.NET Interview Questions
- Comprehensive Series of ASP.NET Interview Questions
- Top 10 ASP.NET MVC Interview Questions
- Top 10 ASP.NET Web API Interview Questions
- Top 10 ASP.NET AJAX Interview Questions