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. namespace WcfServiceLibrary1
  8. {
  9. [ServiceContract]
  10. public interface IMyClass
  11. {
  12. [OperationContract]
  13. string[] Answers(int a, int b);
  14. }
  15. }
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. namespace WcfServiceLibrary1
  6. {
  7. public class MyClass : IMyClass
  8. {
  9. #region IMyClass Members
  10. public string[] Answers(int a, int b)
  11. {
  12. string[] result = new string[5];
  13. if (a > b)
  14. result[0] = a.ToString() + " is greater than " + b.ToString();
  15. else if (a == b)
  16. result[0] = a.ToString() + " is Equal to " + b.ToString();
  17. else
  18. result[0] = a.ToString() + " is Smaller than " + b.ToString();
  19. result[1] = "Addition is " + (a + b).ToString();
  20. result[2] = "Multiplication is " + (a * b).ToString();
  21. if (a > b)
  22. {
  23. if (b != 0)
  24. {
  25. result[3] = "Division is " + (a / b).ToString();
  26. }
  27. else
  28. {
  29. result[3] = "Division is " + "infinite";
  30. }
  31. }
  32. else if (a < b)
  33. {
  34. if (a != 0)
  35. {
  36. result[3] = "Division is " + (b / a).ToString();
  37. }
  38. else
  39. {
  40. result[3] = "Division is " + "infinite";
  41. }
  42. }
  43. else
  44. {
  45. if (a != 0 && b != 0)
  46. {
  47. result[3] = "Division is " + "1";
  48. }
  49. else
  50. {
  51. result[3] = "Division is " + "infinite";
  52. }
  53. }
  54. if (a > b)
  55. result[4] = "Substraction is " + (a - b).ToString();
  56. else if (a < b)
  57. result[4] = "Substraction is " + (b - a).ToString();
  58. else
  59. result[4] = "Substraction is " + "0";
  60. return result;
  61. }
  62. #endregion
  63. }
  64. }
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. public partial class _Default : System.Web.UI.Page
  14. {
  15. protected void Page_Load(object sender, EventArgs e)
  16. {
  17. MyClassClient obj = new MyClassClient("Basic");
  18. string[] datas = obj.Answers(5, 10);
  19. foreach (string data in datas)
  20. {
  21. Response.Write(data);
  22. }
  23. }
  24. }
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!