Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 4157 |  Welcome, Guest!   Register  Login
 Home > Blogs > WCF > Easy steps to create a basic WCF service ...
Rajni.Shekhar

Easy steps to create a basic WCF service

 Blog author: Rajni.Shekhar | Posted on: 4/9/2012 | Category: WCF Blogs | Views: 2710 | Status: [Member] | Points: 75 | Alert Moderator   


"WCF (windows communication foundation) is a part of .net framework that provides unified programming model for rapidly building SOA (service oriented architecture) based application that communicates acrosss the network and the enterprise ( business )."

C - Contract (What) : It defines what functionality provided by the services to its clients.

A - Address (Where) : End Point of a service is an address where a service can be found.

B - Bindings (How) : it contains the information, How a client must communicate with the service.

Three basic steps to create a simple WCF service.

Step 1: Create contract service interface and class

  • Open Visual Studio 2008
  • Click on File to create new website a diloque box will be open.
  • Choose WCF service. set the location where you want to keep your solution file.
  • You will get App_Code folder, in appcode folder, there are one interface named IService.cs and one class file named Service.cs

ServiceContract attribute applied to Iservice interface.

IService.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

// NOTE: If you change the interface name "IService" here, you must also update the reference to "IService" in Web.config.

[ServiceContract]

public interface IService

{

[OperationContract]

string GetData(int value);

[OperationContract]

CompositeType GetDataUsingDataContract(CompositeType composite);

// TODO: Add your service operations here

}

// Use a data contract as illustrated in the sample below to add composite types to service operations.

[DataContract]

public class CompositeType

{

bool boolValue = true;

string stringValue = "Hello ";

[DataMember]

public bool BoolValue

{

get { return boolValue; }

set { boolValue = value; }

}

[DataMember]

public string StringValue

{

get { return stringValue; }

set { stringValue = value; }

}

}

Service.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

// NOTE: If you change the class name "Service" here, you must also update the reference to "Service" in Web.config and in the associated .svc file.

public class Service : IService

{

public string GetData(int value)

{

return string.Format("You entered: {0}", value);

}

public CompositeType GetDataUsingDataContract(CompositeType composite)

{

if (composite.BoolValue)

{

composite.StringValue += "Suffix";

}

return composite;

}

}

Step 2: Address of your service in UI (website) web.config file

There is file Service.svc in your solution

<%@ ServiceHost Language="C#" Debug="true" Service="Service" CodeBehind="~/App_Code/Service.cs" %>

View it in browser to check it is working or not.

Copy the URL of your WCF service.

Create your website to consume the WCF Service.

In your website, add service reference to consume service.

in web.config file you will see automatically added end-point (Address) of your WCF service

<endpoint address="http://localhost:3668/WCFService1/Service.svc"

binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService"

contract="Service.IService" name="WSHttpBinding_IService">

<identity>

<dns value="localhost" />

</identity>

</endpoint>

 

And Binding section in web.config file

<bindings>

<wsHttpBinding>

<binding name="WSHttpBinding_ITestService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">

<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>

<reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false"/>

<security mode="Message">

<transport clientCredentialType="Windows" proxyCredentialType="None" realm=""/>

<message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default" establishSecurityContext="true"/>

</security>

</binding>

</wsHttpBinding>

</bindings>

Your web.config file of UI website will look like.

<system.serviceModel>

<bindings>

<wsHttpBinding>

<binding name="WSHttpBinding_IService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">

<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>

<reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false"/>

<security mode="Message">

<transport clientCredentialType="Windows" proxyCredentialType="None" realm=""/>

<message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default" establishSecurityContext="true"/>

</security>

</binding>

</wsHttpBinding>

</bindings>

<client>

<endpoint address="http://localhost:3667/WCFService1/Service.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService" contract="Service.IService" name="WSHttpBinding_IService">

<identity>

<dns value="localhost"/>

</identity>

</endpoint>

</client>

</system.serviceModel>

In your website (UI).

Default.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.ServiceModel;

using MyService;

public partial class _Default : System.Web.UI.Page

{

ServiceClient objSVC = null;

protected void Page_Load(object sender, EventArgs e)

{

objSVC = new ServiceClient();

string str = objSVC.GetData(20);

CompositeType objComposite =new CompositeType();

objComposite=objSVC.GetDataUsingDataContract(objComposite);

Response.Write("GetData function will return:" + str);

Response.Write("<BR>");

Response.Write("GetDataUsingDataContract will return string vale: " + objComposite.StringValue);

Response.Write("<BR>");

Response.Write("GetDataUsingDataContract will return bool vale: " + objComposite.BoolValue);

}

}

 




Thanks,
Rajni Shekhar
Found interesting? Add this to:



 More Blogs from Rajni.Shekhar

 More ...

Experience:6 year(s)
Home page:
Member since:Friday, March 16, 2012
Level:Bronze
Status: [Member]
Biography:having 6 years of experience of IT industry, involved in dot net technology.
>> Write Response - Respond to this post and get points

More Blogs

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/19/2013 8:17:02 PM