WCF Tutorial



First add one blank solution.

Add 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,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.Runtime.Serialization;

namespace WcfServiceLibrary1
{
        [ServiceContract]
        public interface IMyClass
        {
            [OperationContract]
            string[] Answers(int a, int b);
        }
}

Implement that interface in the Myclass

The sample code is as given,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace WcfServiceLibrary1
{
    public class MyClass : IMyClass
    {
        #region IMyClass Members
 
        public string[] Answers(int a, int b)
        {
            string[] result = new string[5];
            if (a > b)
                result[0] = a.ToString() + " is greater than " + b.ToString();
            else if (a == b)
                result[0] = a.ToString() + " is Equal to " + b.ToString();
            else
                result[0] = a.ToString() + " is Smaller than " + b.ToString();
 
            result[1] = "Addition is " + (a + b).ToString();
            result[2] = "Multiplication is " + (a * b).ToString();
            if (a > b)
            {
                if (b != 0)
                {
                    result[3] = "Division is " + (a / b).ToString();
                }
                else
                {
                    result[3] = "Division is " + "infinite";
                }
            }
            else if (a < b)
            {
                if (a != 0)
                {
                    result[3] = "Division is " + (b / a).ToString();
                }
                else
                {
                    result[3] = "Division is " + "infinite";
                }
            }
            else
            {
                if (a != 0 && b != 0)
                {
                    result[3] = "Division is " + "1";
                }
                else
                {
                    result[3] = "Division is " + "infinite";
                }

            }
            if (a > b)
                result[4] = "Substraction is " + (a - b).ToString();
            else if (a < b)
                result[4] = "Substraction is " + (b - a).ToString();
            else
                result[4] = "Substraction is " + "0";

            return result;
        }
        #endregion
    }
}


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 end point 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

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ServiceReference1;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        MyClassClient obj = new MyClassClient("Basic");
        string[] datas = obj.Answers(5, 10);
        foreach (string data in datas)
        {
            Response.Write(data);
        }
    }
}


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

erver'>
Up Next
    Ebook Download
    View all
    Learn
    View all