Friday 15 March 2013

Creating your Own Snippets Using Visual Studio 2010

Hi friends,

In this article I will tell you how to create your own snippets using visual studio 2010

Sometimes you have your own code and it is used in many projects and used in each and every file. Instead of copy and pasting this code you can create your own Snippets in Visual Studio 2010. That will save your times to rewriting block of code


 For example in MVVM you have to implement iNotifyPropertyChanged notifier and this method is used in many properties.

Part 1: Creating Snippet File


There are 3 types of Snippets are available.

   1. Expansion
   2. Surrounds With
   3. Refactoring
In this article I am creating expansion type of snippet for Property block with MVVM functionality that is iNotifyPropertyChanged. IntelliSense Code Snippets created in XML file.


Step 1: First Create XML file in Visual Studio 2010 and save file as PropMVVM.snippet.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

Code Snippet file start with CodeSnippets element which has xmlns attribute for snippet.


Step 2: Add CodeSnippet Section  
 <?xml version="1.0" encoding="utf-8"?>
 <CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">

Step 3: Add Header Section within Code Snippet Section.
    <Header>
      <Title>PropMVVM </Title>
      <Shortcut>PropMVVM </Shortcut>
      <Description>
     This Code snippet for property with Raisepropertychanged event
      </Description>
      <Author>Sujeet Bhujbal</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>

This section has elements like Title, shortcut for IntelliSense and Description.
Title: Title for Snippet
Shortcut: when you type using Intelligence it will shows list of snippet including which you have created with given shortcut.
Description:   Detailed description of snippet on Mouse Over of you snippet.
Author: Author Name of snippet.
Snippet Type:  which type of snippet are you going to create like Expansion (prop) or Surround with (try/catch).
      

Step 4:   Create Declaration section and then add Literals or Objects within Declaration Section.
Literal Elements are editable values that are inserted by user into snippet.
In the below code I have created Three Literals.

First Literal describes Type of property or variable, I have set Default value to int (you can give any datatypes including (double, bool ,string etc.)

Second Literal  element is for editable name of Property.

Third Literal element has field, user can gives any variable name that used within property Get/Set.

<Declarations> 
   <Literal>
          <ID>type</ID>
          <ToolTip>Property Type</ToolTip>
          <Default>int</Default>
   </Literal>
   <Literal>
          <ID>property</ID>
          <ToolTip>Property Name</ToolTip>
          <Default>MyProperty</Default>
   </Literal>
   <Literal>
          <ID>field</ID>
          <ToolTip>The variable baking this property</ToolTip>
          <Default>MyVariable</Default>
   </Literal>
</Declaration>




Step 5: Code section describes the code for that snippets. In this snippet is added language is CSharp, Inside the Code element, add the C# code for the snippet.
   <Code Language="CSharp">
        <![CDATA[private $type$ $field$;

 public $type$ $property$
 {
  get { return $field$;}
  set { $field$ = value;
    RaisePropertyChanged("$property$");
    }
 }
 $end$]]>



Step 6:

Below is  Full xml of Property block Snippet
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>propmvvm</Title>
      <Shortcut>propmvvm</Shortcut>
      <Description>Code snippet for property with raisepropertychanged event</Description>
      <Author>Microsoft</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>type</ID>
          <ToolTip>Property Type</ToolTip>
          <Default>int</Default>
        </Literal>
        <Literal>
          <ID>property</ID>
          <ToolTip>Property Name</ToolTip>
          <Default>MyProperty</Default>
        </Literal>
        <Literal>
          <ID>field</ID>
          <ToolTip>The variable baking this property</ToolTip>
          <Default>MyVariable</Default>
        </Literal>
      </Declarations>
      <Code Language="CSharp">
        <![CDATA[private $type$ $field$;

 public $type$ $property$
 {
  get { return $field$;}
  set { $field$ = value;
    RaisePropertyChanged("$property$");
    }
 }
 $end$]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>


Step 7: Save snippet file as MyCodeSnippet.snippet

Part 2 : Visual Studio Setting


Step 1: Open Visual Studio and click on Tool à Code Snippets Manager item.






Step 2:  It will show code snippet manager dialog box




Step 3:  Select location for snippets and click on Finish button, your code snippet imported successfully.
Now you can use your snippet in development.





Step 4:  Then Press OK to save the snippet in VS 2010.
Create class file in your project.
In that file start typing with you propmvvm
it will display list of IntelliSense code snippets including your snippetType propmvvm
 You will find following

Then MVVM property snippet is added in your project successfully.


In this way you can create your own snippets.

Happy Programming!!
Don’t forget to leave your feedback and comments below!
If you have any query mail me to Sujeet.bhujbal@gmail.com     
Regards
Sujeet Bhujbal
--------------------------------------------------------------------------------
----------------------------------------------------------------------------------

Tuesday 12 March 2013

Calling WCF/Web Service Without Adding Service Reference , by using ChannelFactory


Hi friends,

In this article i will tell you how to call WCF service without creating service reference


Sometimes we need to calling service without adding service reference.
The ChannelFactory object is used to create a communication channel with the service. Note that the BasicHttpBinding is used (because it was used in configuration of the service's host) and the same URL address

The used ChannelFactory object is created for the service's interface  and is able to create an object implementing this interface which can be easily used in the code 
Following steps are required for creating channelFactory
Step 1 : Initialize Channel factory   

Step  2 : create channel factory

Step  3 :  Access Proxy

 
Example :
 
Let Assume that your service name is ConfigurationServices and it has method getCustomerDetails
we retrieve User Details based on UserId using getCustomerDetails method.   

Below is the code snippet.

var factory = new ChannelFactory<ConfigurationServices>(new BasicHttpBinding(), 
           new EndpointAddress("http://localhost/HirenWebServices/ConfigurationServices"));

 
 
var proxy = factory.CreateChannel();

var response = proxy.getCustomerDetails(
           new getCustomerDetails() { arg0 = AppConfiguration.Current.Userid });

var v = response.@return;





Happy Programming!!
Don’t forget to leave your feedback and comments below!
If you have any query mail me to Sujeet.bhujbal@gmail.com     
Regards
Sujeet Bhujbal
--------------------------------------------------------------------------------
-----------------------------------------------------------------------------------