Store data in WCF Operation Context.Is ThreadStatic safe?

ASP Net MVC

WCF infrastructure allows you to store context sensitive data in InstanceContext of the service instance similarly like we do in HttpContext of MVC. For that you should implement from IExtension and plug that class into WCF’s infrastructure.

public class WcfOperationContext : IExtension<OperationContext>
{
    private readonly IDictionary<string, object> items;

    private WcfOperationContext()
    {
        items = new Dictionary<string, object>();
    }

    public IDictionary<string, object> Items
    {
        get { return items; }
    }

    public static WcfOperationContext Current
    {
        get
        {
            WcfOperationContext context = OperationContext.Current.Extensions.Find<WcfOperationContext>();
            if (context == null)
            {
                context = new WcfOperationContext();
                OperationContext.Current.Extensions.Add(context);
            }
            return context;
        }
    }

    public void Attach(OperationContext owner) { }
    public void Detach(OperationContext owner) { }
}

Which you could use like that:
WcfOperationContext.Current.Items["data"] = "My Data";
string user = WcfOperationContext.Current.Items["data"] as string;

 Continue reading "Store data in WCF Operation Context.Is ThreadStatic safe?" 

The located assembly’s manifest definition does not match the assembly reference

Looking for the solution. Yes you are at the right place. Once I also faced the same issue when I was working on a MVC project, I got the error states that “The located assembly’s manifest definition does not match the assembly reference” .

reference

When I ran the solution I saw the YSD (yellow screen of death) that simply gave me the assembly name which is causing the issue.

Could not load file or assembly ‘System.Web.Optimization, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference. HRESULT: 0x80131040

In my case I have several MVC projects referring to “System.Web.Optimization” , but I’m getting this issue only in newly added project. When I investigated the issue I found that the project referring to the higher version of “System.Web.Optimization”. This is because the dll has been included using nuget and nuget always downloads the latest version of dll.

FYI:  The Microsoft.Web.Optimization package is now obsolete. With ASP.NET (MVC) 4 and higher you should install the Microsoft ASP.NET Web Optimization Framework.

Continue reading “The located assembly’s manifest definition does not match the assembly reference”

MVC Simple Model In ASP.NET

In our previous article we have learnt basics of MVC , now in this article we will learn how to make simple model In MVC.

  • Create New ASP.NET MVC 4 Web Application .
  • Select Internet Website as template.
  • Select ASP.NET as  View Engine
  • Right Click on the model -> Add New Item ->Class -> Movies.cs
MovieModel

Continue reading “MVC Simple Model In ASP.NET”

Avoid multiple call to AuthorizeCore in MVC

AuthorizeCore in MVC

In MVC AuthorizeAttribute Class is another cool feature that makes it easy to add authentication at the Controller level. In AuthorizeAttribute class we have AuthorizeCore function, it specifies that access to a controller or action method is restricted to users who meet the authorization requirement. We can override AuthorizeCore  function as per our authorization logic.

When you place the [Authorize] attribute on a Controller’s action method, a couple of calls get made to the AuthorizeAttribute class at the beginning of each request to your controller to authenticate users. Lately when I was working on a web application I found this multiple calls as an extra overload that even though the user is authorized, AuthorizeCore got executed.

So to avoid this multiple calls, I found one of the property in HttpContext which is  SkipAuthorizationSkipAuthorization is a boolean property, you can set this as true once the user is authorized. The below code snippet will give you the brief idea.

protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext.SkipAuthorization) return true; // hack to avoid multiple call to the method
if (!authorized) //if user is not authorize
{
   return false;
}
if (authorized) //if user is authorize
{
   httpContext.SkipAuthorization = true;
   return true;
}
   return false;
}

MVC Basics for Beginners

MVC Basics

In this article you will learn some basic concepts of  MVC Architecture.

So what is MVC ?

  • An Architectural design principle that separates the components of a Web Application.
  • This separation gives you more control over the individual parts of the application , which lets you more easily develop , modify and test them.
  • Three main components : The Model, The View, The Controller.
Models, Views, and Controllers
  • ModelModel holds application data , data access logic.
  • ViewMarkup and content that is returned to the browser
  • ControllerFires off some logic in response to a web browser request it listens incoming request from the browser manipulate the model data prepare the view to display in response to the web browser. 

    mvc

Continue reading “MVC Basics for Beginners”

Microsoft CacheManager Contains vs Getdata

Microsoft CacheManager Contains vs Getdata

In Microsoft.Practices.EnterpriseLibrary.Caching, GetData is used to get data from the cache and Contains checks whether the key exists in the cache. Ideally if Contains(key) returns true than GetData(key) should returns data from the cache associated with the key.  But somehow it works in the different condition. In the below code block Contains(key) returns true but the block returns null.

          if (cacheManager.Contains(key)) 
            { 
                return cacheManager.GetData(key); 
            }

Why ?

Before returning the cached value, the GetData method performs a check to see if the CacheItem has expired. If it has expired, the CacheItem is removed from the cache and null is returned. However, the call to Contains will return true because the CacheItem is in the cache even though it’s expiration may have elapsed. This seems to be by design. With that in mind, it would be wise not to cache a null value to represent no data since you would not be able to discern an expired CacheItem from an actual cached value of null.

Continue reading “Microsoft CacheManager Contains vs Getdata”