Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid caching MediaTypeHeaderValue that is not serializable #165

Open
micdenny opened this issue Mar 5, 2016 · 6 comments · May be fixed by #166
Open

Avoid caching MediaTypeHeaderValue that is not serializable #165

micdenny opened this issue Mar 5, 2016 · 6 comments · May be fixed by #166

Comments

@micdenny
Copy link

micdenny commented Mar 5, 2016

In general every class that is not a POCO class should not be cached, otherwise it becomes difficult to implement a caching provider based on a distributed caching system.

I'm developing an extension for AppFabric (the same will be when I will implement the Redis extension), and I'm blocked because CacheOutputAttribute tries to put in cache a .net framework object MediaTypeHeaderValue that is not serializable.

If caching that whole object is necessary we should think to use a POCO class instead, that share the same structure:

public class MyMediaTypeHeaderValue
{
        public string CharSet { get; set; }
        public string MediaType { get; set; }
        public List<NameValueHeaderValue> Parameters { get; set; }
}
@micdenny
Copy link
Author

micdenny commented Mar 5, 2016

I saw now that should be enough to cache this responseContent.Headers.ContentType.ToString() and simply use the MediaTypeHeaderValue.TryParse:

            MediaTypeHeaderValue contenttype = null;
            var ct = _webApiCache.Get<string>(cachekey + Constants.ContentTypeKey);
            if (ct != null)
            {
                if (!MediaTypeHeaderValue.TryParse(ct, out contenttype))
                {
                    contenttype = new MediaTypeHeaderValue(cachekey.Split(new[] { ':' }, 2)[1].Split(';')[0]);
                }
            }

and

var contentType = responseContent.Headers.ContentType.ToString();
                        _webApiCache.Add(cachekey + Constants.ContentTypeKey,
                                        contentType,
                                        cacheTime.AbsoluteExpiration, baseKey);

@micdenny
Copy link
Author

micdenny commented Mar 7, 2016

I've submitted a PR to fix this

@micdenny
Copy link
Author

micdenny commented Mar 7, 2016

I need that PR to be merged or discussed to have a working implementation of AppFabric provider: https://github.com/micdenny/Strathweb.CacheOutput.WebApi2.AppFabric

@felickz
Copy link

felickz commented Apr 4, 2016

+1 writing a redis provider, same root issue.

@Iamcerba
Copy link

Iamcerba commented Sep 8, 2017

This happened because MediaTypeHeaderValue has read only properties and depending on your Lib which is serializing, for example JSON.NET, custom JsonConverter is needed.

@TedCrocker
Copy link

I have run into this issue as well trying to implement a FileCache option, any eta?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants