Sunday, 8 January 2017

mvc lifecle

In this chapter, we will discuss the overall MVC pipeline and the life of an HTTP request as it travels through the MVC framework in ASP.NET. At a high level, a life cycle is simply a series of steps or events used to handle some type of request or to change an application state. You may already be familiar with various framework life cycles, the concept is not unique to MVC.
For example, the ASP.NET webforms platform features a complex page life cycle. Other .NET platforms, like Windows phone apps, have their own application life cycles. One thing that is true for all these platforms regardless of the technology is that understanding the processing pipeline can help you better leverage the features available and MVC is no different.
MVC has two life cycles −
  • The application life cycle
  • The request life cycle
MVC Life Cycles

The Application Life Cycle

The application life cycle refers to the time at which the application process actually begins running IIS until the time it stops. This is marked by the application start and end events in the startup file of your application.

The Request Life Cycle

It is the sequence of events that happen every time an HTTP request is handled by our application.
The entry point for every MVC application begins with routing. After the ASP.NET platform has received a request, it figures out how it should be handled through the URL Routing Module.
Modules are .NET components that can hook into the application life cycle and add functionality. The routing module is responsible for matching the incoming URL to routes that we define in our application.
All routes have an associated route handler with them and this is the entry point to the MVC framework.
Route Handler
The MVC framework handles converting the route data into a concrete controller that can handle requests. After the controller has been created, the next major step is Action Execution. A component called the action invoker finds and selects an appropriate Action method to invoke the controller.
After our action result has been prepared, the next stage triggers, which is Result Execution. MVC separates declaring the result from executing the result. If the result is a view type, the View Engine will be called and it's responsible for finding and rending our view.

If the result is not a view, the action result will execute on its own. This Result Execution is what generates an actual response to the original HTTP request.

Friday, 23 December 2016

WCF SOAP AND REST


WCF
1. It is also based on SOAP and return data in XML form.
2. It is the evolution of the web service(ASMX) and support various protocols like 3. TCP, HTTP, HTTPS, Named Pipes, MSMQ.
4. The main issue with WCF is, its tedious and extensive configuration.
5. It is not open source but can be consumed by any client that understands xml.
6. It can be hosted with in the applicaion or on IIS or using window service.

WCF Rest
1. To use WCF as WCF Rest service you have to enable webHttpBindings.
2. It support HTTP GET and POST verbs by [WebGet] and [WebInvoke] attributes respectively.
3. To enable other HTTP verbs you have to do some configuration in IIS to accept request of that particular verb on .svc files
4. Passing data through parameters using a WebGet needs configuration. The UriTemplate must be specified
5. It support XML, JSON and ATOM data format.

Where to use:-

1. In my opinion, SOAP services with SOA architecture are well suited in enterprise applications. SOA architecture includes several key factors like service versioning, scalability, deployment and maintaining of services.
2. REST services are best suited in Internet-facing applications, which can take advantage of HTTP caching and other features.

// SOME OF THE MORE FEATURES ARE GIVEN BELOW



iffernce between normal WCF service and RESTful WCF Service
There is no more difference between WCF service and REST service only main difference is that How Client accesses our Service.
Normal WCF service runs on the SOAP format but when we create REST service then client can access your service in different architecture style like JSON

REST uses4 HTTP methods to insert/delete/update/retrieve information which is below:
GET - Retrive a specific representation of a resource
PUT - Creates or updates a resource with the supplied representation
DELETE - Deletes the specified resource
POST - Submits data to be processed by the identified resource

Where to Use RESTful WCF Service There are some scenario’s where we can use RESTful service like
1) Less Overhead because there is no use of SOAP object
2) Testing is very easy
3) Standardization is very nice

Creation of RESTful WCF Service Step1.
Create a new WCF project in VS 2008/20010

Step 2:
Delete a Service Model node from Web.config

Step 3.
Build the Solution then Right click on the svc file and Click on View Markup button
And add following code to the markup

Factory="System.ServiceModel.Activation.WebServiceHostFactory"

Step 4
Add following references to the WCF Service Application project, if you are using VS2010
Microsoft.Http.dll
Microsoft.Http.Extension.dll
System.ServiceModel.Web.dll


Step 5

  [ServiceContract]
    public interface IService1
    {

        [OperationContract(Name = " AddTwoNumber ")]
        [WebInvoke(UriTemplate = "/", Method = "POST")]
         int AddTwoNumber(int i, int j);

       

        [OperationContract(Name = " SubTwoNumber")]
        [WebGet(UriTemplate = "/")]
        int SubTwoNumber(int i, int j);

      
    }

Add your code in your service like here I have created 2 methods like AddTwonumber which takes two parameter I and j
In the above code we use
WebInvoke :- means invoke web method

Webinvoke takes Two parameters like
1. Method Type there are 4 types of method
a.Post C.Invoke
b.Get d.Delete
2. UriTemplate = "/ADD” tells what would be URI for this particular method


Like this we can create RESTful service.

More differences:
Web Service is an abstract term encompassing a large variety of data providers for distributed systems. Perhaps you are referring to ASMX web services, which can still be found in the wild but aren't really widely used in new development these days.

WCF Service is Microsoft's implementation of SOAP. There are others implementations or you could roll your own (not recommended).

