Create a WCF Service:
First we create a WCF Service Library if u intended to use it without IIS hosting or for Windows based application.In another case , We can choose WCF Service for IIS hosting & For Web Based Application.
We get readymade 1 interface called Iservice & a class Service.
We declare a method in Iservice Interface like “GetVal”.
namespace WcfServiceLibrary1
{
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in App.config.
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetVal();
}
}
Then Implement the above method in Class file as follow.
public class Service1 : IService1
{
public string GetVal()
{
return "Hello";
}
}
Now u simply created ur WCF Service.It’s now time to host this Service in a Console application , called Self Hosting in order to initiate the service.
Self Hosting of WCF Service on a Console Application:
Create a Console Application.
Add reference of System.ServiceModel
Add reference of WCf service that we need to host.
Add App.Config File.
Right click App.Comfig & select Edit WCF Configuration.
(If it’s not found then go for Tools -> WCF service Configutration Editor)
1. Create a new service
2. Specify the service type.Browse & find class already defined within the WCF service.
3. Specify the service Contract.Browse & find interface already defined within the WCF service.
4. Specify Binding. Like Http or MSMQ or net.TCP
5. Specify address like http://localhost:5010/Myservice1. or
U can specify an alias name in such case u should provide the endpoint address as
“Base Address” property later.
6. Finish & a service is created.
Now we know there are mainly two Tags those are need to define in <ServiceModel> Tag.
One is <Services><Service> Tag
AND
Second is <Behaviours><Behaviour> Tag
So, We can go for service End Point Tag & Behaviour Tag fixing as bellow.
B.Advanced
|->Service Behaviour -> New Behaviour à Specify Behaviour Name & -> ADD “serviceMetadata”
->specify Service Metadata Property -> httpGetEnabled = true AND
httpGetUrl="http://localhost:5010/myNewservice1
This httpGetURl should be absolute SVC address.
C.Services
|-> u can go for Host à specify Base Address à But u may or may not do this & service goping well.basically EITHER u mentioned Absolute address in EndPoint Address Property directly OR u specify a relative address in EndPoint Address & in such case u must either specify Absolute Address in Base Address Property or HttpGetUrl property in ServiceMetadata.But it basically vary from different binding protocols we used in Service Configuration & Hosting.
|->specify Endpoint Name
At the End Specify “Behaviour Configuration” in Services Option
Now u get the following App.config as we as u save the app.config generation wizard.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="MyserviceBehaviour">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:5010/myNewservice1"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="MyserviceBehaviour" name="WcfServiceLibrary1.Service1">
<endpoint address="http://localhost:5010/myNewservice1" binding="basicHttpBinding"
bindingConfiguration="" name="ServiceEndpoint1" contract="WcfServiceLibrary1.IService1" />
<!--<host>
<baseAddresses>
<add baseAddress="http://localhost:5010/myNewservice1" />
</baseAddresses>
</host>-->
</service>
</services>
</system.serviceModel>
</configuration>
We can Simply copy & Paste the above configuration based code in any App.config in Host End & jast altering the endpoint addresses for other service as per our requirement.
Now We have to start the Hosted Service by coded following lines in program.cs
ServiceHost sh = new ServiceHost(typeof(WcfServiceLibrary1.Service1));
sh.Open();
Console.WriteLine("Service is Running Press any key to stop the Service");
Console.ReadLine();
S.N.1) U can more than one Endpoint based on different binding protocols or more than one Service Behaviour as per requirement.
S.N.2) The HttpGetEnabled property of ServiceMetadataBehavior is set to true and the HttpGetUrl property is a relative address, but there is no http base address. Either supply an http base address or set HttpGetUrl to an absolute address.
In above example we comment Base Address Specification but set httpGetEnabled property in
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:5010/myNewservice1"/> .
Now U simply run the service & u get the following Console Screen

Create CLIENT Application to Consume WCF Service
Create a Windows Application.
Add Service Reference of of Newly created & hosted service by pasting it’s SVC path.
It shows internal Interfaces & Classes for consuming.
Give a name to the service.

Now u should paste complete endpoint absolute address in given field & get Interfaces & classes in services for consume.

