asp.net mvc BundleConfig - is localhost - asp.net

BundleTable.EnableOptimizations = false;
Hi! I want to set bundle optimization to false if I app is started on localhost. Since it is called from app-start method I cant get to the Request object. Other posibility is Transformation config's but they are not used on this project.
Does anybody has some other solutions?

In my opinion you better use the configuration manager. When in 'Debug' mode disable the optimizations, when building in 'Release' mode enable it.
It's also possible to create your own configurations, for instance 'localhost' build.
This is the setting you need to change for the different build configurations:
<Optimize>true</Optimize>
or
<Optimize>false</Optimize>
This will keep your code cleaner as you don't have to write any exceptions.

This worked for me
if (!HostingEnvironment.IsDevelopmentEnvironment)
BundleTable.EnableOptimizations = true;

Related

Typescript Release Mode and Debug Mode Configuration

I'm using Typescript such that there are many differences in Release Mode and Debug Mode.in the Debug Mode i use http://localhost:port/ basic URL. However, in Release Mode I have to use https://www.example.com/
Main problem
it makes me change URLs in Release Mode and Debug Mode Repeatedly. Indeed, There are other parameters that I have to change in Release Mode and Debug Mode manually which may Cause errors to misconfiguring.
What I am looking for
avoiding manually configuration in Release Mode and Debug Mode
Clues and assumptions
there may be a way using Macros in Typescript likewise something that we would use in MsBuild or WebConfig like $(ConfigurationName) And $(ProjectDir)
Best desired solution that I'm looking for
Easiest Solution in which doesn't need to learn Extra and doesn't change project Architecture.IF I have to use webpack, please put complete details around it.
Project framework
Asp.net .Net Framework or Asp.Net Core
Minimal reproduce able code
Consider you ONLY want to change this URL
const URL = http://localhost:port/
To
const URL = https://www.example.com/
in the Release Mode
Possible hard solutions
as #Kaca992 suggested webpack can resolve the issue by using
mode: 'development' and mode: 'production' like:
module.exports = {
mode: 'development'
};
production and development mode extra information
But the above solution has these Cons:
Takes time to learn webpack
Needs to be familiar with the basics concepts of npm
Needs to be familiar with concepts of bundling and minification
Needs working with modules (Import/Export)
You would have to use a environment variable and do checks based on that. You could create a helper method like this:
export function isProduction() {
return process && process.env && process.env.NODE_ENV === 'production';
}
Just be careful that most bundlers cant remove this code (it needs to be an inline check for it to be removed in compile time). If you are using something like webpack you can easily include the NODE_ENV variable using https://webpack.js.org/plugins/define-plugin/ .
This is also an excelent read into the topic: https://overreacted.io/how-does-the-development-mode-work/
Also a good way of dealing with this if you have alot of different setups would be to group all of the different values in a helper module:
debug.config.ts
release.config.ts
and then in your code you use config.ts wich just re-exports based on your configuration:
import * as debug from debug.config.ts;
import * as release from release.config.ts;
const config = isProduction() ? release : debug;
export default config;

.NET Core, Parallel Run of the same application with different configuration possible?

