asp.net use .json as additional config file - asp.net

I have some configurable logic, but format is very dynamic, so I think best way how to write this is using .json format.
What is the best way to use this config similarly to Web.config? What I mean is:
.json file is read to memory every time it changes. I do not want to read and deserialize it for every request.
I also want some kind of global way to access this config (in memory). Maybe a static class?

Related

Creating a new file without using a ServletContext

Assume I want to write to a new file created within the space of my webapp.
One way would be use getServletContext().getRealPath("/") and use that String to create a new file on the server. However, often I come across advice like not using getServletContext().getRealPath("/").
Can someone please let me know of another way to write a new file within my webapp?
Many thanks.
Have some configuration property containing the absolute path of a directory outside of the webapp and web server path, read this path from the configuration property, and write to this directory.
To serve files from this directory, write a servlet that takes the name or ID of this file as parameter, reads the file from the directory, and sends its content to the response.
This will
work even if the app is deployed as a war file and never unzipped to the file system
allow you to redeploy the next version of the app or server without deleting all the uploaded/created files
allow you to add whatever control you want on the uploaded/created files, instead of making them available to everyone
In short, treat these files as data, stored in a database which happens to be the file system instead of a SQL database.

How to get better compression performance from external files for Amazon S3?

The 3 entries below are from a gtmetrix.com report. How should I handle compression performance for these files for Amazon S3? I know how to do gzip for S3. But the three files below present a more restrictive situation.
I don't have access to mailchimp's css file. Is there some way to get better compression performance in this case?
I periodically update my Thesis theme, which will change the css.css file shown below. I can't version that file since I need to use the name css.css. Is there some technique to handle this scenario?
Compressing http://www.mysite.com/wp-content/thesis/skins/classic/css.css could save 20.5KiB (79% reduction)
Compressing http://cdn-images.mailchimp.com/embedcode/slim-041711.css could save 1.1KiB (60% reduction)
Compressing http://www.mysite.com/wp-includes/js/comment-reply.min.js?ver=3.5.1 could save 374B (48% reduction
Yeah, this is a pretty common question. If you serve static files from a traditional HTTP daemon like Apache, the content is actually compressed on-the-fly via mod_deflate--it transparently gzip's the file and sets the appropriate Content-Encoding header.
If you want to do this off of S3, you have to manually gzip the files before uploading them (normally named something like cool-stylesheet.gz.css) and then set a custom Content-Encoding property on the S3 object like this:
This can be tedious to do by hand, so we actually do it automatically as part of our continuous integration build process. A post-commit hook in our source control fires, executing several build steps (including this one), and then the resulting files are deployed to the proper environment.
Edit:
It seems that you meant to describe a problem with Cloudfront, not S3. Since Cloudfront is a CDN and it caches files at it's edge locations, you have to force it to refetch the latest version of a file when it changes. There are two ways to do this: invalidate the cache or use filename versioning.
Invalidating the cache is slow and can get really expensive. After the first 1,000 invalidation requests per month, it costs a nickel for every 10 files invalidated thereafter.
A better option is to version the filenames by appending a unique identifier before they are pulled into Cloudfront. We typically use the Unix epoch of when the file was last updated. So cool-stylesheet.gz.css becomes cool-stylesheet_1363872846.gz.css. In the HTML document you then reference it like normal: <link rel="stylesheet" type="text/css" href="cool-stylesheet_1363872846.gz.css"> This will cause Cloudfront to refetch the updated file from your origin when a user opens that updated HTML document.
As I mentioned above regarding S3, this is too is a tedious thing to do manually: You'd have to rename all of your files and search/replace all references to them in the source HTML documents. It makes more sense to make it part of your CI build process. If you're not using a CI server though, you might be able to do this with a commit hook in your source repository.

How to serve resx file in ASP.NET?

