Saturday 31 March 2012

Garbage Collection

About Garbage Collection
The .NET Framework's garbage collector manages the allocation and release of memory for your application. Each time you create a new object, the common language runtime allocates memory for the object from the managed heap. As long as address space is available in the managed heap, the runtime continues to allocate space for new objects. However, memory is not infinite. Eventually the garbage collector must perform a collection in order to free some memory. The garbage collector's optimizing engine determines the best time to perform a collection, based upon the allocations being made. When the garbage collector performs a collection, it checks for objects in the managed heap that are no longer being used by the application and performs the necessary operations to reclaim their memory.


Fundamentals of Memory


The following list summarizes important CLR memory concepts.
  • Each process has its own, separate virtual address space. All processes on the same computer share the same physical memory, and share the page file if there is one.
  • By default, on 32-bit computers, each process has a 2-GB user-mode virtual address space.
  • As an application developer, you work only with virtual address space and never manipulate physical memory directly. The garbage collector allocates and frees virtual memory for you on the managed heap.
    If you are writing native code, you use Win32 functions to work with the virtual address space. These functions allocate and free virtual memory for you on native heaps.
  • Virtual memory can be in three states:
    • Free. The block of memory has no references to it and is available for allocation.
    • Reserved. The block of memory is available for your use and cannot be used for any other allocation request. However, you cannot store data to this memory block until it is committed.
    • Committed. The block of memory is assigned to physical storage.
  • Virtual address space can get fragmented. This means that there are free blocks, also known as holes, in the address space. When a virtual memory allocation is requested, the virtual memory manager has to find a single free block that is large enough to satisfy that allocation request. Even if you have 2 GB of free space, the allocation that requires 2 GB will be unsuccessful unless all of that space is in a single address block.
  • You can run out of memory if you run out of virtual address space to reserve or physical space to commit.
Your page file is used even if physical memory pressure (that is, demand for physical memory) is low. The first time your physical memory pressure is high, the operating system must make room in physical memory to store data, and it backs up some of the data that is in physical memory to the page file. That data is not paged until it is needed, so it is possible to encounter paging in situations where the physical memory pressure is very low.




Garbage collection occurs when one of the following conditions is true:
  • The system has low physical memory.
  • The memory that is used by allocated objects on the managed heap surpasses an acceptable threshold. This means that a threshold of acceptable memory usage has been exceeded on the managed heap. This threshold is continuously adjusted as the process runs.
  • The GC.Collect method is called. In almost all cases, you do not have to call this method, because the garbage collector runs continuously. This method is primarily used for unique situations and testing.


    GC.Collect Method

    Use this method to try to reclaim all memory that is inaccessible.
    All objects, regardless of how long they have been in memory, are considered for collection; however, objects that are referenced in managed code are not collected. Use this method to force the system to try to reclaim the maximum amount of available memory.


    using System;
    
    namespace GCCollectExample
    {
        class MyGCCollectClass
        {
            private const int maxGarbage = 1000;
    
            static void Main()
            {
                // Put some objects in memory.
                MyGCCollectClass.MakeSomeGarbage();
                Console.WriteLine("Memory used before collection: {0}", GC.GetTotalMemory(false));
    
                // Collect all generations of memory.
                GC.Collect();
                Console.WriteLine("Memory used after full collection: {0}", GC.GetTotalMemory(true));
            }
    
            static void MakeSomeGarbage()
            {
                Version vt;
    
                for(int i = 0; i < maxGarbage; i++)
                {
                    // Create objects and release them to fill up memory
                    // with unused objects.
                    vt = new Version();
                }
            }
        }
    }
    
    


Thursday 29 March 2012

adding webservice with authorization in linkedIn. In silverlight (getting verifier)

Step 1:
we need to create a web service with the fuction:

In OauthlinkedIn.cs