U can change name of ServiceReference.
Now u simply code the following to call service method
private void button1_Click(object sender, EventArgs e)
{
ServiceReference1.Service1Client obj=new serviceReference1.Service1Client();
MessageBox.Show(obj.GetVal());
}
Here ServiceReference1 is namespace defined while service associate in client application end.
Service is name of class but it comes with suffix “client ” so it will be Serviceclient.
GetVal() is method that declared & Defined within WCF service.
Configuration of App.Config FOR net.TCP BINDING
Every steps for hosting net.TCP binding are same with basicHttp binding except , we need to mention “mextcpbinding ” as endpoint and it’s not allowed to Add Hosted Service through Add Service Reference Wizard directly rather than we have go through proxy creation by executing svcutil [complete svc path] in VS command promt and utilize Service1.cs & ouput.config file for Client Application Configuration.
We simply avoid service metadata property like httpgetenable or hetpgeturl because we need not this service in http mode.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="NewBehavior1">
<serviceMetadata/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="NewBehavior1" name="WcfServiceLibrary1.Service1">
<endpoint address="net.tcp://localhost:5035/mytcpservice" binding="netTcpBinding"
bindingConfiguration="" name="tcpendpoint" contract="WcfServiceLibrary1.IService1" />
<endpoint address="mex" binding="mexTcpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:5035/mytcpservice"/>
</baseAddresses>
</host>
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="PortSharingBinding"
portSharingEnabled="true">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
</system.serviceModel>
</configuration>
Configuration of App.Config FOR net.TCP & BasicHttp BINDING CONSUMING
Self Hosting of WCF Service on a Console Application for netTcp & BasicHttp:
Here we Go through wizard to configure App.config file for both http & netTcp Binding:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="NewBehavior1">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="NewBehavior1" name="WcfServiceLibrary1.Service1">
<endpoint address="http://localhost:5040/myhttpservice" binding="basicHttpBinding"
bindingConfiguration="" name="myhttpEndPoint" contract="WcfServiceLibrary1.IService1" />
<endpoint address="net.tcp://localhost:5041/mytcpservice" binding="netTcpBinding"
bindingConfiguration="" name="mytcpEnd" contract="WcfServiceLibrary1.IService1" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:5040/myhttpservice"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
Beside above Configuration,
EITHER u can do it in following way where we put an alias name in endpoit Address for htppbinding & specify Absolute address In httpgetUrl property in
ServiceMetadata Tag.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="NewBehavior1">
<serviceMetadata httpGetEnabled="true"
httpGetUrl="http://localhost:5040/myhttpservice />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="NewBehavior1" name="WcfServiceLibrary1.Service1">
<endpoint address="myhttpservice" binding="basicHttpBinding"
bindingConfiguration="" name="myhttpEndPoint" contract="WcfServiceLibrary1.IService1" />
<endpoint address="net.tcp://localhost:5041/mytcpservice" binding="netTcpBinding"
bindingConfiguration="" name="mytcpEnd" contract="WcfServiceLibrary1.IService1" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:5040/myhttpservice"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
Now U simply run the service & u get the following Console Screen

Create CLIENT Application to Consume WCF Service
Here First we create a Windows Application as Client.
Then Add ServiceModel reference to application.
We NOT Add Service Reference Directly through Wizard But We Create Proxy of Hosted Services.
After Creation Proxy i.e .cs file & Output.config file , we add Service Model Tag in Client Application App.config file & Add Service.cs page as Existing Item.
The Main thing is We create two Client Applications, one for Http utilization and another for netTcp utilization.
EVEN we have to create two proxy one for httpbinding another for netTcp Binding.Noticed that we have created different proxies for different bindings in different locations.
First we run below command for create proxy for httpbinding that create proxy means Service1.cs file & output.config in VC file directly.

AGAIN,
We have to run the following command for create proxy of netTcp service under vc/netTCP> file location.

Thus in Client that use httpBinding should take Service1.cs & output.config file from C:/Program Files/Microsoft Visual Studio 9/VC> AND Client that use netTcp Binding should take Service1.cs & output.config file from from C:/Program Files/Microsoft Visual Studio 9/VC/netTcp> location.
Therefore we simple add the following code as below to consume the service:
But because we are using more than one end point with different bindings so, we have to mention
Name of each endpoint while consuming services at Client Application End.
Like , in Application end that going to use Http Binding we mention the Endpoint name as “myhttpEndPoint” that we used in App.Config for basichttp binding.Obviously we should use Service1.cs that is actually generated proxy from svcutil utility using endpoint address like
“http://localhost:5040/myhttpservice” .
Service1Client obj = new Service1Client(“myhttpEndPoint”);
MessageBox.Show(obj.GetData());
AND
Like , in Application end that going to use netTcp Binding we mention the Endpoint name as “mytcpEnd” that we used in App.Config for basichttp binding.Obviously we should use Service1.cs that is actually generated proxy from svcutil utility using endpoint address like
“net.tcp://localhost:5041/mytcpservice” .
Service1Client obj = new Service1Client(“mytcpEnd”);
MessageBox.Show(obj.GetData());