Wednesday, June 25, 2014

Part 4: Http Unrevealed - Outside of Http

Outside of Http:
Proxy Server-
 
Is very much visible to end users. It takes users http request and take response back from server to the user.
Benefits of Proxy Server-1. In order to prevent users accessing restricted sites we can use proxy server to capture all http traffic at proxy server and thus setup strategy that will not send these requests to destination server. In this way we can ensure no one use the sites that is restricted.

2.Proxy server in fact can be used to inspect /probe the confidential message flow from system to server. In a way we can remove referrer headers that referenced organization resources such as images, files etc.

3. Proxy server can acts as access control to create http log that act as audit trail for http request that is send.
 
Type of Proxy
Forward Proxy Server-
This server sits close to client. This proxy help secure confidential information and can help filter traffic send from the client to destinations server.
 
Reverse Proxy-
is a proxy server that sits close to the server.This server can take off the load from destination webserver or any server for that matter. Take a case of compression of file or data using gzip which can delegated to this proxy server making destination server concentrate on processing of data or request.
 
Load Balanaced Proxy server-
All http request reaches this load balanced proxy server which will send requests to given server based on load as per round robin manner.

SSL Acceleration server-
This server will encrypt and decrypt http messages. This provides very secured level of isolation and can act as a centralized source to provide insulation to attack such as cross site scripting (XSS) or sql
injection for that reason.
 
Caching proxies-
Can act as centralized repository of cached data in distributed environment.

Caching -
HTTP response can have a value for Cache-Control of public, private, or no-cache
There is also a no-store value, meaning the message might contain sensitive information and should not be persisted, but should be removed from memory as soon as possible.
 
Public Cache- This is cache is applicable to whole system and is available for all users. Say for e.g we have logo, header and footer which will be consistent for all users.
Private Cache- This cache is specific to users , we can use Response.Cache for this kind of caching.
 
EtagThere are other ways to even identify given resource has been changed from last cache value is Etag. Etag is hashed values. Every time resource value or resources data changes it generated new hash value. Using this identifier we can even check the cache nature of the data.
HTTP/1.1 200 OK
Server: Apache
Last-Modified: Fri, 06 Jan 2012 18:08:20 GMT
ETag: "8e5bcd-59f-4b5dfef104d00"
Content-Type: text/xml

Tuesday, June 24, 2014

Part 2 : Http Unrevealed

In part 1 , I almost listed most of the intrinsic feature of Http. In part 2, the focus is more on how http interact with other layer protocol and ensure connectivity and communication.

Courtesy Succinctly Http: By Scott Allen



1. Http :it allows two Web Browser and Web Server( IIS or Apache) to communicate over a network.
2.TCP- Transmission Control Protocol

Browser extract host name and port . Opens TCP socket. Once ports is data is written into the socket.
TCP ensures data is transmitted to server and notify error if any. TCP also controls the flow of data, it has mechanism where it controls the rate at which that data being transfered thus ensuring enough time for receiver to process the data. Flow control is two way handshaking that TCP very well follows. Hence TCP is reliable protocol with flow control and error detection
mechanism.

3.IP: Internet protocol:-
IP ensures data moves across switches, routes, gateways , repeaters and other devices that help flow of data. IP even breaks down the larger piece of information into packets-fragment called data datagrams. This is indeed optimizes performance over network segment.

4. Data Link Layer- Ethernet .Eventually this IP datagram travels through  a optic fiber cable, wifi n/w or a satellite link.

Tool
www.wireshark.org

 Wireshark is network analyzer for IP and TCP- network traffic.

Monday, June 23, 2014

Part 1 Http Unrevealed

Part 1 Hypertext Transfer Protocol
Whatever we see in www is possible through http protocol.

Few know-how to be familiar with HTTP.

Resource Locators: URL Uniform Resource locator.

URL path
://:/?#
http://somthing.com - host name
http://somthing.com/author/santosh


URL with keyword such as author is help rank your website for given search keywords in URL.It improves your Search Engine Optimization.

