Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Monday, November 27, 2017

Why and How to implement Dependency Injection Pattern?

Benefits

By using dependency injection, you solve two main problems,

1- Unit Test
2- Instantiation

Unit Test

You can resolve this by implementing interface of every class that is required to instantiated in any method or implementation. Always pass interface as parameter in constructor rather than making variables and instantiate in the class. For unit test, you can send mocked (the dummy) instantiation of that interface to that class.

Before

        public class Order
        {
            public string ID { get; set; }
            public string Detail { get; set; }

            public void Process()
            {
            }
        }
        public class Cart
        {
            public void ProcessOrder()
            {
                Order order = new Order();
                order.Process();
            }
        }

Wednesday, July 6, 2016

How to add Web API to an existing ASP.NET MVC Web Application

The steps I needed to perform were:
  1. Add reference to System.Web.Http.WebHost.
  2. Add App_Start\WebApiConfig.cs (see code snippet below).
  3. Import namespace System.Web.Http in Global.asax.cs.
  4. Call WebApiConfig.Register(GlobalConfiguration.Configuration) in MvcApplication.Application_Start() (in file Global.asax.cs), before registering the default Web Application route as that would otherwise take precedence.
  5. Add a controller deriving from System.Web.Http.ApiController.
I could then learn enough from the tutorial (Your First ASP.NET Web API) to define my API controller.
App_Start\WebApiConfig.cs:
using System.Web.Http;

class WebApiConfig
{
    public static void Register(HttpConfiguration configuration)
    {
        configuration.Routes.MapHttpRoute("API Default", "api/{controller}/{id}",
            new { id = RouteParameter.Optional });


// To return objects in Json format (Not XML Format),
var appXmlType = configuration.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
            configuration.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);

    }
}
Global.asax.cs:
using System.Web.Http;

...

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    WebApiConfig.Register(GlobalConfiguration.Configuration);
    RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

Monday, May 9, 2016

Operation is not valid due to current state of the object -- Elevated Privileges


Exception

ExceptionType=System.InvalidOperationExceptionJSON
Message=Operation is not valid due to the current state of the object.

Cause: 

I tried to call SPUtility.SendEmail within the RunWithElevatedPrivileges delegate.

Comments

If i try to run this method out of Elevated delegate, it works fine. All the other things that are working fine in Elevated Privileges (except send email) are,

  1. SPUtility.GetPrincipalsInGroup
  2. spWeb.EnsureUser

Wednesday, November 19, 2014

Reflection: Dynamically reading Object Properties and Calling Methods -- .NET


Introduction

In the .NET Framework, every program is compiled into an assembly containing metadata which has information of assembly behavior and structure. Reflection in .NET is used to observe and change the behavior of any object which means you can read read metadata of an object through reflection.

Current sample is going to describe following features of Reflection,
  1. Object properties and their datatypes.
  2. Call object methods dynamically. 
The first thing to use reflection in your project is, include the reflection namespace,

using System.Reflection;

Monday, May 5, 2014

The assembly 'DevExpress ...' is not registered for COM Interop. Please register it with regasm.exe /tlb.

Last week i was stuck in to a problem for registering the DevExpress library. I received following error while compiling the project,

"The assembly 'DevExpress.Utils.v13.2, Version=13.2.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a' is not registered for COM Interop. 
Please register it with regasm.exe /tlb."

Solution:
1- Open command prompt with administrator privileges and navigate to folder,
C:\Program Files (x86)\DevExpress 13.2\Components\Bin\Framework

2- Run following command,
RegAsm.exe "C:\Program Files (x86)\DevExpress 13.2\Components\Bin\Framework\DevExpress.Utils.v13.2.dll"