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”

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;
}

Must Haves Visual Studio Extensions

Web development is easier when you’ve got the right tools/extensions. There are various tools and extensions available for visual studio that will make your programming more efficient by saving your lots of time and make your life as a developer easier and more productive. The Visual Studio Gallery is the best place to find tools, controls, extensions and templates, so as my favorite place too.

In this article I’m publishing my personal favorite  extension which I assume may helpful for you too as per my programming experience. This extensions may update time to time, so for now I am listing all those which are useful and must haves. You can comment those you find useful for you so we can keep this list updated and which will be useful for me and others too.

Continue reading “Must Haves Visual Studio Extensions”

How to sign third party assemblies

How to sign third party assemblies

You need to sign assemblies if you want to put them in the GAC.

If you sign an executable, then any class libraries it links to also needs to be signed. This can be difficult if you’re using a third-party library.A very important reason to sign an assembly is so you can be sure it is your assembly. Since the private key is yours, nobody else can sign an assembly with that same key. This means that when the public key of an assembly is one you know.

Continue reading “How to sign third party assemblies”

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”