I am trying to get my head around the .netcore way of configuring an application.
Szenario:
I am running the same rest api (application) for client1 and client2 on the same server in the DEV-Environment.
We have environment specific configurations as well as client specific configurations.
"Old" Way:
With web.config transformations, I created build configurations for dev-client1 and dev-client2 and set the values accordingly...this all worked fine.
"New" Way:
As the appsettings.json depends on the windows environment variable (one per server) and therefore run into troubles. I could not find a way so far to run the apps in parallel on the same server with different configurations.
I might have missunderstood the new way of configuring an application, therefore any help would be appreciated.
in a typical new .NET Core web app, you will see this code in the Startup.cs:
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{environment.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables()
.Build();
all you need to do is add another appsettings file with client-specific settings, like:
.AddJsonFile($"appsettings.client.json", optional: true)
then have your build script copy in the correct build configuration to appsettings.client.json. the ConfigurationBuilder will pick up the settings in all the sources and compile them into the configuration object.
also check out the full docs:
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration

ASP.NET Bundling: Run IBundleTransform even when not optimizing

We're using System.Web.Optimization bundling to bundle & compress our JS and CSS.
We also use a custom IBundleTransform implementation in addition to to the existing JsMinify and CssMinify to do some fancy stuff to the JS (replacement of certain placeholders) before sending it to the browser.
Everything works fine as long as we're running in Release mode, because then the bundling and optimizing kicks in. But in Debug mode (which is nice for debugging ;) it seems to completely ignore all the specified IBundleTransform (makes sense in most use-cases, I guess).
Is there any way to always run our own IBundleTransform, even in Debug mode, but run the other (default) bundling algorithms (JsMinify, CssMinify) only when I really want to optimize (in Release mode)?
So the debug/release magic is controlled via the Scripts/Styles helpers. The behavior to not apply any transforms is baked into the implementation of these helpers, so if you want to do this, the best workaround might just be to have a debug/release version of each bundle and always enable bundling via BundleTable.EnableOptimizations = true.
if (!HttpContext.Current.IsDebuggingEnabled)
BundleTable.EnableOptimizations = true;

Log4j in Websphere

I recently take over a web application project using websphere and log4j running under AIX. To create a development environment, I setup all the components in windows, using eclipse to compile a WAR file and deploy it.
All is working fine except that log file is not created.
I changed the log file in log4j.properties from something like in below and and give everyone full access permission to the directory:
log4j.appender.F1.File=/abc/def/logs/admin.log
to
log4j.appender.F1.File=c:/logs/admin.log
What else can I check?
I create a simple standalone testapp which use the same log4j.properties and it can create the log file, but when the servlet deployed to websphere, it doesn't work. Please help! Thanks!
Ok, I think this article should help you. It seems that WebSphere CE uses log4j by default and controls it with a global properties file. There is a section on how to use application-specific properties files.
Here is what I try and do to troubleshoot similar issues.
Turn on log4j debugging to see where it actually picks up the file from. You need evidence of which file is picked up (so turning the debug on is a worthwhile activity) This provides you information with what log4j is trying to do to locate the configuration file.
-Dlog4j.debug=true
I would not hardcode the log4j location in the code. Instead I
would use the log4j.configuration System property and state that in
the JVM arguments. This way even I don't need to touch my code.
-Dlog4j.configuration=file:///home/manglu/log4j.properties
I would use this approach irrespective of the runtime server that I use (be it Tomcat or WAS CE or WAS)
Hope this helps
I suggest you use environment variables set on your server like this :
You must access the admin console of your server.
Under custom properties
Server_path=/abc/def/logs
In your log4j, use this : {$server_path}/log.txt
Make sure the user running the app has access to that directory.

knowing if a site was published in release mode or debug mode

How can I tell the difference between a website that has been published in release mode and the same website that was published in debug mode?
As I understand your question, you want to check programatically if the Website is running in debug mode. If this is really your question, then you may have a look at this Blog post from Rick Strahl.
Don't forget to read the comments, as there are other solutions.
Rick's solution:
bool isInDebugMode = HttpContext.Current.IsDebuggingEnabled
You can tell from ILDASM if an assembly is debug or release build.
http://blogs.msmvps.com/bill/2004/06/17/how-to-tell-if-an-existing-assembly-is-debug-or-release-build/
if it were published in debug mode with debug=true in the web.config then there would be a little bit of a performance hit. also if you can access the server then debug mode may mean the presence of .pdb files
apart from that i am not sure you can
You can use C# Preprocessor Directives: #IF DEBUG
#if DEBUG
//Put code in here to show it's in debug mode.
//like.....
DebugMessageLabel.Visible = true;
#endif
http://msdn.microsoft.com/en-us/library/4y6tbswk.aspx

Resources