Friday, May 30, 2014

CA2214 : Do not call overridable methods in constructors Workaround

When you seriously take .net code analysis you may either try to suppress them or try to fix it. There is one such code analysis review if have override or virtual method call in constructor.

 
http://stackoverflow.com/questions/17018569/accessing-an-implemented-abstract-property-in-the-constructor-causes-ca2214-do

http://blogs.msdn.com/b/ericlippert/archive/2008/02/18/why-do-initializers-run-in-the-opposite-order-as-constructors-part-two.aspx

http://blog.duncanworthy.me/swdev/csdotnet/constructor-gotchas-part1/

http://stackoverflow.com/questions/119506/virtual-member-call-in-a-constructor


When an object written in C# is constructed, what happens is that the initializers run in order from the most derived class to the base class, and then constructors run in order from the base class to the most derived class
Problem - Constructor has call to override method.



 
 Workaround :- Add Sealed


 

Monday, May 19, 2014

Powershell Scripting: Automated scripting of database DDL

Tips # OneNote Copy Text From Image

How to extract text from image-Use One Note

1. Copy Image to onenote
2. Right click on image select Copy text from Image
3. Paste it.

 

Sunday, May 18, 2014

Tips #-.Net Reflection to fetch value of Object Property.

Runtime Activity- I want to pass a class and property in the string and resolve the value of the property.

 With Generics

Generics

Friday, May 2, 2014

Get website details such as physical path using Powershell



Import-Module "WebAdministration" -ErrorAction Stop

foreach($site in (dir iis:\sites\*WebSiteNameXyz*))



{

write-host $site.Name

write-host $site.Bindings

write-host $site.State

write-host $site.physicalpath



}

Power of Powershell remoting WSMan

If you want to execute powershell script from your local box onto remote server this is the best solution to do.

Before you take deep dive in below powershell script . Ensure few things in local source and destination remote server settings.
* Ensure WinRs windows remote services . Type services.msc and this service should be enabled and auto mode.
* Ensure WSMan is enabled
Use gpedit.msc and look at the following policy:
 
Computer Configuration ->
Administrative Templates ->
System ->
Credentials Delegation -> Allow Delegating Fresh Credentials. 
 
Verify that it is enabled and configured with an SPN appropriate for the target computer.C lick on add server list mention WSMAN/myserver.domain.com
 

set-item wsman:localhost\client\trustedhosts -value *

#Enable credssp authentication
Enable-WSManCredSSP -Role Client –DelegateComputer *

# This will establish server connection
Connect-WSMan myserver

invoke-command -computername myserver-authentication credssp -credential domain\username -filepath c:\dbtest.ps1

  

This will prompt for password and you are good to go.
Now this dbtest.ps1 can be your powershell script which will deploy code base to different environment.

 
Useful References


Write to event log
http://blogs.technet.com/b/heyscriptingguy/archive/2013/06/20/how-to-use-powershell-to-write-to-event-logs.aspx

Monitor Scheduled Job
http://blogs.technet.com/b/heyscriptingguy/archive/2014/04/29/powertip-use-powershell-to-show-state-of-scheduled-tasks.aspx

 
Database administration