As we know WCF supports various hosting options. Self Hosting is one of them.
What is Self Hosting?
WCF allows hosting a service in an application such as Console Application.
9.
14. Click on Add and select serviceMetaData
15. Select serviceMetaData from left side and set HttpGetEnabled to True.
16. Select Service name from left side and select BehaviorConfiguration from drop down. Refer highlighted section in below screen.
17. Now close the WCF Config Editor, and it will prompt to save. Save it.
18. Now your new App.Config file will look like this.
22. The host is running now; let’s try to add service reference once again.
23. Now you can see, service was found.
What is Self Hosting?
WCF allows hosting a service in an application such as Console Application.
- Create a Console Application Project to host a WCF Service.
- Add WCF Service project Reference in this Console Application.
- Delete existing App.config file.
- Add new App.config file.
- Rightclick on App.Config and select Edit WCF Configuration.
7. Browse for Bin->Debug location (Make sure you have built the console application successfully.)
8.
9.
10.
13. Now go for Service Behavior
14. Click on Add and select serviceMetaData
15. Select serviceMetaData from left side and set HttpGetEnabled to True.
16. Select Service name from left side and select BehaviorConfiguration from drop down. Refer highlighted section in below screen.
17. Now close the WCF Config Editor, and it will prompt to save. Save it.
18. Now your new App.Config file will look like this.
19. Now write code in Program.cs
static void Main(string[] args)
{
using (ServiceHost myhost = new ServiceHost(typeof(WcfServiceLibraryMaster.Product)))
{
myhost.Open();
Console.WriteLine(“Host Started Successfully…”);
Console.ReadLine();
}
}
Now remember this is your service url:
20. Now create a client to consume this WCF service. Add a service reference.
22. The host is running now; let’s try to add service reference once again.
23. Now you can see, service was found.
24. Change the namespace to WCFSelfServiceRef and write below code.
WCFSelfServiceClient.WCFSelfServiceRef.ProductClient objClient = new WCFSelfServiceRef.ProductClient();
string message = objClient.GetProduct(1001);
Console.Write(message);
Console.ReadLine();
25. Execute the code and you will see the output like below.
Related blogs to WCF Service-
- Introduction to WCF
- ABC of WCF
- Basics of WCF Architecture
- WCF vs Web Service
- What is XML serialization?
- WCF Binding
- Create first WCF application
- Fault Contract in
- WCF Data Contract vs Message Contract
- Message Contract
- Data Contract Serialization and De-Serialization
- Data Contract in WCF
- Operation Contract in WCF
- Service Contract in WCF
- How to host a WCF service in IIS?
- WAS Hosting in WCF
- Self Hosting in WCF
- How to create WCF RESTful Service.