Windows Communication Foundation (Code named Indigo) is a programming platform and runtime system for building, configuring and deploying network-distributed services. It is the latest service oriented technology; Interoperability is the fundamental characteristics of WCF. It is unified programming model provided in .Net Framework 3.0. WCF is a combined features of Web Service, Remoting, MSMQ and COM+. WCF provides a common platform for all .NET communication.
Below figures shows the different technology combined to form WCF.

Advantage
WCF is interoperable with other services when compared to .Net Remoting,where the client and service have to be .Net.
WCF services provide better reliability and security in compared to ASMX web services.
In WCF, there is no need to make much change in code for implementing the security model and changing the binding. Small changes in the configuration will make your requirements.
WCF has integrated logging mechanism, changing the configuration file settings will provide this functionality. In other technology developer has to write the code.
Disadvantage
Making right design for your requirement is little bit difficult. I will try to help you on solving these difficulties in the following article.
how to use WCF in C# with a simple example?
First add one blank solution.
Add new project to this

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 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:

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,

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

Now Click on the Services it will give you following screen,

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 .

Click on add button,
Select ServiceMetadata.
Go to top and do like the following screen.

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 ,

Just enable httpgetenabled so it will look like,

Again save and exit.
Now refresh the page again it is ready to work.
Your refreshed page now look like this,

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
No comments:
Post a Comment