Tuesday, 6 September 2011

WCF interview questions and answers


WCF, all services expose contracts. The contract is a platform-neutral and standard way of describing what the service does. 

WCF defines four types of contracts.
 


Service contracts
 

Describe which operations the client can perform on the service.
 
There are two types of Service Contracts.
 
ServiceContract - This attribute is used to define the Interface.
 
OperationContract - This attribute is used to define the method inside Interface.
 


[ServiceContract]

interface IMyContract

{

   [OperationContract]

   string MyMethod( );

}

class MyService : IMyContract

{

   public string MyMethod( )

   {

      return "Hello World";

   }

}



Data contracts
 

Define which data types are passed to and from the service. WCF defines implicit contracts for built-in types such as int and string, but we can easily define explicit opt-in data contracts for custom types.
 

There are two types of Data Contracts.
 
DataContract - attribute used to define the class
 
DataMember - attribute used to define the properties.
 


[DataContract]

class Contact

{

   [DataMember]

   public string FirstName;



   [DataMember]

   public string LastName;

}


If DataMember attributes are not specified for a properties in the class, that property can't be passed to-from web service.
 

Fault contracts
 

Define which errors are raised by the service, and how the service handles and propagates errors to its clients.
 


Message contracts
 

Allow the service to interact directly with messages. Message contracts can be typed or untyped, and are useful in interoperability cases and when there is an existing message format we have to comply with.





Q1. What is WCF?
WCF stands for Windows Communication Foundation. It is a Software development kit for developing services on Windows. WCF is introduced in .NET 3.0. in the System.ServiceModel namespace. WCF is based on basic concepts of Service oriented architecture (SOA)

Q2. What is endpoint in WCF service?
The endpoint is an Interface which defines how a client will communicate with the service. It consists of three main points: Address,Binding and Contract.

Q3. Explain Address,Binding and contract for a WCF Service?
Address:Address defines where the service resides.
Binding:Binding defines how to communicate with the service.
Contract:Contract defines what can be done with the service.

Q4. What are the various address format in WCF?
a)HTTP Address Format:--> http://localhost:
b)TCP Address Format:--> net.tcp://localhost:
c)MSMQ(MicroSoft Message Queue) Address Format:--> net.msmq://localhost:
Q5. What are the types of binding available in WCF?
A binding is identified by the transport it supports and the encoding it uses. Transport may be HTTP,TCP etc and encoding may be text,binary etc. The popular types of binding may be as below:
a)BasicHttpBinding
b)NetTcpBinding
c)WSHttpBinding
d)NetMsmqBinding

Q6. What are the types of contract available in WCF?
The main contracts are:
a)Service Contract:Describes what operations the client can perform.
b)Operation Contract : defines the method inside Interface of Service.
c)Data Contract:Defines what data types are passed
d)Message Contract:Defines wheather a service can interact directly with messages

Q7. What are the various ways of hosting a WCF Service?
a)IIS b)Self Hosting c)WAS (Windows Activation Service)

Q8. What is the proxy for WCF Service?
A proxy is a class by which a service client can Interact with the service.
By the use of proxy in the client application we are able to call the different methods exposed by the service

Q9. How can we create Proxy for the WCF Service?
We can create proxy using the tool svcutil.exe after creating the service.
We can use the following command at command line.
svcutil.exe *.wsdl *.xsd /language:C# /out:SampleProxy.cs /config:app.config

Q10.What is the difference between WCF Service and Web Service?
a)WCF Service supports both http and tcp protocol while webservice supports only http protocol.
b)WCF Service is more flexible than web service.


Address (Where):- An address indicates where we can find this service. Address is a URL, which points to the location of the service.

Bindings (How):- Bindings determined that how this end can be accessed. It determines how communication is done. For instance, you expose your service, which can be accessed using SOAP over HTTP or BINARY over TCP. So for each of these communications medium two bindings will be created.

