Thursday, 19 December 2013

WCF Interview Questions

1. What is WCF also known as?

  WCF (Windows Communication Foundation) is also know an  Indigo by its code name.

2. Difference between WCF and Web Services?

Below are the main differences between the WCF and Web Service:
Web Service:
1.    Can be hosted in IIS only
2.    Only two types of operations affects- One-Way, Request-Response
3.     To serialize the data use System.Xml.Serialization
4.    To encode the data use- XML 1.0, MTOM, DIME, Custom

WCF service:
1.    Can be hosted in IIS, Self Hosting, WAS, Windows Services etc
2.    Three types of operations affects- One-Way, Request-Response and Duplex
3.     To serialize the data use System.Runtimel.Serialization
4.    To encode the data use- XML 1.0, MTOM,Binary, Custom
5.    WCF Service can be accessed through HTTP, TCP, Named pipes, MSMQ,P2P etc.

3. What are Endpoints?

 The collection of Address, Binding and Contract is called as End Point. In Sort,
EndPoint = A+B+C
Address (Where)-  it means where the service is hosted. URL of the service shows the address.
Binding (How)- How to connect to the service, is defined by the Binding. It basically has the definition of the communication channel to communicate to the WCF service
Contract (what)- It means what the service contains for the client. What all the methods are implemented in the WCF service is implemented in the Contract.

4. What are Behavior and Bindings?

Binding mainly describes about the communication of the client and service. For this, there are protocols corresponding to the binding behavior which will take care of the communication channel. There are different protocols which we use for the different types of bindings. E.g. HTTP, TCP, MSMQ, Named Pipes etc.
Behavior is used for the common configurations that could be for endpoints. When we use the common behavior, they affect to all the end points. Adding the service behavior affect the service related stuff while the endpoint related behavior affects the end points. Also operations level behavior affects the operations. 

5. What are different types of Contracts supported?

 There are mainly 5 type of contracts used in WCF service:
1. Service Contract
2. Operation Contract
3. Data Contract
4. Message Contract
5. Fault Contract

6. What is the difference between Transport and Message Security mode?

 WCF supports 2 types of security- Transport Level Security and Message Level Security
Transport Level Security- In this type of security, we make the transport channel as secure so that the data flows in that channel will be automatically secured. For HTTP channel, we use the client certificate for the security of the web address. SSL is used for the HTTP channel security. As we don’t need to secure each of the messages which are floating between the client and the service, the speed is faster as direct message is going to the client from the service.
Message level security- This type of security in WCF is used where we don’t have the fixed transport medium and we need to secure each message which is floating between the server and the client. In this type of security we use certain algorithms for making the message as secure message. We use some extra bits and send with the message. We also use some encryption techniques like SHA1 or MD5 which make the proper security for our message. As each message needs to be secured, this type of security makes some delay in the process of sending and receiving the messages.

7. How to configure WCF security to support Windows authentication?

To support the WCF security in Windows Authentication, we need to add the ClientCredetialType attribute to “Windows” under the security tab element:
transport clientCredentialType="Windows"

8. How to use Fault Contract?

 Fault Contract is mainly used for viewing and displaying the errors which occurred in the service. 
So it basically documents the error and the error message can be shown to the user in the understandable way.
We can’t use here the try….catch block for the error handling because the try…catch is the technology specific (.Net Technology). 
So we use the Fault contract for the error handling.

e.g. To use the Fault contract, we can simply write like the below:

public  int Add(int number1,int number2)
  // write some implementation
 throw new FaultException (“Error while adding data..”);

Here the fault Exception method is the inbuilt method which will throw the exception and display the message . We can use the custom class so that the message can be customized and the customized message can be sent to the client.

So we can creeat  a clss like:

Public Class CustomException()
public int ID{get;set;}
public string Message{get;set;}

public string Type{get;set;}

Now this custom type we ca use with the Operation Contract as:

Public interface IMyInterface
Int Add(int num1,int num2);

Now while implementation of the Add method, we can assign the class properties.

Sunday, 8 December 2013

Setting Default theme for appbar in windows phone 7

  var phoneAccentBrush = new SolidColorBrush((App.Current.Resources["PhoneAccentBrush"] as SolidColorBrush).Color);
            this.ApplicationBar.BackgroundColor = Color.FromArgb(phoneAccentBrush.Color.A, phoneAccentBrush.Color.R, phoneAccentBrush.Color.G, phoneAccentBrush.Color.B);

