Wednesday, September 29, 2010

How to allow wcf client to get WCF service exception details

There are two ways to allow client to get the wcf exption details

IncludeExceptionDetailInFaults

one is by adding the "includeExceptionDetailInFaults" in serviceDebug tag

< behaviors >
< serviceBehaviors >
< behavior name="WCFSampleServiceBehaviors" >
< serviceMetadata httpGetEnabled="true" / >
< !-- The tag below allow to get the exception details from client -- >
< serviceDebug includeExceptionDetailInFaults="true"/ >

< serviceCredentials >
< userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WcfService1.CustomUserNameValidator,WcfService1"/ >
< /serviceCredentials >
< /behavior >
< behavior name="ServiceBehavior" >
< serviceSecurityAudit auditLogLocation="Application" serviceAuthorizationAuditLevel="SuccessOrFailure"
messageAuthenticationAuditLevel="SuccessOrFailure" / >
< /behavior >
< /serviceBehaviors >
< /behaviors >

Second option is adding service behavior "IncludeExceptionDetailInFaults=true"
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class WCFSampleService : IWCFSampleService
{

}

Saturday, September 18, 2010

WCF question answer

What is ABC of WCF ?

A : Address (Where is the service?)
B : Binding (How do i talk to service?)
C : Contracts (What service can do/perform ?)

Address : Specifies the location of the service which will be like http://Myserver/MyService.Clients will use this location to communicate with our service.

Binding : Specifies how the two paries will communicate in term of transport and encoding and protocols

Contract : Specifies the interface between client and the server.It's a simple interface with some attribute.

there are three types of contract in WCF.
1. DataContract
2. OperationContract
3. ServiceContract

What is endpoint ?
End point is the address where Service resides.


What is the difference between web service and wcf ?
1. Asp.net web services can use only HTTP channel, when WCF support Tcp, Http & msmq
2. Web service can hosted on IIS only, when WCF can be hosted on IIS, Was, self hosting
3. With WCF you can use SSL to encrypt the communication (to do that in ASMX you need to use WSE - Web Services Enhancements), you can send big files and securely (to do that in ASMX you need to use MTOM - Message Transmission Optimization Mechanism)


WCF Security related question & answer
http://msdn.microsoft.com/en-us/library/ff649839.aspx

Wcf Security implementation
http://www.codeguru.com/csharp/.net/net_general/netmyservices/article.php/c13313

Thursday, September 16, 2010

LINQ to SQL implementation step by step

LINQ to SQL implementation step by step

Right click on project solution => Add New Item =>
Select DBML class, give some appropriate name and add in project.

now open your server explorer and add some database. => drag some table on the dbml file.

// this is the class generated by hr.dbml
HRDataContext hr = new HRDataContext();

// get any one table
var q = from a in hr.GetTable()
select a;

// bind the table in datagrid
GridView1.DataSource = q;
GridView1.DataBind();

Data LINQ examples

To use LINQ (Language Integrated Query) you just need to have the following 2 namespace reference
using System.Collections.Generic;
using System.Linq;


// using where clasue within the collection.
List allCities = Common.GetAllCities().Where(c => c.DewPoint> 1).ToList() ;

// using where clasue & some property contain value search.
List allCities1 = Common.GetAllCities().Where(c => c.StationName.Contains("a")).ToList() ;

// pick the default value collection
Climate climate = Common.GetAllCities().SingleOrDefault();

// pick the First/default value collection
Climate climate1 = Common.GetAllCities().FirstOrDefault();

// sort the list
Common.GetAllCities().Sort();

Read xml data using Xml.Linq to generic list

using Xml.Linq.XDocument is very easy. now lets see how to pick the following xml file into xdocument and query with in the filedata using XML.Linq

< ?xml version="1.0" encoding="utf-8" ? >
< Cities >
< City StationName="Mumbai" >
< WeatherCondition >Not Extreem< /WeatherCondition >
< WindDirection >North-to-South< /WindDirection >
< Temperature >30 degree C< /Temperature >
< DewPoint >16< /DewPoint >
< Humidity >14.05< /Humidity >
< WindSpeed >Mild< /WindSpeed >
< /City >
< City StationName="Kolkata" >
< WeatherCondition >Dry< /WeatherCondition >
< WindDirection >East-to-South< /WindDirection >
< Temperature >32 degree C< /Temperature >
< DewPoint >11< /DewPoint >
< Humidity >12.35< /Humidity >
< WindSpeed >Strong< /WindSpeed >
< /City >
< City StationName="Delhi" >
< WeatherCondition >Hot< /WeatherCondition >
< WindDirection >East-to-North< /WindDirection >
< Temperature >42 degree C< /Temperature >
< DewPoint >7< /DewPoint >
< Humidity >11.35< /Humidity >
< WindSpeed >Strong< /WindSpeed >
< /City >
< /Cities >

Now read the above xml data using Xml.Linq.XDocument

public static List GetAllCities()
{
// Load a Xml File into XDocument class
XDocument xmlDoc = XDocument.Load(@"App_Data/CitiesClimate.xml");

// Read the Xml data into a
var cities = from xmlData in xmlDoc.Elements("Cities").Elements("City")
select xmlData;

Climate climate = null;
List climateList = new List();

//Now loop through all the cities in resultset
foreach (var city in cities)
{
climate = new Climate();
climate.StationName = city.Attribute("StationName").Value;
climate.WeatherCondition = city.Element("WeatherCondition").Value;
climate.WindDirection = city.Element("WindDirection").Value;
climate.Temperature = city.Element("Temperature").Value;
climate.DewPoint = Convert.ToDecimal(city.Element("DewPoint").Value) ;
climate.Humidity =Convert.ToDecimal(city.Element("Humidity").Value);
climate.WindSpeed = city.Element("WindSpeed").Value;
climateList.Add(climate);
}

return climateList;
}



Now see how to search some records
// Here we can perform search
var cities1 = from xmlData in xmlDoc.Elements("Cities").Elements("City")
where Convert.ToInt32(xmlData.Elements("DewPoint")) > 11
select xmlData;