By default URL with out port points to port 80. Other than port 80, one needs to mention or host url with port number.

#sign represents fragment. The fragment locate a specific HTML element in a html page by control ids. For example bookmark in given page.

URL EncodingUnsafe characters such space and ^ carat are replaced with %
%20 is the encoding for a space character ( 20 is hexadecimal value = US-ASCII space character).
%5 represents ^

Resources and Media

Web page can request different resource format for display for end users they can be executable applications ,mages, XML , Json,video,HTML audio etc.
When a host server responds to an HTTP request, it returns a resource and also specifies the content type. This content type is called Media type. Media type is represented by Multipurpose Internet Mail Extensions (MIME) standards.
Client request content type html then it will send text/html
Similarly
"image/jpeg"
"image/gif"
"image/png"

File Extension in URL

File extensions not always output requested file extension as given example http://something.com/main.jpg
How it works:-
1. Client first look at response header content type tag send by host server.
2. If content type not available it will parse first 200 bytes of response to identify content type.
3. If client fails to identify content type and first 200 bytes of response then it look into URL file extension to guess content type.

Content Type Negotiation

Client can tell host server what content type it want at time of response. For example some resource of book url may suggest the client may display book content in some specific language and can be downloaded in specific format.In such scenarioncontent type negotiation plays a essential role.
Accept keyword in Response header suggests this.

Accept-Language: fr-FR

Http Request Method
Method Description
GET Safe Method:Retrieve a resource
PUT Store a resource
DELETE Remove a resource
POST Update a resource
HEAD Retrieve the headers for a resource

HTTP Response Request

A full HTTP request might look like the following.
Request
GET http://something.com/ HTTP/1.1
Host: something.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) Chrome/16.0.912.75 Safari/535.7
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Referer: http://www.google.com/url?&q=odetocode
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

Response

HTTP/1.1 200 OK

Cache-Control: private

Content-Type: text/html; charset=utf-8

Server: Microsoft-IIS/7.0

X-AspNet-Version: 2.0.50727

X-Powered-By: ASP.NET

Date: Sat, 14 Jan 2012 04:00:08 GMT

Connection: close

Content-Length: 17151

Http Status Code Classification
Range Category
100–199 Informational
200–299 Successful
300–399 Redirection
400–499 Client Error
500–599 Server Error

With the help of  Fiddler Tool we can read and analyze the http response and request.

Tuesday, June 17, 2014

Iterator Enumerator with Yield Return.


MSDN :-
 
public System.Collections.IEnumerable SampleIterator(int start, int end)
{
    for (int i = start; i <= end; i++)
    {
        yield return i;
    }
}
We can fetch the results from SampleIterator:-
ListClass test = new ListClass();

foreach (int n in test.SampleIterator(1, 10))
{
    System.Console.Write(n + " ");
}
// Output: 1 2 3 4 5 6 7 8 9 10


IEnumerable and IEnumerator

 
 class Test : IEnumerable, IEnumerator
 {
        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        public object Current
        {
            get { throw new NotImplementedException(); }
        }

        public bool MoveNext()
        {
            throw new NotImplementedException();
        }
        public void Reset()
        {
            throw new NotImplementedException();
        }
 }

IEnumerable has GetEnumerator() Method with IEnumerator return type. Whereas IEnumerator has one property Current (position) and two method MoveNext() and Reset().

Important Facts:- When you write a foreach loop in C#, the compiler generates code that uses an Enumerator.

 
foreach (small x in Big)
{
   ...
}
it's functionally equivalent to writing:
IEnumerator x= Big.GetEnumerator();
while (x.MoveNext())
{
   y= (Big)x.Current
   ...
}

Thursday, June 12, 2014

Code Analysis :Avoid Calling Virtual Method in Constructor.


 
 
The above code is referenced from Bill Wagner post. The intent is the base constructor get called first and which indeed called Vfunc of derived class as in runtime the derived class had to be initialized.

Wednesday, June 11, 2014