Sunday, 1 December 2013

Improve Database Performance

To Improve Data Base Performance we should remember the following

1. Choose Appropriate Data Type
Choose appropriate SQL Data Type to store your data since it also helps in to improve the query performance. Example: To store strings use varchar in place of text data type since varchar performs better than text. Use text data type, whenever you required storing of large text data (more than 8000 characters). Up to 8000 characters data you can store in varchar.
2. Avoid nchar and nvarchar
Practice to avoid nchar and nvarchar data type since both the data types takes just double memory as char and varchar. Use nchar and nvarchar when you required to store Unicode (16-bit characters) data like as Hindi, Chinese characters etc.
3. Avoid NULL in fixed-length field
Practice to avoid the insertion of NULL values in the fixed-length (char) field. Since, NULL takes the same space as desired input value for that field. In case of requirement of NULL, use variable-length (varchar) field that takes less space for NULL.
4. Avoid * in SELECT statement
Practice to avoid * in Select statement since SQL Server converts the * to columns name before query execution. One more thing, instead of querying all columns by using * in select statement, give the name of columns which you required.
1. -- Avoid
2. SELECT * FROM tblName
3. --Best practice
4. SELECT col1,col2,col3 FROM tblName
5. Use EXISTS instead of IN
Practice to use EXISTS to check existence instead of IN since EXISTS is faster than IN.
1. -- Avoid
2. SELECT Name,Price FROM tblProduct
3. where ProductID IN (Select distinct ProductID from tblOrder)
4. --Best practice
5. SELECT Name,Price FROM tblProduct
6. where ProductID EXISTS (Select distinct ProductID from tblOrder)
6. Avoid Having Clause
Practice to avoid Having Clause since it acts as filter over selected rows. Having clause is required if you further wish to filter the result of an aggregations. Don't use HAVING clause for any other purpose.
7. Create Clustered and Non-Clustered Indexes
Practice to create clustered and non clustered index since indexes helps in to access data fastly. But be careful, more indexes on a tables will slow the INSERT,UPDATE,DELETE operations. Hence try to keep small no of indexes on a table.
8. Keep clustered index small
Practice to keep clustered index as much as possible since the fields used in clustered index may also used in nonclustered index and data in the database is also stored in the order of clustered index. Hence a large clustered index on a table with a large number of rows increase the size significantly. Please refer the article Effective Clustered Indexes
9. Avoid Cursors
Practice to avoid cursor since cursor are very slow in performance. Always try to use SQL Server cursor alternative. Please refer the article Cursor Alternative.
10. Use Table variable inplace of Temp table
Practice to use Table varible in place of Temp table since Temp table resides in the TempDb database. Hence use of Temp tables required interaction with TempDb database that is a little bit time taking task.
11. Use UNION ALL inplace of UNION
Practice to use UNION ALL in place of UNION since it is faster than UNION as it doesn't sort the result set for distinguished values.
12. Use Schema name before SQL objects name
Practice to use schema name before SQL object name followed by "." since it helps the SQL Server for finding that object in a specific schema. As a result performance is best.
1. --Here dbo is schema name
2. SELECT col1,col2 from dbo.tblName
3. -- Avoid
4. SELECT col1,col2 from tblName
13. Keep Transaction small
Practice to keep transaction as small as possible since transaction lock the processing tables data during its life. Some times long transaction may results into deadlocks. Please refer the article SQL Server Transactions Management
Practice to set NOCOUNT ON since SQL Server returns number of rows effected by SELECT,INSERT,UPDATE and DELETE statement. We can stop this by setting NOCOUNT ON like as:
2. AS
5. .
6. .
7. END
15. Use TRY-Catch
Practice to use TRY-CATCH for handling errors in T-SQL statements. Sometimes an error in a running transaction may cause deadlock if you have no handle error by using TRY-CATCH. Please refer the article Exception Handling by TRY…CATCH
16. Use Stored Procedure for frequently used data and more complex queries
Practice to create stored procedure for quaery that is required to access data frequently. We also created stored procedure for resolving more complex task.
17. Avoid prefix "sp_" with user defined stored procedure name
Practice to avoid prefix "sp_" with user defined stored procedure name since system defined stored procedure name starts with prefix "sp_". Hence SQL server first search the user defined procedure in the master database and after that in the current session database. This is time consuming and may give unexcepted result if system defined stored procedure have the same name as your defined procedure.