Contracts (What):- Contract is an agreement between two or more parties. It defines the protocol how client should communicate with your service. Technically, it describes parameters and return values for a method.

Gridview Searching and sorting through Jquery

Paste this on head section of the page....

<head runat="server">
 <script type="text/javascript" src="../Scripts/jquery-1.6.1.min.js"></script>
    <script src="../Scripts/jquery.tablesorter.min.js" type="text/javascript"></script>
    <script src="../Scripts/jquery.quicksearch.js" type="text/javascript"></script>
    <title></title>
      <script type="text/javascript">

          function pageLoad() {
          debugger
              $("#Gridview1").tablesorter();//for sorting table

              $("#txtName").quicksearch("table#Gridview1 tbody tr", {
                  noResults: '#GridView1_noresults',
                  stripeRows: ['odd', 'even'],
                  loader: 'span.loading'
              });

          }
       
    </script>
</head>

Add serch textbox on page....


Search:<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                           <span class="loading">Loading...<img alt="Loading..." src="../Icon/loader.gif" /></span><br />


//bind code of the gridview

Gridview1.Databind();
Gridview1.UseAccessibleHeader = true;
Gridview1.HeaderRow.TableSection = TableRowSection.TableHeader;

Sample:











Download:

http://autobahn.tablesorter.com/jquery.tablesorter.min.js

https://github.com/riklomas/quicksearch/blob/master/jquery.quicksearch.js

http://code.jquery.com/jquery-1.6.1.min.js

Monday, 22 August 2011

Regular Expression for a Float

^[0-9]*[0-9]+[.]?[0-9]*$
output:
123300             Valid
123300.            Valid
123300.00        Valid
123300..          Invalid
123300..0        Invalid






Sunday, 17 July 2011

C# DateTime Ticks Property

The DateTime Ticks property simply returns a long that represents the amount of ticks (100-nanosecond interval) that have passed since january 1, 0001 12:00:00.

using System;
class Program
{
    static void Main()
    {
        DateTime variable = DateTime.Now;
        long ticks = variable.Ticks;
        Console.WriteLine(ticks);
        Console.Read();
    }
}

output:633117518171562500


Convery Ticks Back to DateTime

Now that we have converted a DateTime object to a Ticks now we have to learn how to convert is back to a DateTime value.

using System;
class Program
{
    static void Main()
    {
        long ticks = 633117518171562500;
        DateTime date = new DateTime(ticks);
        Console.WriteLine(date.ToString());
        Console.Read();
    }
}


Output: 4/9/2007 9:43:37 PM


Thursday, 14 July 2011

Show a word document content in asp.net page

using Microsoft.Office.Interop.Word;
//fuction
  private void readFileContent(string path)

  {

       ApplicationClass wordApp = new ApplicationClass();

      object file = path;

      object nullobj = System.Reflection.Missing.Value;       

      Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(

      ref file, ref nullobj, ref nullobj,

      ref nullobj, ref nullobj, ref nullobj,

      ref nullobj, ref nullobj, ref nullobj,

      ref nullobj, ref nullobj, ref nullobj,

      ref nullobj, ref nullobj, ref nullobj, ref nullobj);

      doc.ActiveWindow.Selection.WholeStory();

      doc.ActiveWindow.Selection.Copy();

      string sFileText = doc.Content.Text;

      doc.Close(ref nullobj, ref nullobj, ref nullobj);

      wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);       

      Response.Write(sFileText);

   }

Thursday, 16 June 2011