How can i serve a locale appropriate .resx file to a http client in ASP.NET?
e.g.
GET /App_LocalResources/MeshModdler.resx
Background
i have a client-side binary that needs to ask a web-server for the appropriate language resources (i.e. it does not have all possible translations; it asks for the one it needs).
Right now the client-side binary prefers to be given an xml file containing all the localized resources (strings, etc). This XML file has a format that looks curiously like Microsoft's resx format (one might think the format was copied - and they would not be wrong).
Ideally we can use the power of an ASP.NET web-server to locate the appropriate resx file based on the http client's Accept-Language, e.g.
GET /App_LocalResources/MeshModdler.resx
Accept-Language: az-Cyrl-AZ
Ideally the web-server would try to return, in order of preference:
MeshModdler.az-Cyrl-AZ.resx
MeshModdler.az-AZ.resx
MeshModdler.az.resx
MeshModdler.resx
But instead the server returns:
HTTP/1.1 404 Not Found
Bonus Chatter
i know this is not possible. So in addition to a cannot be done answer, i would also accept an answer that just does what i want:
harnesses the power of an ASP.NET web-server to perform resource resolution and fallback
allows new localization resx files to be dropped into a folder and have them picked up
will not require resorting to creating a dummy page that builds what looks like a resx file, but has to thunk every entry with:
<root>
<data name="TesselateMesh.Caption">
<value><%$ Resources:MeshModdler, TesselateMesh.Caption1 %></value>
</data>
...
</root>
Additional Chatter
The hack for now will be to rename the resx files to xml:
MeshModdler.az-Cyrl-AZ.xml
MeshModdler.az-AZ.xml
MeshModdler.az.xml
MeshModdler.xml
And re-invent the fallback code:
GET /MeshModdler.az-Cyrl-AZ.xml
404 Not found
GET /MeshModdler.az-AZ.xml
404 Not found
GET /MeshModdler.az.xml
200 Ok
But it would be nice to work with ASP.NET, not against it.
You can create an ASHX file that takes a resource name file and looks up the correct .ResX file on the server. (moving your current fallback logic to /GetResource.ashx?Name=MeshModeler)

VB.Net Read 2 Connection Strings from external file

I have a situation where I have a vb.net program that uses two connection strings.
This program will be used on multiple web servers on multiple domains and the only difference for any of them is the two connection strings.
What would be the best solution to read in these connection strings from a file separate from my actual .vb code?
Ideally I want to be able to drop the .vb and the connection string file into its own folder on any number of asp.net websites and not update anything else (for example add any connections to the web.config or anything) than the external file containing the connection strings, I assume something like xml would suffice?
If possible could you give me code examples?
I would put them both in the same web.config. Give them two different names and dependent on which server you are on. You can use:
Request.ServerVariables("SERVER_NAME")
-OR-
Request.ServerVariables("LOCAL_ADDR")
Depending on your expected return you can condition the webconfigurationmanager to read one of the two connectionStrings property. Your best bet is to set a Session Variable from a basepage. Condition it to see if the Session has been populated and thus, you can read the variable throughout the application.
If you use two different .config files you are going to run into a problem where you are going to have to code your entire application on which one you want to ALWAYS use depending on which server you are on.
If you include a web.config file inside another web.config file it is the same as just adding the same two different connectionStrings in the same web.config file.
Use the built in .config support.
If you add an app.config file to your project, you can add a connectionStrings section to it, which you can access using the ConfigurationManager.ConnectionStrings property.
Please don't try to reinvent the wheel.

ASP.NET - Have settings in the Web.config (and access them using ConfigurationSection) or in a separate XML file

I have few settings which I could place in a separate XML file and have them accessed in the Web app. Then I thought (thinking of one additional file to deploy), why not have them in the web.config itself. However, just because I need to have custom nodes, I can not have the settings under . So, I am thinking of creating a custom config handler following this. Would that be better than having a separate XML file? Is It going to be an overkill or performance wise? Is there a better way to go?
From performance standpoint putting custom settings in web.config and creating a configuration handler will be OK because the config values are cached and read only once when the application starts. Putting the values in a separate XML file you will need to handle the caching your self if you want to avoid parsing it every time you need to access those values.

Resources