0

I have this in a lot of my MVC controllers:

private JavaScriptSerializer _javaScriptSerializer;
        protected JavaScriptSerializer javaScriptSerializer
        {
            get
            {
                if (_javaScriptSerializer == null)
                {
                    _javaScriptSerializer = new JavaScriptSerializer();
                }

                return _javaScriptSerializer;
            }
            set
            {
                _javaScriptSerializer = value;
            }
        }

I have tried resolving it doing:

<alias alias="JavaScriptSerializer" type="System.Web.Script.Serialization.JavaScriptSerializer, System.Web.Script.Serialization" />

and

<register type="JavaScriptSerializer" mapTo="JavaScriptSerializer" />

But every time I run my application it gives me cannot resolve dependency. Is there a way I can initialize this once throughout my application and now have to initialize it for every controller?

1 Answer 1

1

Actually, you don't have to register anything. JavaScriptSerializer is a concrete class with a zero argument constructor, so Unity should just new it up for you, no configuration needed.

So why didn't it work before? When you configured it, you need to use the full assembly name in the alias. You didn't - you left off the version, public key token, and culture fields. As such, the CLR was unable to locate the assembly you asked for and the resolve failed.

Remove the alias and registration from your configuration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.