Jquery validate for gridview inner control(Lable,Textbox) on button click Event


   $('#btnSubmit').click(function () {
             var isValid = true;
                $("table[id$='gvPurchase']").find("span.validate").each(function () {
                //span use for lable control and validate is class of the control
                // <asp:Label ID="Label2" runat="server" class="validate" Text="0"></asp:Label>               
                    if ($(this).html() == "") {
                        $("#gvValidate").html("*Rate and quantity not be null");
                        isValid = false;
                    }
                    if (parseInt($(this).html()) <= 0) {

                        $("#gvValidate").html("*Rate and quantity must be grater then zero");
                        isValid = false;
                    }

                });

Friday, 27 May 2011

Change image size in asp.net with C#.

System.Drawing.Image image = System.Drawing.Image.FromFile(url+"/"+filename);
            int srcWidth = image.Width;
            int thumbWidth;
            int srcHeight = image.Height;
            int thumbHeight = 440;
            Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);
            System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
            gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            System.Drawing.Rectangle rectDestination = new 
            System.Drawing.Rectangle(0, 0, thumbWidth,  thumbHeight);
            gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
            string newfilename = filename.Substring(0, filename.LastIndexOf('.'))+reference;
            string fileExt=filename.Substring(filename.LastIndexOf('.'));
            bmp.Save(url + "\\" + newfilename + fileExt);
            bmp.Dispose();           
            image.Dispose();
         

Thursday, 19 May 2011

What is httpHandler & httpModule?

httpHandler – Extenstion Based Preprocessor :
ASP.NET HTTP handler is the process that runs in response to a request made to an ASP.NET Web application. The most common handler is an ASP.NET page handler that processes .aspx files. When users request an .aspx file, the request is processed by the page through the page handler. You can create your own HTTP handlers that render custom output to the browser.
httpModule – Event based Preprocessor :
An HTTP module is an assembly that is called on every request that is made to your application. HTTP modules are called as part of the ASP.NET request pipeline and have access to life-cycle events throughout the request. HTTP modules let you examine incoming and outgoing requests and take action based on the request.
When to use HTTP handlers:
  • RSS feeds: To create an RSS feed for a Web site, you can create a handler that emits RSS-formatted XML. You can then bind a file name extension such as .rss to the custom handler. When users send a request to your site that ends in .rss, ASP.NET calls your handler to process the request.
  • Image server:   If you want a Web application to serve images in a variety of sizes, you can write a custom handler to resize images and then send them to the user as the handler’s response.
When to use HTTP modules:
  • Security:   Because you can examine incoming requests, an HTTP module can perform custom authentication or other security checks before the requested page, XML Web service, or handler is called. In Internet Information Services (IIS) 7.0 running in Integrated mode, you can extend forms authentication to all content types in an application.
  • Statistics and logging:   Because HTTP modules are called on every request, you can gather request statistics and log information in a centralized module, instead of in individual pages.
  • Custom headers or footers:   Because you can modify the outgoing response, you can insert content such as custom header information into every page or XML Web service response.
Main Features:
  • The IHttpHandler and IHttpModule interfaces are the starting point for developing handlers and modules.
    The IHttpAsyncHandler interface is the starting point for developing asynchronous handlers.
  • Custom handler and module source code can be put in the App_Code folder of an application, or it can be compiled and put in the Bin folder of an application.
  • Handlers and modules developed for use in IIS 6.0 can be used in IIS 7.0 with little or no change.
  • Modules can subscribe to a variety of request-pipeline notifications. Modules can receive notification of events of the HttpApplication object.
Built-in HTTP Handlers in ASP.NET
  • ASP.NET page handler (*.aspx): The default HTTP handler for all ASP.NET pages.
  • Web service handler (*.asmx): The default HTTP handler for Web service pages created as .asmx files in ASP.NET.
  • Generic Web handler (*.ashx): The default HTTP handler for all Web handlers that do not have a UI and that include the @ WebHandler directive.
  • Trace handler (trace.axd): A handler that displays current page trace information. 

“Modules are called before and after the handler executes. Modules enable developers to intercept, participate in, or modify each individual request. Handlers are used to process individual endpoint requests. Handlers enable the ASP.NET Framework to process individual HTTP URLs or groups of URL extensions within an application. Unlike modules, only one handler is used to process a request”.


    Table Partitioning in SQL Server

      Table Partitioning in SQL Server – Step by Step Partitioning in SQL Server task is divided into four steps: Create a File Group Add Files ...