Tuesday, 20 November 2018

IQ


Static Constructor
Ø  Static constructor is first block of code to execute in class.
Ø  A static constructor is a constructor declared using a static modifier.
Ø  A static constructor is use to initialize static data
Ø  Static constructor does not take any parameters.
Ø  It is the first block of code executed in a class. With that, a static constructor executes only once in the life cycle of class.

class Demo
    {
        static int val1;
        int val2;

        static Demo()
        {
            Console.WriteLine("This is Static Constructor");
            val1 = 70;
        }


        public Demo(int val3)
        {
            Console.WriteLine("This is Instance Constructor");
            val2 = val3;
        }

        private void show()
        {
            Console.WriteLine("First Value = " + val1);
            Console.WriteLine("Second Value = " + val2);
        }

        static void Main(string[] args)
        {
            Demo d1 = new Demo(110);
            Demo d2 = new Demo(200);
            d1.show();
            d2.show();
            Console.ReadKey();
        }
    }


Ø  Private Constructor in C#?
Ø  Private constructor is constructor that is preceded by private access specifier. For example, below class has a private constructor.
Ø  We know that if we don’t write constructor in the class then by default constructor gets called on object creation which is public. Or if we want to allow object creation of the class then we write public constructor or else we explicitly specify private access specifier on class constructor.
Ø  Can we create object of class with private constructor in C#?Ø  No, object of a class having private constructor cannot be instantiated from outside of the class. However, we can create object of the class inside class methods itself.














class A
{
    //private constructor
    private A()
    {
    }
}

class Program
{
    static void Main(string[] args)
    {
        A obj = new A();//Error cannot access private constructor
    }
}
 Ø  Use of private constructor in C# Sharp Programming:
Following are the scenarios when we can make use of private constructor in C# code.
 1)  Stop object creation of a class
If we want to stop object creation of a class, then we can make the class constructor private. So, if we try to create an object from the main program or other classes, then compiler will flash an error.
2) Use in Singleton class
We know that singleton class allows only single instance of it throughout the execution of the application. When we design a singleton class, we use private constructor, so, no user can create object of this class to stop multiple object creation. And singleton class itself provide the same object over and over on demand. 
3)  Stop a class to be inherited
Ø  If we don’t want a class to be inherited, then we make the class constructor private. So, if we try to derive another class from this class then compiler will flash an error. Why compiler will flash an error? We know the order of execution of constructor in inheritance that when we create an object of a derived class then first constructor of the base call will be called then constructor of derived class. Since, base class constructor is private, hence, derived class will fail to access base class constructor.
Ø  In below c sharp code example, compiler flashes error on creation of object of derived class or as soon as you write constructor of derived class.























//base
class B
{
    private B()
    {
    }
}
//Derived
class D : B
{
    public D()
    {
    }
    
}

class Program
{
    static void Main(string[] args)
    {
       D obj = new D();//error
    }
}





Ø  Difference between Singleton Class and Static Class


Static Class:
  1. You cannot create the instance of static class.

  2. Loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.

  3. Static Class cannot have constructor.

  4. We cannot pass the static class to method.

  5. We cannot inherit Static class to another Static class in C#.
Singleton:
  1. You can create one instance of the object and reuse it.

  2. Singleton instance is created for the first time when the user requested. 

  3. Singleton class can have constructor.

  4. You can create the object of singleton class and pass it to method.

  5. Singleton class does not say any restriction of Inheritance.

  6. We can dispose the objects of a singleton class but not of static class.

WCF Predefined Bindings

1.   basicHttpBinding

basicHttpBinding is best when you have traditional ASMX(Active Server Methods) web services and needs to be replace with WCF. It supports text as well as MTOM encodings and it does not support WS-* standards like WS-Addressing, WS-Security and WS-ReliableMessaging.
basicHttpBinding uses HTTP or HTTPS protocols. You can configure 
SSL for Transport Layer security with basicHttpBinding.

2.   WsHttpBinding

This is secure and interoperable bindings uses SOAP over HTTP. With WsHttpBinding messages are encrypted by default and achieve message level security. It supports reliability, transactions and security over internet. It supports HTTP or HTTPS protocols and text as well as MTOM encoding.
The difference between basicHttpBinding and WsHttpBinding is WsHttpBinding does support WS-* standards like WS-Addressing, WS-Security and WS-ReliableMessaging

3.   wsDualHttpBinding

wsDualHttpBinding is best when you required bidirectional communication with client. In some scenario when client makes call to WCF service, after processing of long running request service has to call client application for example updating shipment details to client application.
It supports reliability, transactions and security over internet. It supports HTTP or HTTPS protocols and text as well as MTOM encoding. You can implement Duplex message exchange pattern with wsDualHttpBinding.

4.   webHttpBinding

webHttpBinding is best when you wish to implement RESTful WCF service. This is secure and interoperable binding which sends information directly over HTTP or HTTPS without creating SOAP messages. It allows HTTP request to use plain old XML (POX) style messaging which reduces the message size on wire compare to SOAP messages.

5.   NetTcpBinding

netTcpBinding is best when WCF service and its clients are in intranet infrastructure. As it supports only TCP protocol and not HTTP so service cannot be accessed over internet.
This is secure binding is used to send binary encoded SOAP messages with in intranet computers. It supports reliability, transaction and security. If your using netTcpBinding and host WCF service in IIS, you need to make some settings on system and IIS this article will help you for required settings.

6.   netNamedPipeBinding

