Friday, 29 January 2016

What is Marshalling?



Marshalling is medium or gateway to communicate with an unmanaged code from managed code in better way and vice versa, by using pinvoke, and ensures that data is returned back in a safe manner.
It is one of the core services offered by CLR (Common Language Runtime).
Because most of types in unmanaged environment do not have counterparts in managed environment, so we have to create conversion routine that convert manage types into unmanaged types and vice versa.
We call .Net code “Managed code” because it is controlled by CLR. Other code that is not managed by the CLR is called as unmanaged code.

What does mean of Thread Safe?What is Thread safe?



A thread safe is an unfortunate term because it does not have a solid definition. 

Thread safe basically means a bit of code or a piece of code will function correctly even when accessed by multiple threads. Multiple problems can occur if you use non-thread safe code in threaded application. The most common problem is deadlocking. 

Example:-
public class WidgetManipulator
{
   public int TotalWidgets = 0;
   public void AddWidget()
   {
      TotalWidgets++;
      Console.WriteLine("Total widgets = " + TotalWidgets.ToString());
   }
   public void RemoveWidgets()
   {
      TotalWidgets -= 10;
   }
}

This class exposes two methods. One method,addWidgets, add 1 to TotalWidgets fileds and writes the value to console. The second method subtracts 10 from the value of TotalWidgets. Consider a situation what would happen if two threads simultaneously attempt to access the same instance of WidgetManipulator class. One thread call AddWidget at the same time that second thread called RemoveWidgets. In this case, the value of TotalWidgets could be changed by the second thread before an accurate value could be reported by the first thread.

Tuesday, 26 January 2016

what is unit testing?

A Unit testing is smallest testable part of an application like function or procedure or classes or interfaces. Unit testing is a method by which individual unites of source code are tested to be determined if they are fit to use.

Monday, 25 January 2016

Procedure Vs Function

1.) Unlike Stored Procedure, Function returns only single value.
2.) Unlike Stored Procedure, Function accepts only input parameter.
3.) Unlike Stored Procedure, Function is not used for Insert|Update|Delete data in database.
4.) Unlike Stored Procedure, Function can be nested up to 32 level.
5.) Stored procedure can have 2100 input parameter while Function have up to 1023 input parameter.
6.) Unlike Stored Procedure, User Defined function cannot returns xml data type.
7.) Unlike Stored Procedure, User defined function doesn’t support exception  handling.

Why we go for page rendering in asp.net page life cycle?

Render is not an event. It a method to generate the output to send to the client. Browser understand only html markup so in page rendering All APSX controls are converted into html controls. That’s why we go for page rendering.

Tuesday, 19 January 2016

Why we cannot create object of abstract class?


If a class use keyword ‘abstract’ then it cannot be instantiated and can only be used as  base class.

Abstract class is like a template or an empty or partially empty structure, you have to extend it and build it before you can use it.
Example:-  let’s take an example animal abstract class. There are no such things as real animal. There are specific types of animal. You can instantiate cow and elephant and tiger but you cannot instantiate plain animal. So we create derived class from base class with different functionality like ‘makesound()’ but cannot be defined as base class level. So that it is not possible to create object of abstract class.

Monday, 18 January 2016

is it possible to create a constructor in Abstract class?

Yes. You can create a constructor in abstract class but it cannot be inherited. You cannot declare abstract constructor because constructor cannot be overridden. You can create public, private and protected constructors for abstract class.

Wednesday, 28 October 2015

What is Message contract in wcf?


Message is a packet of data which contains important information. WCF use this message to transfer from source to destination path.

WCF uses a SOAP message for communication. SOAP message contain message Envelop, Header and body.

Message contract is used to control the structure of a message body and serialization process. It is used to send/access the information over SOAP. Using message contract you can customize the parameter sent using a SOAP message between the client and the server.
How to Define/Declare MessageHeader and MessageBody?

MessageHeader:-
A Message Header is applied to a member of a message contract to declare the member within the message header.
 
[MessageContract(IsWrapped = true)]
public class EmpRequest
{

  [MessageHeader(Name = "EmpIdentity")]
  public string EmpId;

}


Message Body:-
A Message Body Member is applied to a member of a message contract to declare the member within the message body.




[MessageContract(IsWrapped = true)]

public class EmpResponse

{

    [MessageBodyMember]

    public Employee EmpINfo;

}
 
 
Example:-
Create WCF Service application.
 
Code for IService.cs :-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
 
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService" in both code and config file together.
[ServiceContract]
public interface IService
{
 
    [OperationContract]
    [FaultContract(typeof(string))]
    EmpResponse GetEmpInfo(EmpRequest request);
 
 
            // TODO: Add your service operations here
}
 
// Use a data contract as illustrated in the sample below to add composite types to service operations.
 
[MessageContract(IsWrapped = true)]
public class EmpRequest
{
    [MessageHeader(Name = "EmpIdentity")]
    public string EmpId;
}
 
[MessageContract(IsWrapped = true)]
public class EmpResponse
{
    [MessageBodyMember]
    public Employee EmpINfo;
}
 
[DataContract]
public class Employee
{
    [DataMember(Name = "FirstName", Order = 1)]
    public string FirstName;
 
    public string firstName
    {
        get { return firstName; }
        set { firstName = value; }
    }
 
    [DataMember(Name = "LastName", Order = 2)]
    public string LastName;
 
    public string lastName
    {
        get { return lastName; }
        set { lastName = value; }
    }
 
    [DataMember(Name = "Address", Order = 3)]
    public string Address;
 
    public string address
    {
        get { return address; }
        set { address = value; }
    }
}
 
Code for Service.cs :-
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
 
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in code, svc and config file together.
public class Service : IService
{
            public string GetData(int value)
            {
                        return string.Format("You entered: {0}", value);
            }
            public CompositeType GetDataUsingDataContract(CompositeType composite)
            {
                        if (composite == null)
                        {
                                    throw new ArgumentNullException("composite");
                        }
                        if (composite.BoolValue)
                        {
                                    composite.StringValue += "Suffix";
                        }
                        return composite;
            }
    private const string EmpId = "d2nf@5";
    public EmpResponse GetEmpInfo(EmpRequest request)
    {
        EmpResponse response = new EmpResponse();
 
        if (request.EmpId != EmpId)
        {
            string error = "Invalid Emp Id";
            throw new FaultException<string>(error);
  }
       response.EmpINfo = new Employee();
        response.EmpINfo.firstName = "Sushil";
        response.EmpINfo.lastName = "Modi";
        response.EmpINfo.address = "Bangalore"
        return response;
    }
}
 


Now deploy the Service on IIS or Local Server.

Create one Windows or Console application.

Add service reference into your application.
After adding service reference, create an object of Service Client proxy class and call the GetEmpInfo method and display the Employee info as given below.


class Program

{

static void Main(string[] args)

{

Try

{

   ServiceReference2.ServiceClient ref2 = new ServiceClient();

   ServiceReference2.Author a = ref2.GetAuthorInfo("d2nf@5");

   Console.WriteLine("First Name: " + a.firstName + "\t" + "Last Name" +                a.lastName + "\t" + "Address : " + a.address);

}

 catch (FaultException<string> ex)

 {

   Console.Write(ex.Detail + "<br/>");

 }

}

If you pass wrong EmpId the it show “Invalid Emp Id”.