AggregateException in Unit testing Task Parallel Library Asyc Call

Some Background Exception Handling (Task Parallel Library)

If a task is the parent of attached child tasks, or if you are waiting on multiple tasks, then multiple exceptions could be thrown. To propagate all the exceptions back to the calling thread, the Task infrastructure wraps them in an AggregateException instance

http://msdn.microsoft.com/en-us/library/dd997415(v=vs.110).aspx

Problem Statement:

Most of the time when we create a unit testing on Async Method- TPL it executes the unit test completely but ends up throwing AggregateException. This is due to the fact that it completes the one set of flow completely within a thread and comes out without executing all thread in parallel. If you see there is no UI and hence while testing the TPL must understand that unit testing is just to check the conditions of expected output with actual given a input as required whereas TPL works in different context. So ideally we must create test on non TPL methods in such case create separate methods to test on.
http://blog.stephencleary.com/2012/02/async-unit-tests-part-1-wrong-way.html

•An asynchronous method call will either produce a result immediately if one is available and continue execution in the current method or it will produce a result at a later time and immediately return control to the caller of the current method.
•The code that comes after an awaited method – the continuation – is what will be executed when the awaited method returns with a result.
http://www.jayway.com/2012/10/07/asyncawait-in-c-a-disaster-waiting-to-happen/

Workaround:

Brute force method not recommended but can be used if just unit test case conditions with failed and pass data.
[TestClass]
public class TestAsyncClassTest {
    [TestMethod]
    [ExpectedException(typeof(AggregateException))]
    public void SomeAsyncTest() {
    }
}

Sunday, June 8, 2014

Data Driven Excel data source Unit Testing Using VS 2013

 

Introduction:- We're using excel as data source for data driven unit testing.



Test Data:


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Trobleshooting Issues:
Probable Error: "Excel microsoft.ace.oledb.12.0 provider is not registered"
Download and install
2007 Office System Driver: Data Connectivity Components
 
 
 
 
 
 
 
 
 
 

 

Monday, June 2, 2014

Column Store Index in Sql Server 2012

The data in dataware house keep growing day by day , data  processing in data mart sometimes becomes unmanageable. The larger point is performance and to keep things at its bench mark we need proactive setup therefore Column Store Index is one of the important facility we can use in table to speed of query performance.

http://www.mssqltips.com/sqlservertip/2586/sql-server-2012-column-store-index-example/

Courtesy above blog post

 

Limitations of SQL Server ColumnStore Indexes

There are several limitations of using SQL Server ColumnStore indexes over Row Store indexes including:
  • A table with a ColumnStore Index cannot be updated
  • ColumnStore index creation takes more time (1.5 times almost) than creating a B-tree index (on same set of columns) because the data is compressed
  • A table can have only one ColumnStore Index and hence you should consider including all columns or at least all those frequently used columns of the table in the index
  • A ColumnStore Index can only be non cluster and non unique index; you cannot specify ASC/DESC or INCLUDE clauses
  • Not all data types (binary, varbinary, image, text, ntext, varchar(max), nvarchar(max), etc.) are supported
  • The definition of a ColumnStore Index cannot be changed with the ALTER INDEX command, you need to drop and create the index or disable it then rebuild it
  • You can create a ColumnStore index on a table which has compression enabled, but you cannot specify the compression setting for the column store index
  • A ColumnStore Index cannot be created on view
  • A ColumnStore Index cannot be created on table which uses features like Replication, Change Tracking, Change Data Capture and Filestream
  • CREATE NONCLUSTERED COLUMNSTORE INDEX  
    ON 
    (
     Col1,
     Col2,
     ....
     ....
     Coln
    )
    GO

Friday, May 30, 2014

CA2214 : Do not call overridable methods in constructors Workaround

When you seriously take .net code analysis you may either try to suppress them or try to fix it. There is one such code analysis review if have override or virtual method call in constructor.

 
http://stackoverflow.com/questions/17018569/accessing-an-implemented-abstract-property-in-the-constructor-causes-ca2214-do