public string AuthorizationLinkGet()
        {
//here you need to change your callback url.
            string oauth_callback="http://localhost/DataAccess.asmx";
            string ret = null;          
            string response = oAuthWebRequest(Method.POST, REQUEST_TOKEN, String.Empty,oauth_callback);
            if (response.Length > 0)            {
                //response contains token and token secret.  We only need the token.
                //oauth_token=36d1871d-5315-499f-a256-7231fdb6a1e0&oauth_token_secret=34a6cb8e-4279-4a0b-b840-085234678ab4&oauth_callback_confirmed=true
                NameValueCollection qs = HttpUtility.ParseQueryString(response);               
                if (qs["oauth_token"] != null)
                {
                    this.Token = qs["oauth_token"];
                    this.TokenSecret = qs["oauth_token_secret"];
                 
                    ret = AUTHORIZE + "?oauth_token=" + this.Token;                 

                }
            }


In default.asmx.cs (In webservice)



public string CreateAuthorization()
        {        
            string auth_link = _oauth.AuthorizationLinkGet();                     
            string auth_token = _oauth.Token;
            string auth_tokensecret = _oauth.TokenSecret;
            //string auth_verify = _oauth.AccessTokenGet(auth_token);
            string auth_verifier = _oauth.Verifier;             
            return auth_token;
        }
we can call this function in silverlight.


public partial class MainPage : UserControl
    {
        public const string AUTHORIZE = "https://api.linkedin.com/uas/oauth/authorize";
        public string oauth_callback ="http://localhost/DataAccess.asmx";
        public MainPage()
        {
            InitializeComponent();
            Service1SoapClient proxy = new Service1SoapClient();
            proxy.CreateAuthorizationCompleted+=new EventHandler<CreateAuthorizationCompletedEventArgs>(proxy_createauthorizationcompleted);
           proxy.CreateAuthorizationAsync();
        }

        private void proxy_createauthorizationcompleted(object sender, CreateAuthorizationCompletedEventArgs e)
        {
            try
            {               
               string  ret = AUTHORIZE + "?oauth_token=" + e.Result.ToString();
               System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(ret));
               //NavigationService.Equals(ret, oauth_callback);               
              //  HttpContext.Current.Response.Redirect(ret);
                textBox1.Text = "Oauth_token :  " + e.Result.ToString();
            }
            catch
            {
                textBox1.Text = e.Result.ToString();
            }
        }
    }

result:
you  will redirect to linked in page, if you click on the continue, it will call back to localhost.

if you check in fiddler. you will get the verifier also. 
 




Wednesday 28 March 2012

how to get the oauth_verifier by sending oauth_token

    public string AuthorizationLinkGet()
        {
            string oauth_callback="http://localhost/DataAccess.asmx";
            string ret = null;
            //string rett = null;
            string response = oAuthWebRequest(Method.POST, REQUEST_TOKEN, String.Empty,oauth_callback);
            if (response.Length > 0)
            {
                //response contains token and token secret.  We only need the token.
                //oauth_token=36d1871d-5315-499f-a256-7231fdb6a1e0&oauth_token_secret=34a6cb8e-4279-4a0b-b840-085234678ab4&oauth_callback_confirmed=true
                NameValueCollection qs = HttpUtility.ParseQueryString(response);
               
                if (qs["oauth_token"] != null)
                {
                    this.Token = qs["oauth_token"];
                    this.TokenSecret = qs["oauth_token_secret"];
                    //this.Verifier = qs["oauth_verifier"];
                    ret = AUTHORIZE + "?oauth_token=" + this.Token;
                   HttpContext.Current.Response.Redirect(ret);
                   

                }
            }
       

           
            return ret;
        }

Wednesday 21 March 2012

crossdomain.xml and clientaccesspolicy.xml

clientaccesspolicy.xml