SOAP is a kind of stateful, session-based, message-based web service. It's good if your service is designed as a set of complex actions.

REST is a stateless, sessionless, resource-based web service. It's good if your service is designed to access data and perform simple CRUD operations on it. SOAP and REST are mutually exclusive. A service cannot be both. There are ways to manipulate vanilla WCF to make is RESTful but these techniques are becoming deprecated. If you want to implement a RESTful web service there are two main choices in the Microsoft world: WCF Data Services and ASP.NET Web API.



property

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OopsConcept
{
    class property
    {
    }
    class Student
   {
      private string code = "N.A";
      private string name = "not known";
      private int age = 0;
     
      // Declare a Code property of type string:
      public string Code
      {
         get
         {
            return code;
         }
         set
         {
            code = value;
         }
      }
     
      // Declare a Name property of type string:
      public string Name
      {
         get
         {
            return name;
         }
         set
         {
            name = value;
         }
      }
     
      // Declare a Age property of type int:
      public int Age
      {
         get
         {
            return age;
         }
         set
         {
            age = value;
         }
      }
      public override string ToString()
      {
         return "Code = " + Code +", Name = " + Name + ", Age = " + Age;
      }
   }
  
   class ExampleDemo
   {
      public static void MainProperty()
      {
     
         // Create a new Student object:
         Student s = new Student();
        
         // Setting code, name and the age of the student
         s.Code = "001";
         s.Name = "Zara";
         s.Age = 9;
         Console.WriteLine("Student Info: {0}", s);
        
         //let us increase age
         s.Age += 1;
         Console.WriteLine("Student Info: {0}", s);
         Console.ReadKey();
      }
   }
}

private constructor

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OopsConcept
{
    class privateConstructor
    {
    //{
    //    Private constructor is a special instance constructor used in a class that contains static member only. If a class has one or more private constructor and no public constructor then other classes is not allowed to create instance of this class this mean we can neither create the object of the class nor it can be inherit by other class. The main purpose of creating private constructor is used to restrict the class from being instantiated when it contains every member as static.
        public static string st = string.Empty;
        public  string st1 = string.Empty;
        public string stw()
        {
            return "sttest";
        }
        public privateConstructor(string st1, string st2)
        {
            Console.WriteLine("Hi"+st1+"and hi"+st1);

        }
        private privateConstructor()
        {
            Console.WriteLine("This is the private cinstructor that can not instiated0");

        }

    }
    class program
    {
        public static void Main5(string[] ar)
        {
            privateConstructor objPrivatecontructor = new privateConstructor("hi p1","p2");
            objPrivatecontructor.st1 = "test";
        }
    }

}

Polymorphism

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OopsConcept
{
    class Polymorphism
    {

    }
    ////compie time polymorphis start here
    //public class Class1
    //{
    //    public void NumbersAdd(int a, int b)
    //    {
    //        Console.WriteLine(a + b);
    //    }
    //    public void NumbersAdd(int a, int b, int c)
    //    {
    //        Console.WriteLine(a + b + c);
    //    }
    //}
    //public class Compiletimepolymorphis
    //{
    //    public static void main(string[] args)
    //    {
    //        Class1 objClass1 = new Class1();
    //        objClass1.NumbersAdd(12,5);
    //        objClass1.NumbersAdd(12, 12, 13);
    //    }
    //}

    ///Compile time poly morphis end here
    ///
    ///Run time poly morphis star here
    ///
    public class Bclass
    {
        public virtual void Sample1()
        {
            Console.WriteLine("Base Class");
        }
    }
    // Derived Class
    public class DClass : Bclass
    {
        public override void Sample1()
        {
            Console.WriteLine("Derived Class");
        }
    }
    // Using base and derived class
    class ProgramPloy
    {
        static void MainPlohy(string[] args)
        {
            // calling the overriden method
            DClass objDc = new DClass();
            objDc.Sample1();
            // calling the base class method
            Bclass objBc = new DClass();
            objBc.Sample1();
          
            Console.ReadLine();
        }
    }
}

partial class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OopsConcept
{
    class Partialclass
    {
        public partial class student
        {
            public void GetStudentDetails()
            {
                //developer is working on student details module
            }
            //some other function for same modules..
        }

        public partial class student
        {
            public void GetStudentResults()
            {
                //developer is working on student results module
            }

            //some other function for same modules..
        }
    }
}

InterfaceTest

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OopsConcept
{
    class InterfaceTest
    {
        interface myclass
        {
            string Animal();
            string Animal1();
  
            // {
            //     string stranimaltext ="Dog has four leg";
            //     return stranimaltext;

            // }
            //public static void Refrencefunction()
            //{
            //}

        }
        class Aceeessabstract : myclass
        {
            public string Animal()
            {
                string stranimaltext = "Cow has four leg";
                return stranimaltext;

            }
            public string Animal1()
            {
                string stranimaltext = "Buffalow1 has four leg";
                return stranimaltext;

            }

        }


        static void Main2(string[] args)
        {
            Aceeessabstract objAb = new Aceeessabstract();
            Console.WriteLine(objAb.Animal());
            Console.WriteLine(objAb.Animal1());
            Console.Read();
        }
    }
}