http://blogs.msdn.com/b/ericlippert/archive/2008/02/18/why-do-initializers-run-in-the-opposite-order-as-constructors-part-two.aspx

http://blog.duncanworthy.me/swdev/csdotnet/constructor-gotchas-part1/

http://stackoverflow.com/questions/119506/virtual-member-call-in-a-constructor


When an object written in C# is constructed, what happens is that the initializers run in order from the most derived class to the base class, and then constructors run in order from the base class to the most derived class
Problem - Constructor has call to override method.



 
 Workaround :- Add Sealed


 

Monday, May 19, 2014

Powershell Scripting: Automated scripting of database DDL

Tips # OneNote Copy Text From Image

How to extract text from image-Use One Note

1. Copy Image to onenote
2. Right click on image select Copy text from Image
3. Paste it.

 

Sunday, May 18, 2014

Tips #-.Net Reflection to fetch value of Object Property.

Runtime Activity- I want to pass a class and property in the string and resolve the value of the property.

 With Generics

Generics

Friday, May 2, 2014

Get website details such as physical path using Powershell



Import-Module "WebAdministration" -ErrorAction Stop

foreach($site in (dir iis:\sites\*WebSiteNameXyz*))



{

write-host $site.Name

write-host $site.Bindings

write-host $site.State

write-host $site.physicalpath



}

Power of Powershell remoting WSMan

If you want to execute powershell script from your local box onto remote server this is the best solution to do.

Before you take deep dive in below powershell script . Ensure few things in local source and destination remote server settings.
* Ensure WinRs windows remote services . Type services.msc and this service should be enabled and auto mode.
* Ensure WSMan is enabled
Use gpedit.msc and look at the following policy:
 
Computer Configuration ->
Administrative Templates ->
System ->
Credentials Delegation -> Allow Delegating Fresh Credentials. 
 
Verify that it is enabled and configured with an SPN appropriate for the target computer.C lick on add server list mention WSMAN/myserver.domain.com
 

set-item wsman:localhost\client\trustedhosts -value *

#Enable credssp authentication
Enable-WSManCredSSP -Role Client –DelegateComputer *

# This will establish server connection
Connect-WSMan myserver

invoke-command -computername myserver-authentication credssp -credential domain\username -filepath c:\dbtest.ps1

  

This will prompt for password and you are good to go.
Now this dbtest.ps1 can be your powershell script which will deploy code base to different environment.

 
Useful References


Write to event log
http://blogs.technet.com/b/heyscriptingguy/archive/2013/06/20/how-to-use-powershell-to-write-to-event-logs.aspx

Monitor Scheduled Job
http://blogs.technet.com/b/heyscriptingguy/archive/2014/04/29/powertip-use-powershell-to-show-state-of-scheduled-tasks.aspx

 
Database administration

Wednesday, April 16, 2014

Four Timeout Property for Bindings in WCF

Four Timeout Property for Bindings in WCF
sendTimeout: time taken by WCF service to respond to client
openTimeout: Amount of time client willing to wait for WCF service to open connection
closeTimeOut: Amount of time client close the proxy connection to WCF service
receiveTimeout: Time spent by client to process response received from WCF service

 
                 sendTimeout="00:25:00">
   

 


http://stackoverflow.com/questions/1520283/wcf-service-how-to-increase-the-timeout

Powershell Issue: SOAP header Action was not understood. when use wsHttpBinding

Powershell Script Calling WCF Service.

Issue: SOAP header Action was not understood. when use wsHttpBinding

This can be solved if we use basicHttpBinding.

You might want to use the basicHttpBinding to get it working with a .NET 2.0 client. That binding already provides the compatibility required by older clients or other platforms. If you still decide to go with wsHttpBinding, you will have to use Microsoft WSE to create messages in the .NET 2.0 client that are compatible with the wsHttpBinding.
With Above statement from Stackoverflow that means we can run powershell in .net 4 runtime to even overcome above problem.
http://stackoverflow.com/questions/8518282/how-to-communicate-with-a-wcf-service-wshttpbinding-a-net-2-0-client


