Define Host Base Address.
<configuration>
<system.serviceModel>
<services>
<service name="WcfCustomerService.CustomerService">
<endpoint address =""
binding="basicHttpBinding"
contract="WcfCustomerService.ICustomerService"/>
<host>
<baseAddresses>
<add baseAddress ="http://localhost:8081/CustomerService"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
Once above code is in place we can now good to built and run the console host executable. Action: CTRL+F5
***Built & Run WCFHost :Output
It may happen the port which we configured in Endpoints may be used by some other application in transport layer and hence we may come across below mentioned error. Remediation change the Address port number[XXX] to some unused one like http://localhost:XXX/ABC
Add project-> window Console application for WCF Client.
Add Service Reference(Project -> Add Service Reference menu )
The following error may occurred: WCF Host is not running. ERROR 1:There was an error downloading 'http://localhost:8081/CustomerService'. Unable to connect to the remote server No connection could be made because the target machine actively refused it 10.0.0.1:8081 Metadata contains a reference that cannot be resolved: 'http://localhost:8081/CustomerService'. There was no endpoint listening at http://localhost:8081/CustomerService that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. Unable to connect to the remote server No connection could be made because the target machine actively refused it 10.0.0.1:8081 If the service is defined in the current solution, try building the solution and adding the service reference again.
Meta Data Not Configured In WCF Host Configuration
***Add Meta Data Exchange
In order to create proxy class or allow VS2010 Add Service Reference(Project ➤ Add Service Reference menu ) to properly create proxy class we need to add metadata exchange configuration. This can be achieved including <Behaviour> tags in Host configuration file. The host will create meta data representation of service to help creating proxy class for client. This is the same concept we've in Web service WSDL. We can also author proxy class using .net 4.0 framework SDK tool:svcutil.exe In existing host configuration just Add behaviour tag under system.serviceModel and give behaviour.configuration under services node.
<behaviors>
<serviceBehaviors>
<behavior name="CustomerServiceMEXBehavior" >
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
Complete WCF Host configuration Setting
<configuration>
<system.serviceModel>
<services>
<service name="WcfCustomerService.CustomerService"
behaviorConfiguration = "CustomerServiceMEXBehavior" >
<endpoint address =""
binding="basicHttpBinding"
contract="WcfCustomerService.ICustomerService"/>
<host>
<baseAddresses>
<add baseAddress ="http://localhost:8081/CustomerService"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="CustomerServiceMEXBehavior" >
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Once above tags are added,again add service reference by right click on WCFClient project. Enter the address as http://localhost:8081/CustomerService and click on 'Go' button. Reference will be added successfully.
Consume WCF Service in Main Program. The BasicHttpBinding_ICustomerService can be found in app.config file.The main highlight of this implementation is that we consumed service using HTTP binding. In later part we cover consuming service through IIS and TCP binding.
Note:If we're using IIS as hosting platform then we do not require explicit Host console application like we've now.
using WCFClient.BasicHttpBindingService;
namespace WCFClient
{
class Program
{
static void Main(string[] args)
{
try
{
using (CustomerServiceClient customerServiceClient = new CustomerServiceClient("BasicHttpBinding_ICustomerService"))
{
Console.WriteLine("***** Console Based WCF Client Started *****");
Customer customer = customerServiceClient.GetCustomerDetails(1);
Console.WriteLine("***** Consume WCF Service *****");
Console.WriteLine("HTTP Binding WCF Service Response :{0}", customer.CustomerName);
Console.ReadLine();
}
}
catch (Exception ex)
{
Console.WriteLine("HTTP Binding WCF Service Response ERROR:{0}", ex);
}
}
}
}
This configuration will be created by default once we add service reference successfully.
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ICustomerService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8081/CustomerService" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_ICustomerService" contract="BasicHttpBindingService.ICustomerService"
name="BasicHttpBinding_ICustomerService" />
</client>
</system.serviceModel>
</configuration>
Now built and run the WCFClient Console application .CTRL+F5 . So with above steps and troubleshooting we are able to create WCF Service,WCF Hosting Engine and WCF Client.
***WCF Service -WCF Host-WCF Client Layers Snapshot
***Run WCFClient :Output
In above example we configured HTTP binding in endpoints .Now we'll configure TCP binding. The objective is that client can consumed services through muiltple binding at a time.We can include multiple binding information in Host Console application's configuration file.
<endpoint address ="net.tcp://localhost:8090/CustomerService"
binding="netTcpBinding"
contract="WcfCustomerService.ICustomerService"/>
<host>
<baseAddresses>
<add baseAddress ="http://localhost:8081/CustomerService"/>
<add baseAddress ="net.tcp://localhost:8090/CustomerService"/>
</baseAddresses>
</host>
<configuration>
<system.serviceModel>
<services>
<service name="WcfCustomerService.CustomerService"
behaviorConfiguration = "CustomerServiceMEXBehavior" >
<endpoint address =""
binding="basicHttpBinding"
contract="WcfCustomerService.ICustomerService"/>
<endpoint address ="net.tcp://localhost:8090/CustomerService"
binding="netTcpBinding"
contract="WcfCustomerService.ICustomerService"/>
<host>
<baseAddresses>
<add baseAddress ="http://localhost:8081/CustomerService"/>
<add baseAddress ="net.tcp://localhost:8090/CustomerService"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="CustomerServiceMEXBehavior" >
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration >
Built and run the WCFHost.exe again.CTRL+F5 .Keep this service running so that we can take service reference of this in WCFClient.exe
***Run WCFHost.exe
Go to WCFClient.exe and update service reference(BasicHttpBindingService) file.The dialog box appear ,add TCP address. Once we update following tags will be added as default in WCFClient.exe app.config file
<netTcpBinding>
<binding name="NetTcpBinding_ICustomerService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog="10"
maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10"
maxReceivedMessageSize="65536">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Transport">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8081/CustomerService" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_ICustomerService" contract="BasicHttpBindingService.ICustomerService"
name="BasicHttpBinding_ICustomerService" />
<endpoint address="net.tcp://localhost:8091/CustomerService"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_ICustomerService"
contract="BasicHttpBindingService.ICustomerService" name="NetTcpBinding_ICustomerService">
</endpoint>
</client >
***Update WCF Service Request In WCFClient
Invoke WCFService through TCP binding using below code.
namespace WCFClient
{
class Program
{
static void Main(string[] args)
{
try
{
using (CustomerServiceClient customerServiceClient = new CustomerServiceClient("BasicHttpBinding_ICustomerService"))
{
Console.WriteLine("***** Console Based WCF Client Started *****");
Customer customer = customerServiceClient.GetCustomerDetails(1);
Console.WriteLine("***** Consume WCF Service *****");
Console.WriteLine("HTTP Binding WCF Service Response :{0}", customer.CustomerName);
}
using (CustomerServiceClient customerServiceClient = new CustomerServiceClient("NetTcpBinding_ICustomerService"))
{
Console.WriteLine("***** Console Based WCF Client Started *****");
Customer customer = customerServiceClient.GetCustomerDetails(1);
Console.WriteLine("***** Consume WCF Service *****");
Console.WriteLine("TCP Binding WCF Service Response :{0}", customer.CustomerName);
}
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("HTTP/TCP Binding WCF Service Response ERROR:{0}", ex);
}
}
}
}
Select WCFClient and CTRL+F5
***Run WCFClient :Output
If we're using IIS as host engine then no need to create explicit console or window service to run continuous WCF Service. No Host application here. So you must be thinking where can we mention my endpoints between host and Client.In previous example host application contains endpoint information for client to consume service.In IIS as hosting engine we can specify endpoint information in WCFService configuration itself. i.e Web.config file of WCF Service Application.
Select WCFCustomerService and rightclick and select Property menu. In VS2010 we get options to create virtual directory for the WCFCustomerService.
If we're using IIS platform then there is no need of host package.In such cases we need to configure endpoint in server itself. Add below tags in WCFCustomerService package configuration file itself.
<services>
<service name="WcfCustomerService.CustomerService">
<endpoint address =""
binding="basicHttpBinding"
contract="WcfCustomerService.ICustomerService"/>
</service>
</services>
Note: The configuration file for Service,Host and Client are almost similar.This configuration can be done using tool available in VS2010 but having hands -on will help in understand the concept.
Once virtual directory is created and endpoints are mentioned in WCFCustomerService Package, add service reference in WCFClient.exe for hosted services in IIS.
***Multiple Binding In Service Reference
The below endpoints and bindings gets added in WCFClient configuration file by default. There is no manual addition of tags here.
Note: If IIS is set for windows Authentication then in below configuration set Windows as <transport clientCredentialType="Windows" > <security mode="TransportCredentialOnly"> under basicHttpBinding
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ICustomerService1" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</bindings>
</basicHttpBinding>
<client>
<endpoint address="http://localhost:8081/CustomerService" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_ICustomerService" contract="BasicHttpBindingService.ICustomerService"
name="BasicHttpBinding_ICustomerService" />
<endpoint address="net.tcp://localhost:8091/CustomerService"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_ICustomerService"
contract="BasicHttpBindingService.ICustomerService" name="NetTcpBinding_ICustomerService" />
<endpoint address="http://10.10.10.10/WcfCustomerService1/CustomerService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICustomerService1"
contract="IISHostCustomerService.ICustomerService" name="BasicHttpBinding_ICustomerService1" />
</client>
One must include specific references in case of multiple bindings.
using WCFClient.BasicHttpBindingService;
using WCFClient.IISHostCustomerService;
namespace WCFClient
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("...HTTP BINDING...");
using (BasicHttpBindingService.CustomerServiceClient customerServiceClient1 =
new BasicHttpBindingService.CustomerServiceClient("BasicHttpBinding_ICustomerService"))
{
Console.WriteLine("***** Console Based WCF Client Started *****");
BasicHttpBindingService.Customer customer = customerServiceClient1.GetCustomerDetails(1);
Console.WriteLine("***** Consume WCF Service *****");
Console.WriteLine("HTTP Binding WCF Service Response :{0}", customer.CustomerName);
}
Console.WriteLine("...");
Console.WriteLine("...TCP BINDING...");
using (BasicHttpBindingService.CustomerServiceClient customerServiceClient2 =
new BasicHttpBindingService.CustomerServiceClient("NetTcpBinding_ICustomerService"))
{
Console.WriteLine("***** Console Based WCF Client Started *****");
BasicHttpBindingService.Customer customer = customerServiceClient2.GetCustomerDetails(1);
Console.WriteLine("***** Consume WCF Service *****");
Console.WriteLine("TCP Binding WCF Service Response :{0}", customer.CustomerName);
}
Console.WriteLine("...");
Console.WriteLine("...IIS HTTP BINDING...");
using (IISHostCustomerService.CustomerServiceClient customerServiceClient3 = new IISHostCustomerService.CustomerServiceClient("BasicHttpBinding_ICustomerService1"))
{
Console.WriteLine("***** Console Based WCF Client Started *****");
IISHostCustomerService.Customer customer = customerServiceClient3.GetCustomerDetails(1);
Console.WriteLine("***** Consume WCF Service *****");
Console.WriteLine("IIS HTTP Binding WCF Service Response :{0}", customer.CustomerName);
}
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("HTTP/TCP Binding WCF Service Response ERROR:{0}", ex);
}
}
}
}
***Run WCFClient :Output
Add any service reference as given above.
Add Service Reference In WCFClient
Click on Advance button as given in the screen shot below.
Select Aysnchronous options under client section and click ok ,close the dialog box. Configuration for END Points will be added by default in WCFClient App.config file
<client>
<endpoint address="net.tcp://localhost:8091/CustomerService"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_ICustomerService1"
contract="AsynchronousCallWCFService.ICustomerService" name="NetTcpBinding_ICustomerService1">
</endpoint>
</client>
Invoke Asynchronous Call to WCF Service: WCFClient
namespace WCFClient
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("...");
Console.WriteLine("...Async Calling of WCF Service...");
Console.WriteLine("***** The Async Customer Client *****\n");
using (AsynchronousCallWCFService.CustomerServiceClient asynProxyService =
new AsynchronousCallWCFService.CustomerServiceClient("NetTcpBinding_ICustomerService1"))
{
asynProxyService.Open();
IAsyncResult asyncResult = asynProxyService.BeginGetServerResponse(786,
strResult =>
{
Console.WriteLine
("Get Server Response = {0}", asynProxyService.EndGetServerResponse(strResult));
},
null);
while (!asyncResult.IsCompleted)
{
Thread.Sleep(100);
Console.WriteLine("In Progress...");
}
}
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine
("Aysnhcronous WCFClient Server Response ERROR:{0}", ex);
}
}
}
}
Run the WCFClient.exe and you can see the response from WCFCustomerService. My whole idea was to represent the simplest cases that can help developer to built WCF fabrics in their application. Initially I ran pillars to post to do it by myself but it took me a lot to understand the concept and implementation. Thought of writing it as a demo and helping folks who would be interested to try simple WCF development.
I hope novice learner will able to grasp usuage of WCF service with ease and less issues.Any suggestions and corrections are most welcome.