WCF Tutorial

First, add one blank solution.
 
Add a new project to this
 
wcf1.gif 
 
Select "WCF Service Library" as shown in above figure.
 
Delete the two files IService1.cs and Service1.cs
 
Now add two classes with the names IMyClass.cs and MyClass.cs
 
Now your solution should look like:
 
wcf2.gif 
 
Now define the service contract and operation contract in iMyclass
 
The sample code is as given,
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.ServiceModel;  
  6. using System.Runtime.Serialization;  
  7.   
  8. namespace WcfServiceLibrary1  
  9. {  
  10.         [ServiceContract]  
  11.         public interface IMyClass  
  12.         {  
  13.             [OperationContract]  
  14.             string[] Answers(int a, int b);  
  15.         }  
  16. }  
Implement that interface in the Myclass
 
The sample code is as given,
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.    
  6. namespace WcfServiceLibrary1  
  7. {  
  8.     public class MyClass : IMyClass  
  9.     {  
  10.         #region IMyClass Members  
  11.    
  12.         public string[] Answers(int a, int b)  
  13.         {  
  14.             string[] result = new string[5];  
  15.             if (a > b)  
  16.                 result[0] = a.ToString() + " is greater than " + b.ToString();  
  17.             else if (a == b)  
  18.                 result[0] = a.ToString() + " is Equal to " + b.ToString();  
  19.             else  
  20.                 result[0] = a.ToString() + " is Smaller than " + b.ToString();  
  21.    
  22.             result[1] = "Addition is " + (a + b).ToString();  
  23.             result[2] = "Multiplication is " + (a * b).ToString();  
  24.             if (a > b)  
  25.             {  
  26.                 if (b != 0)  
  27.                 {  
  28.                     result[3] = "Division is " + (a / b).ToString();  
  29.                 }  
  30.                 else  
  31.                 {  
  32.                     result[3] = "Division is " + "infinite";  
  33.                 }  
  34.             }  
  35.             else if (a < b)  
  36.             {  
  37.                 if (a != 0)  
  38.                 {  
  39.                     result[3] = "Division is " + (b / a).ToString();  
  40.                 }  
  41.                 else  
  42.                 {  
  43.                     result[3] = "Division is " + "infinite";  
  44.                 }  
  45.             }  
  46.             else  
  47.             {  
  48.                 if (a != 0 && b != 0)  
  49.                 {  
  50.                     result[3] = "Division is " + "1";  
  51.                 }  
  52.                 else  
  53.                 {  
  54.                     result[3] = "Division is " + "infinite";  
  55.                 }  
  56.   
  57.             }  
  58.             if (a > b)  
  59.                 result[4] = "Substraction is " + (a - b).ToString();  
  60.             else if (a < b)  
  61.                 result[4] = "Substraction is " + (b - a).ToString();  
  62.             else  
  63.                 result[4] = "Substraction is " + "0";  
  64.   
  65.             return result;  
  66.         }  
  67.         #endregion  
  68.     }  
  69. }  
Now build the solution. It is necessary that your solution will build; if it doesn't then fix the error(s) then re-build.
 
Now add one more project to the solution as given below:
 
wcf3.gif 
 
Again delete the two files Service1.svc.cs and IService1.cs
 
Add the reference of the DLL which we have generated by building.
 
Open the service1.svc file and make the changes to make similar to the following line
 
<%@ ServiceHost Language="C#" Debug="true" Service="WcfServiceLibrary1.MyClass" CodeBehind="MyClass.cs %>
 
Save it.
 
Now right click and select View in browser. It will show an error. No problem just take the URL.
 
For me, it gave the URL: http://localhost:54640/Service1.svc
 
Now right click on web.config and select "Edit WCF Configuration"
 
Delete the Endpoints (whatever you have) then also delete the service.
 
Now, click on the "Create a new Service".
 
Click on the browse button --> Go to bin folder. You will find two Dlls over there.
 
Double click both one by one so you can be given this screen,
 
wcf4.gif 
 
Select That. And click on next.
 
Select the contract by repeating above step generally, it has been given. Click next button.
 
Select HTTP and click next. Again next.
 
Now It will ask for the address give the address which we have got I am giving this address http://localhost:54640/Service1.svc
 
Proceed ahead and click finish.
 
In the endpoint section, Give Name Basic
 
wcf5.gif 
 
Now Click on the Services it will give you following screen,
 
wcf6.gif 
 
Click on link says "click to create"
 
Again give the name "Basic"
 
Now go to advanced => Service Behaviour.
 
Which Gives you following screen
 
Right click on service behavior and add new.
 
wcf7.gif
 
Click on add button,
 
Select ServiceMetadata.
 
Go to top and do like the following screen.
 
wcf8.gif
 
Just save now and exit.
 
Refresh the url now it will gives you something instead of error.
 
Actually I have forgotten one setting no problem we will do that now
 
Again edit web.config by right clicking as done previously.
 
Try to reach following screen
 
wcf9.gif
 
Just enable httpgetenabled so it will look like,
 
wcf10.gif
 
Again save and exit.
 
Now refresh the page again it is ready to work.
 
Your refreshed page now look like this,
 
wcf11.gif
 
You can test the wcf from visual studio command prompt by writing
 
Wcftestclient http://localhost:54640/Service1.svc
 
Please provide your own url over here.
 
Now add website to our solution,
 
Add service reference give namespace that you want to give rest of the thing is simple as we do in the webservice.
 
Sample code to use this
  1. using System;  
  2. using System.Configuration;  
  3. using System.Data;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.HtmlControls;  
  9. using System.Web.UI.WebControls;  
  10. using System.Web.UI.WebControls.WebParts;  
  11. using System.Xml.Linq;  
  12. using ServiceReference1;  
  13.   
  14. public partial class _Default : System.Web.UI.Page   
  15. {  
  16.     protected void Page_Load(object sender, EventArgs e)  
  17.     {  
  18.   
  19.         MyClassClient obj = new MyClassClient("Basic");  
  20.         string[] datas = obj.Answers(5, 10);  
  21.         foreach (string data in datas)  
  22.         {  
  23.             Response.Write(data);  
  24.         }  
  25.     }  
  26. }  
For more details download the source code I have given to source demo solution as I have explained and one other download and check out that.
 
Cheers!