http://stackoverflow.com/questions/2094694/how-can-i-run-powershell-with-the-net-4-runtime

Simply modify (or create) $pshome\powershell.exe.config so that it contains the following:
xml version="1.0"?> 
 
     useLegacyV2RuntimeActivationPolicy="true"> 
         version="v4.0.30319"/> 
         version="v2.0.50727"/> 
     

Path where this config file must be created :C:\Windows\System32\WindowsPowerShell\v1.0\

Sample Script to call WCF Service with Powershell as client
$uri="http://localhost:59889/Service1.svc"

$proxy = New-WebServiceProxy -Uri $URI -Class newClass1 -Namespace WebService1




$proxy.GetName()
#this is where we have int as input to service and string as output. GetData below ... 
 
$proxy.GetData(5,$true)
 

Wednesday, March 26, 2014

Force Explicit Calling of Static Class-Static constructor

The static constructor loads or called only once when any method of static class is invoked or initialize. There is no need of explicit calling of static constructor. But when there is no static property or method to do so then we can have explicit option to call static constructor as given below
typeof(StaticClassName).TypeInitializer.Invoke(null, null);

Monday, March 24, 2014

C# Cool Code Tips

1.Specialized Collection- NameValueCollection
Real time example
Parse a string such as "p1=6&p2=7&p3=8" into a NameValueCollection

NameValueCollection qscoll = HttpUtility.ParseQueryString(querystring);

Results-
qscoll["p1"] , qscoll["p2"] and qscoll["p3"]                

Speciality- This NameValueCollection can hold duplicates Key with different Values.

   // Creates and initializes a new NameValueCollection.
      NameValueCollection myCol = new NameValueCollection();
      myCol.Add( "red", "rojo" );
      myCol.Add( "green", "verde" );
      myCol.Add( "blue", "azul" );
      myCol.Add( "red", "rouge" );


 public static void PrintKeysAndValues2( NameValueCollection myCol )  {
      Console.WriteLine( " [INDEX] KEY VALUE" );
      for ( int i = 0; i < myCol.Count; i++ )
         Console.WriteLine( " [{0}] {1,-10} {2}", i, myCol.GetKey(i), myCol.Get(i) );
      Console.WriteLine();
   }

Displays the elements using
GetKey and Get:   
[INDEX] KEY VALUE   
[0]     red rojo,rouge   
[1]     green verde   
[2]     blue azul

2. Array.ConvertAll
Real Time Example
You have string  with comma separated with integer value and you want to have int[] conversion from string [] array.

look at this now!

string commaSep= "1,2,3"
int[] transform= Array.ConvertAll(commaSep.split(','),s=>int.parse(s))

3. Fetch Calling Method Name using CallerMemberNameAttribute C#4.5

In .Net 4.5 there is now a much easier way to do this. You can take advantage of the CallerMemberNameAttribute
public class SomeClass
{
    public void SomeMethod([CallerMemberName]string memberName = "")
    {
        Console.WriteLine(memberName); //output will be name of calling method
    }
}


https://stackoverflow.com/questions/3095696/how-do-i-get-the-calling-method-name-and-type-using-reflection

Thursday, March 13, 2014

Linq to replace Dictionary values with XML formed tag values.

static void Main(string[] args)
{
 
IDictionary<string, string> placeHolder = new Dictionary<string, string>();

placeHolder.Add("#FirstName#","Phil");

placeHolder.Add("#MiddleName#", "J");

placeHolder.Add("#LastName#", "Haack");
        string parsedXmlString=GetParsedTemplate(@"



#FirstName#

#MiddleName#

#LastName#





", placeHolder);}
 
 
 

public static string GetParsedTemplate(string wellFormedXML, IDictionary<string, string> placeHolder)
{

placeHolder.AsEnumerable().ToList().ForEach(t => wellFormedXML = wellFormedXML.Replace(t.Key, t.Value));

return wellFormedXML;


}