<?xml version="1.0" encoding="utf-8" ?>
<!--<?xml version="1.0" encoding="utf-8" ? >-->
<access-policy>
  <cross-domain-access>
    <policy>
      <!--<allow-from >-->
      <allow-from http-request-headers="SOAPAction">

        <domain uri="https://*"/>
        <domain uri="http://*"/>

        <!--<domain url="*"></domain>-->
      </allow-from>
      <grant-to>
        <resource include-subpaths="true" path="/"></resource>
      </grant-to>
    </policy>
  </cross-domain-access>

</access-policy>


crossdomain.xml

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE cross-domain-policy SYSTEM
"http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
  <allow-http-request-headers-from domain="*" headers="*"/><!--//"SOAPAction,Content-Type"/>-->
</cross-domain-policy>

403.14 error and 404 errors

  1. this is for 403.14
  2. Start IIS Manager. To do this, click Start, click Run, type inetmgr.exe, and then click OK.
  3. In IIS Manager, expand server name, expand Web sites, and then click the Web site that you want to modify.
  4. In Features view, double-click Directory Browsing.
  5. In the Actions pane, click Enable.
  6. check the link for this
  7. http://support.microsoft.com/kb/942062/

for 404 errors:
http://www.getnetgoing.com/HTTP-404.html
http://www.getnetgoing.com/HTTP-404.html 

how to deploy and test a custom application.

create a custom application. ( i created custom application in asp.net)
place this custom application folder in C:\Inetpub\wwwroot

then go to start- run- inetmgr

create new website, give physical path
c:\inetpub\wwwroot\myapplication.

then right click on website icon, click on browse.
you will get the result in internet explorer.

how to deploy a silverlight application with webservices in dynamic crm 2011

first create a silverlight application with webservices. (I did with asp.net webservice).
place it in :
c:\Programfile\dynamic crm\crmweb\isv.
ex: C:\Program Files\Microsoft Dynamics CRM\CRMWeb\ISV\forwebsersilver\forwebsersilver\Bin\Debug


place both the files. webservice file and silverlight file.
then call it in IE or mozilla.

ex: http://servername/ISV/forwebsersilver/forwebsersilver/Bin/Debug/forwebsersilverTestPage.html
here first we can give the server name and call the html file to give the result.


if you want to deploy silverlight application with wcf the follow the below link
http://www.michaelsnow.com/2010/05/03/silverlight-tip-of-the-day-11-deploying-silverlight-applications-with-wcf-web-services/
 

Monday 12 March 2012

how to add buttons on home page in crm 2011

