Friday, December 4, 2009

VSTS Code Analysis Rules for .NET development project

Here is the list of Code Analysis Rules which can be used for any .NET development project.




CA1001
CA1001: Types that own disposable fields should be disposable
CA1009
CA1009: Declare event handlers correctly
CA1016
CA1016: Mark assemblies with AssemblyVersionAttribute
CA1033
CA1033: Interface methods should be callable by child types
CA1049
CA1049: Types that own native resources should be disposable
CA1060
CA1060: Move P/Invokes to NativeMethods class
CA1061
CA1061: Do not hide base class methods
CA1063
CA1063: Implement IDisposable correctly
CA1065
CA1065: Do not raise exceptions in unexpected locations
CA1301
CA1301: Avoid duplicate accelerators
CA1400
CA1400: P/Invoke entry points should exist
CA1401
CA1401: P/Invokes should not be visible
CA1403
CA1403: Auto layout types should not be COM visible
CA1404
CA1404: Call GetLastError immediately after P/Invoke
CA1405
CA1405: COM visible type base types should be COM visible
CA1410
CA1410: COM registration methods should be matched
CA1415
CA1415: Declare P/Invokes correctly
CA1900
CA1900: Value type fields should be portable
CA1901
CA1901: P/Invoke declarations should be portable
CA2000
CA2000: Dispose objects before losing scope
CA2002
CA2002: Do not lock on objects with weak identity
CA2100
CA2100: Review SQL queries for security vulnerabilities
CA2101
CA2101: Specify marshaling for P/Invoke string arguments
CA2108
CA2108: Review declarative security on value types
CA2111
CA2111: Pointers should not be visible
CA2112
CA2112: Secured types should not expose fields
CA2114
CA2114: Method security should be a superset of type
CA2116
CA2116: APTCA methods should only call APTCA methods
CA2117
CA2117: APTCA types should only extend APTCA base types
CA2122
CA2122: Do not indirectly expose methods with link demands
CA2123
CA2123: Override link demands should be identical to base
CA2124
CA2124: Wrap vulnerable finally clauses in outer try
CA2126
CA2126: Type link demands require inheritance demands
CA2136
CA2136: Members should not have conflicting transparency annotations
CA2140
CA2140: Transparent code must not reference security critical items
CA2147
CA2147: Transparent methods may not use security asserts
CA2207
CA2207: Initialize value type static fields inline
CA2212
CA2212: Do not mark serviced components with WebMethod
CA2213
CA2213: Disposable fields should be disposed
CA2214
CA2214: Do not call overridable methods in constructors
CA2216
CA2216: Disposable types should declare finalizer
CA2220
CA2220: Finalizers should call base class finalizer
CA2229
CA2229: Implement serialization constructors
CA2232
CA2232: Mark Windows Forms entry points with STAThread
CA2235
CA2235: Mark all non-serializable fields
CA2236
CA2236: Call base class methods on ISerializable types
CA2237
CA2237: Mark ISerializable types with SerializableAttribute
CA2238
CA2238: Implement serialization methods correctly
CA2240
CA2240: Implement ISerializable correctly
CA2242
CA2242: Test for NaN correctly
CA1000
CA1000: Do not declare static members on generic types
CA1002
CA1002: Do not expose generic lists
CA1020
CA1020: Avoid namespaces with few types
CA1031
CA1031: Do not catch general exception types
CA1051
CA1051: Do not declare visible instance fields
CA1053
CA1053: Static holder types should not have constructors
CA1500
CA1500: Variable names should not match field names
CA1709
CA1709: Identifiers should be cased correctly
CA1726
CA1726: Use preferred terms
CA1804
CA1804: Remove unused locals
CA1811
CA1811: Avoid uncalled private code
CA1820
CA1820: Test for empty strings using string length
CA1822
CA1822: Mark members as static
CA1823
CA1823: Avoid unused private fields
CA2200
CA2200: Rethrow to preserve stack details
CA2201
CA2201: Do not raise reserved exception types
CA1007
CA1007: Use generics where appropriate
CA1011
CA1011: Consider passing base types as parameters
CA1050
CA1050: Declare types in namespaces
CA1054
CA1054: URI parameters should not be strings
CA1055
CA1055: URI return values should not be strings
CA1056
CA1056: URI properties should not be strings
CA1505
CA1505: Avoid unmaintainable code
CA1506
CA1506: Avoid excessive class coupling
CA1800
CA1800: Do not cast unnecessarily
CA1802
CA1802: Use literals where appropriate
CA1814
CA1814: Prefer jagged arrays over multidimensional


