Sunday, April 26, 2015

WCF Key points



WCF  : Stands For Window Communication Foundation

How to make changes  to WCF Service without breaking client ?
Use Name property  ServiceContract Attribute
Serialization is the process of converting an Object into an XML
Deserialization is the process of converting XML into Object
By Default WCF Uses : DataContractSerialization
If you expect the service to accept and return inherited types then use KnowTypesAttributes
With DataContract we have very limited control over SOAP XML Request and response but MessageContracts for full control over the generated XML SOAP messages
MessageContracts only if there is reason to tweak the structure of SOAP XML
Ex : SOAP Header, User credentials and license keys
ExtensionDataObject : Use IExtensionDataObject to preserve unkown elements during serialization and deserialization
When an exception occurs in wcf service serializes the exception into a SOAP Fault and then send to client
For debugging purpose IncludeExceptionDetailsInfaults settings.
SOAP Faults are in XML Format contains : Fault code, Fault Reason and Detail elements
Endpoint consists of 3 things :
A - Address  ( where the service available)
B - Binding   (how the client needs to communicate with service)
C - Contract  (what the service can do)
Binding that you choose determines the following
1.       Transport Protocol  (Http,tcp,Namepipes …)
2.       Message Encoding (text/XML, Binary)
3.       Protocols (reliable messaging transaction support)
Hosting  WCF Service :
·         Self hosting :  using window app
·         Window service :  using window service
·         Internet Information service : only Http bindings
·         Window activation service : it supports all bindings including Non – Http
Message Exchange Patterns in wcf
How client and wcf service exchange messages
1.       Request-reply :  client sends a message to wcf service and the waits for reply
2.       One-way :  client make a call to the service method, but does not wait for response
3.       Duplex : can be implemented using Request/Reply or OneWay operations

Default Message encoding mechanisms in wcf is text which is Base64 encodes
The preferred approach to send large binary messages in wcf is to use MTOM
Instancing Modes in WCF :
1.       PerCall :  new instance of service object is created for every request irrespective of whether the request comes from the same client or different client
2.       Persession : A new instance of the service object is created for each new client session and maintained for the duration of that session.
3.       Single : A single instance of the service object is created and handles all request for the lifetime of the application.
Multiple threads executing the application code simultaneously is called as concurrency.
The default concurrency mode in wcf is Single Concurrency Mode
WCF handles client request concurrently or not depends on 3 things
·         Service instance context mode
·         Service concurrency mode
·         Binding support
Multiple Concurrency mode an exclusivelock is not acquired on the service instance. This means multiple threads are allowed to access the service instance simultaneously and we get better throughput
Reentrant Concurrency mode allows the WCF service to issue callbacks to the client application
Thoughput :  amount of work done in a given time
WCF Throttling :  Throttling settings also influence the throughput of a wcf service and can be specified either in config or in code.
·         MaxConcurrentCalls=
·         MaxConcurrentInstancs=
·         MaxConcurrentSessions=

How to Prevent Cross-Site Scripting in ASP.NET

Check Request validation is enabled

      <system.web>
      <pages buffer="true" validateRequest="true" />
      </system.web>

     <%@ Page Language="C#" ValidateRequest="false" %>


Encode HTML output.

    Response.Write(HttpUtility.HtmlEncode(Request.Form["test"]));



Encode URL Output

    Response.Write(HttpUtility.UrlEncode(urlString));

Filter User Input
  •    Disable   ValidateRequest="false" in @page directive
  •    Encode string input with  HtmlEncode.
  •    Use StringBuilder to  Replace  
    sb.Replace("&lt;b&gt;", "<b>"); 

Set the Correct Character Encoding

<meta http-equiv="Content Type"
      content="text/html; charset=ISO-8859-1" />

OR
<% @ Page ResponseEncoding="iso-8859-1" %>

<configuration>
   <system.web>
      <globalization
         requestEncoding="iso-8859-1"
         responseEncoding="iso-8859-1"/>
   </system.web>
</configuration>


 

Wednesday, April 1, 2015

Threading in C#



What is the Process?
Process is an instance of a computer program that is being executed. It contains the program code and its current activity. Depending on the operating system, a process may be made up of multiple threads of execution that execute instructions concurrently.

A Process has at least one thread which is commonly called main Thread.

Threading :  threading in C# is Parallel code execution. 

Let’s see in example below


By default we have 2 types of Threads :

Background Thread:  This will quit if your main application quit

Foreground Thread:  which is keeping running even if your main application quit.