for this:
first : http://gtcrm.wordpress.com/2011/08/24/adding-a-new-button-group-to-the-crm-ribbon/
first go for the SDK :sdk\resources\exportedribbonxml
here you will get the .xml for every enity.
from there we will get the location of the entity.
<CustomActions>
            <CustomAction Id="Mscrm.HomepageGrid.new_cooldrink.CustomGroup.CustomAction"
                          Location="Mscrm.HomepageGrid.new_cooldrink.MainTab.Groups._children"
                          Sequence="110">
              <CommandUIDefinition>
                <Group Id="Mscrm.HomepageGrid.new_cooldrink.CustomGroup.Group"
                        Command= "Mscrm.HomepageGrid.new_cooldrink.CustomGroup.Command"
                      
                        Title="custom groups"
                        Sequence="51"
                        Template="Mscrm.Templates.Flexible2">
                  <Controls Id="Mscrm.HomepageGrid.new_cooldrink.CustomGroup.Controls">
                    <Button Id="Mscrm.HomepageGrid.new_cooldrink.CustomGroup.Button.A"
                            Command="Mscrm.HomepageGrid.new_cooldrink.CustomGroup.Button.A.Command"
                            Sequence="100"
                            LabelText="Connect to linkedIn"
                            ToolTipTitle="TipTitle"
                            ToolTipDescription="TipDescription"
                            TemplateAlias="o1"
                            Image16by16="/_imgs/ribbon/newchart16.png"
                            Image32by32="/_imgs/ribbon/newchart32.png"  />
                    <Button Id="Mscrm.HomepageGrid.new_cooldrink.CustomGroup.Button.B"
                            Command="Mscrm.HomepageGrid.new_cooldrink.CustomGroup.Button.B.Command"
                            Sequence="120"
                            LabelText="Import Company Details"
                            ToolTipTitle="TipTitle"
                            ToolTipDescription="TipDescription"
                            TemplateAlias="o1"
                            Image16by16="/_imgs/ribbon/CustomEntity_16.png"
                            Image32by32="/_imgs/ribbon/CustomEntity_32.png"   />

                    <Button Id="Mscrm.HomepageGrid.new_cooldrink.CustomGroup.Button.C"
                            Command="Mscrm.HomepageGrid.new_cooldrink.CustomGroup.Button.C.Command"
                            Sequence="130"
                            LabelText="Import Contact Details"
                            ToolTipTitle="TipTitle"
                            ToolTipDescription="TipDescription"
                            TemplateAlias="o1"
                            Image16by16="/_imgs/ribbon/CustomEntity_16.png"
                            Image32by32="/_imgs/ribbon/CustomEntity_32.png"   />
                   
                  </Controls>
                </Group>
              </CommandUIDefinition>
            </CustomAction>
            <CustomAction Id="Mscrm.HomepageGrid.new_cooldrink.CustomGroup.MaxSize.CustomAction"
                          Location="Mscrm.HomepageGrid.new_cooldrink.MainTab.Scaling._children"
                          Sequence="120">
              <CommandUIDefinition>
                <MaxSize  Id="Mscrm.HomepageGrid.new_cooldrink.CustomGroup.MaxSize"
                          GroupId="Mscrm.HomepageGrid.new_cooldrink.CustomGroup.Group"
                          Sequence="21"
                          Size="LargeLarge" />
              </CommandUIDefinition>
            </CustomAction>
            <CustomAction Id="Mscrm.HomepageGrid.new_cooldrink.CustomGroup.Popup.CustomAction"
                          Location="Mscrm.HomepageGrid.new_cooldrink.MainTab.Scaling._children"
                          Sequence="140">
              <CommandUIDefinition>
                <Scale    Id="Mscrm.HomepageGrid.new_cooldrink.CustomGroup.Popup.1"
                          GroupId="Mscrm.HomepageGrid.new_cooldrink.CustomGroup.Group"
                          Sequence="87"
                          Size="Popup" />
              </CommandUIDefinition>
            </CustomAction>
          </CustomActions>

In Command Definition:

<CommandDefinitions>
            <CommandDefinition Id="Mscrm.HomepageGrid.new_cooldrink.CustomGroup.Button.A.Command">
              <EnableRules />
              <DisplayRules />
              <Actions>
                <Url Address="http://www.google.com" />
              </Actions>
            </CommandDefinition>
            <CommandDefinition Id="Mscrm.HomepageGrid.new_cooldrink.CustomGroup.Button.B.Command">
              <EnableRules />
              <DisplayRules />
              <Actions>
                <Url Address="http://www.google.com" />
              </Actions>
            </CommandDefinition>
            <CommandDefinition Id="Mscrm.HomepageGrid.new_cooldrink.CustomGroup.Button.C.Command">
              <EnableRules />
              <DisplayRules />
              <Actions>
                <Url Address="http://www.google.com" />
              </Actions>
            </CommandDefinition>
            <CommandDefinition Id="Mscrm.HomepageGrid.new_cooldrink.CustomGroup.Command">
              <EnableRules>
                <!--<EnableRule Id="Mscrm.ConvertActivity" />-->
              </EnableRules>
              <DisplayRules>
                <!--<DisplayRule Id="Mscrm.ConvertActivity" />-->
              </DisplayRules>
              <Actions />
            </CommandDefinition>
          </CommandDefinitions>

Change your entity accordingly.