When your WCF service and its clients reside on same computer netNamedPipeBinding is the best choice and gives best performance over other bindings. This is secure bindings. Binary encoded SOAP messages are sent over named pipes.
See how to implement netNamedPipeBinding in WCF services.

7.   netPeerTcpBinding

netPeerTcpBinding is best when you require more security for peer to peer communication as netTcpBinding does not provide it. It is secure binding and supports TCP protocols.

8.   WsFederationHttpBinding

It is secure and interoperable binding supports federated security. It supports HTTP and HTTPS transport protocols as well as text and MTOM encodings.

9.   NetMsmqBinding

netMsmqBinding is best when you have to execute service operations in queued manner. Service requests are placed in queue and executed one by one. With netMsmqBinding service operations will always be one way and does not return any response to client.

  • How implement security in WCF






Tuesday, 6 November 2018

IQ


  1. Can we Declare non static methods in static class?
  • No we can't declare non static methods in static class
  1. Can we Declare static methods in Non static class?


  • Yes we can declare non static methods in static class.
 public  class MyStaticClass
    {
        public static int cnt = 0;
        public int cv = 0;
        public int add(int a,int b)
        {
            return a + b;           
        }
        public static int add1(int a, int b)
        {
            return a+b;            
        }
    }

    class Program
    {
        
        static void Main(string[] args)
        {
            int a = 0;
            int b = 0;
            MyStaticClass obj = new MyStaticClass();
            b=MyStaticClass.add1(2, 3);
            a= obj.add(3, 5);
            Console.WriteLine(a);
            Console.WriteLine(b);
            Console.ReadLine();
        }
    }

  • Given number is prime number or not?
  • Reverse a number in c# code?
  • how to change first letter as capital one in sql
    declare @val varchar(10)
      set @val='gupta'
        select upper(left(@val, 1)) + right(@val,LEN(@val)-1) 


      • O/P--Gupta

      • SELECT ISNULL(NULL, 'TEST')
      • --O/P--Test

      • SELECT COALESCE(NULL, 'Test','W3Schools.com');
      • --O/P--Test



        • What is collation?
          Collation refers to a set of rules that determine how data is sorted and compared. Character data is sorted using rules that define the correct character sequence, with options for specifying case-sensitivity, accent marks, kana character types and character width.
        How to remove duplicate records in sql.
         With CTE_Duplicates as
           (select empid,name , row_number() over(partition by empid,name order by empid,name ) rownumber 
           from EmpDup  )
           delete from CTE_Duplicates where rownumber!=1
           
        At a time 10 members sends a request to IIS then How IIS is able to find which request is which person?



        Thursday, 1 November 2018

        Request flow used for ASP.NET MVC framework

        Request flow handles the request from the clients and passes it to the server. The Request flow is as follows:

        • Request is being taken from User to controller.
        • Controller processes the request from the user and creates a data Model of that particular request. 
        • Data model that is being created is then passed to View that handles the frontend or the design. 
        • View then transforms the Data Model by using its own functions in an appropriate output format.
        • The output format that is being given by the View is then gets rendered to the Browser and the View will be seen by the user. 

        Now the question is,  how does this architecture work?

        • The workflow of MVC architecture is given below, and we can see the request response flow of a MVC web application.
         
        The figure is very self-explanatory and shows the work flow of MVC architecture. The client, which is the browser, sends a request to server (internet information server) and the Server finds a Route specified by the browser in its URL and through Route. The request goes to a specific Controller and then the controller communicates with the Model to fetch/store any records . Views are populated with model properties, and controller gives a response to the IIS7 server, as a result server shows the required page in the browser.

        Thursday, 4 October 2018

        OWASP Top 10 Project: Security Vulnerabilities for ASP.NET

        What Is OWASP?

        • The Open Web Application Security Project (OWASP) is an open community dedicated to enabling organizations to develop, purchase, and maintain applications and APIs that can be trusted.

        What Are The Latest OWASP Top 10 Vulnerabilities?
        1.   Injection
        2.   Broken Authentication
        3.   Sensitive Data Exposure
        4.   XML External Entities (XXE)
        5.   Broken Access Control
        6.   Security Misconfiguration
        7.   Cross-Site Scripting (XSS)
        8.   Insecure Deserialization
        9.   Using Components with Known Vulnerabilities
        10.   Insufficient Logging & Monitoring

        What are the default access modifiers in C#



                            | Default   | Permitted declared accessibilities
        ------------------------------------------------------------------
        namespace            | public    | none (always implicitly public)
        
        enum                 | public    | none (always implicitly public)
        
        interface            | internal  | public, internal
        
        class                | internal  | public, internal
        
        struct               | internal  | public, internal
        
        delegate             | internal  | public, internal
        Nested type and member accessiblities
                             | Default   | Permitted declared accessibilities
        ------------------------------------------------------------------
        namespace            | public    | none (always implicitly public)
        
        enum                 | public    | none (always implicitly public)
        
        interface            | public    | none
        
        class                | private   | All¹
        
        struct               | private   | public, internal, private²
        
        delegate             | private   | All¹
        
        constructor          | private   | All¹
        
        interface member     | public    | none (always implicitly public)
        
        method               | private   | All¹
        
        field                | private   | All¹
        
        user-defined operator| none      | public (must be declared public)
        ¹ All === public, protected, internal, private, protected internal
        ² structs cannot inherit from structs or classes (although they can, interfaces), hence protected is not a valid modifier