What is WCF?
WCF is a unification technology, which unites .Net Remoting, MSMQ, Web Services
and COM+ technologies.
WCF provides a runtime environment for your services, enabling you to expose CLR
types as services and to consume other services as CLR types.
Creating WCF Service:
Open Visual Studio 2008/2010. Click on File menu -> New -> Project ->WCF Service
Application. Change the name as "WCFService" then click Ok button.
Delete auto generated files IService1.cs and Service1.svc and create new (right
click on WCFService project and select Add -> New Item -> WCF Service).
Rename service as "UserService".
Now add an Entity Data Model in your project to communicate with database.
Service Code
Replace interface IUserService with the code given below :
using
System;
using
System.Data;
using
System.Collections.Generic;
using
System.Linq;
using
System.Runtime.Serialization;
using
System.ServiceModel;
using
System.Text;
namespace
WCFService
{
[ServiceContract]
public interface
IUserService
{
[OperationContract]
string Authenticate(string
UserName, string A_CryptKey);
[OperationContract]
DataTable GetApprovedUser();
}
}
Replace UserService class code with the code given below :
using
System;
using
System.Data;
using
System.Collections.Generic;
using
System.Linq;
using
System.Runtime.Serialization;
using
System.ServiceModel;
using
System.Text;
namespace
WCFService
{
public class
UserService :
IUserService
{
public string
Authenticate(string UserName,
string A_CryptKey)
{
using (var
Context = new
UsersEntities())
{
var user = Context.Users.Where(c
=> c.UserName == UserName && c.A_CryptKey == A_CryptKey).FirstOrDefault();
if (user !=
null)
return
Convert.ToString(user.UserID) +
"," + Convert.ToString(user.B_CryptKey);
else
return
string.Empty;
}
}
public DataTable
GetApprovedUser()
{
using (var
Context = new
UsersEntities())
{
DataTable objdt =
new DataTable();
objdt = Context.Users.Where(c => c.IsApproved ==
true).ToList().CopyToDataTable();
return objdt;
}
}
}
}
Service configuration
<?xml
version="1.0"
encoding="utf-8"?>
<configuration>
<system.web>
<compilation
debug="true"
targetFramework="4.0">
<assemblies>
<add
assembly="System.Data.Entity,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
/>
</assemblies>
</compilation>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!--
To avoid disclosing metadata information, set the value below to false and
remove the metadata endpoint above before deployment
-->
<serviceMetadata
httpGetEnabled="true"
/>
<!--
To receive exception details in faults for debugging purposes, set the value
below to true. Set to false before deployment to avoid disclosing exception
information
-->
<serviceDebug
includeExceptionDetailInFaults="false"
/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment
multipleSiteBindingsEnabled="true"
/>
</system.serviceModel>
<system.webServer>
<modules
runAllManagedModulesForAllRequests="true"
/>
</system.webServer>
<connectionStrings>
<add
name="UsersEntities"
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider
connection string="Data
Source=ServerName;Initial Catalog=Favourites;Persist Security Info=True;User ID=UserName;Password=Password;MultipleActiveResultSets=True""
providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
Consuming WCF Service:
Now right click on UserService.svc and select "View in Browser". it will show a
browser with service url. Copy that url
Now right click on WCFClient project select "Add Service Reference" and paste
copied url in Address section of "Add Service Reference" dialog box and click on
Go button and rename Namespace as "UserService" and click ok button.
WCF Client code
using
System;
using
System.Data;
using
System.Collections.Generic;
using
System.Linq;
using System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
UserService;
public
partial class
_Default : System.Web.UI.Page
{
protected void
Page_Load(object sender,
EventArgs e)
{
UserServiceClient proxy =
new
UserServiceClient();
string User = proxy.Authenticate("Mukesh",
"X%AY#$tZ");
DataTable objdt =
new DataTable();
objdt = proxy.GetApprovedUser();
proxy.Close();
}
}
Client side configuration
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding
name="BasicHttpBinding_IUserService"
closeTimeout="00:01:00"
openTimeout="00:01:00"
receiveTimeout="00:10:00"
sendTimeout="00:01:00"
allowCookies="false"
bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536"
maxBufferPoolSize="524288"
maxReceivedMessageSize="65536"
messageEncoding="Text"
textEncoding="utf-8"
transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas
maxDepth="32"
maxStringContentLength="8192"
maxArrayLength="16384"
maxBytesPerRead="4096"
maxNameTableCharCount="16384"
/>
<security
mode="None">
<transport
clientCredentialType="None"
proxyCredentialType="None"
realm=""
/>
<message
clientCredentialType="UserName"
algorithmSuite="Default"
/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint
address="http://localhost:57728/UserService.svc"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IUserService"
contract="UserService.IUserService"
name="BasicHttpBinding_IUserService"
/>
</client>
</system.serviceModel>