Code to create .xoml file from WF workflow using C#

Here is the simple code to create .xoml file from WF workflow;


using (XmlWriter writer = XmlWriter.Create(@"D:\Temp\MyWorkflow.xoml"))
            {
                WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
                SimpleWorkFlow sm = new SimpleWorkFlow();

                serializer.Serialize(writer, sm);
            }

Invoking WF with Input & Output parameters using C#

Here is the simple code for invoking the WF with Input and Output parameters using C#


Dictionary<string, object>  parameters = new Dictionary<string, object>();
            Employer emp = new Employer();
            emp.FirstName = txtFirstName.Text;
            emp.LastName = txtLastName.Text;
            emp.DOB = txtDOB.SelectedDate;
            emp.Email = txtEmail.Text;
            parameters["Emp"] = emp;
            using (WorkflowRuntime workflowRuntime = new WorkflowRuntime())
            {
                AutoResetEvent waitHandle = new AutoResetEvent(false);
                workflowRuntime.WorkflowCompleted += delegate(object sender1,
                    WorkflowCompletedEventArgs e1)
                {
                    lblResult.Text = e1.OutputParameters["Result"].ToString();
                    waitHandle.Set();
                };

                workflowRuntime.WorkflowTerminated += delegate(object sender2,
                    WorkflowTerminatedEventArgs e2)
                {
                    lblResult.Text = e2.Exception.Message;
                    waitHandle.Set();
                };

                WorkflowInstance instance =
                    workflowRuntime.CreateWorkflow(typeof(EmployerWorkFlow), parameters);

                instance.Start();

                waitHandle.WaitOne();
            }

Collecting Data from web parts and submitting to back end using C#

There can be scenarios, wherein you may have to collect data from various web parts in a page and submit to back end. One solution will be to use Connected Web parts feature. Or the sample code below also will help to achieve this;


1) Define one base class
public abstract class IBaseForm : WebPart
    {

    }



2) Implement this class in all the web parts whose data need to be collected
public class InputForm2 : IBaseForm
    {
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            TextBox t1 = new TextBox();
            t1.ID = "LastName";
            t1.Text = "Last Name";
            this.Controls.Add(t1);
        }
    }



3) This is the piece of code which will collect the data from all the web parts which implement the base class
public class SubmitButton : WebPart
    {
        TextBox t1 = new TextBox();
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            Button b1 = new Button();
            b1.ID = "submitData";
            b1.Text = "Submit Data";
            b1.Click += new EventHandler(b1_Click);
            this.Controls.Add(b1);

            t1.ID = "result";
            this.Controls.Add(t1);
        }

        void b1_Click(object sender, EventArgs e)
        {
            t1.Text = string.Empty;
            System.Web.UI.ControlCollection colls = this.Parent.Controls;
            IEnumerable bf= colls.OfType();
            foreach (IBaseForm f in bf)
            {
                t1.Text = t1.Text + GetValue(f);
            }
        }

        string GetValue(IBaseForm f)
        {
            string res = string.Empty;
            TextBox t = default(TextBox);

            foreach (System.Web.UI.Control c in f.Controls)
            {
                if (c.GetType().Equals(typeof(TextBox)))
                    t = c as TextBox;
                break;
            }
            res = t.Text;
            return res;
        }
    }

Wednesday, December 2, 2009

To disable "Use command line option '/keyfile' or appropriate project settings instead of 'AssemblyKeyFile'"

This is what will help to avoid this warning;


// disable warning about using /keyfile instead of AssemblyKeyFile
#pragma warning disable 1699
[assembly: AssemblyKeyFile(@"..\AAS.Portal.snk")]
#pragma warning restore 1699