Preventing ASP.Net projects from copying {assembly}.dll.config to bin folder - c#

When building ASP.Net projects in Visual Studio (version 2015 Community), website configuration files (e.g., Web.Config) are copied by the build process into the bin folder alongside the compiled .dll file, and renamed {assembly}.dll.config.
This occurs despite the Properties Window for the Web.config file being set with the following parameters:
For our solution, this second copy of the file is not necessary, nor used in the {assembly}.dll.config form found in the output bin folder.
I would like to know what is necessary to prevent Visual Studio from making a copy of the configuration file and placing it in the bin output directory.
Having sensitive configuration settings in two places (Web.config in the root directory, and {assembly}.dll.config in the output directory) is problematic from a security standpoint.

To deploy your app, Publish your application. This way, only the necessary assemblies/files are deployed. Your published /bin folder will reflect this (will not have the "duplicates" you mention).
Security: depending on what you're after, you can
separate "sensitive" information from web.confg into their own files. This usually is only for keeping such from online repositories (TFS, Git, etc.) - by excluding the file from being in source control
e.g.
<appSettings file="AllMySecrets.config">
....
Use encryption
Hth.

Related

Is the arrangement of an application reading App.config specific to Visual Studio IDE and/or compiler?

I heard that for C# programs,
App.config —This file contains configuration settings that the application reads when it starts.
For applications compiled from C programs, they don't always read some files in a specific directory. Whether that happens is up to the applications.
Is the arrangement of an application reading App.config in a particular directory done by the compiler?
Is the arrangement specific to Visual Studio IDE or compiler?
If the application is compiled not within Visual Studio, will the arrangement still hold?
Thanks.
Good question. As I understand it, the reading of the configuration is handled by classes in the System.Configuration namespace, and by convention they search for the .config file in the same directory with the same name as the executable you are running. You can use System.Configuration's ConfigurationManager to open arbitrary .config files, but that's uncommon in my experience.
From the link:
Executable–hosted app.
These apps have two configuration files: a source configuration file, which is modified by the developer during development, and an output file that is distributed with the app.
When you develop in Visual Studio, place the source configuration file for your app in the project directory and set its Copy To Output Directory property to Copy always or Copy if newer. The name of the configuration file is the name of the app with a .config extension. For example, an app called myApp.exe should have a source configuration file called myApp.exe.config.

How to create folder in mvc solution directory so that it is included in project?

I am new in mvc and c# and I can't solve following problem:
I am trying to create a folder named "Items" in solution folder.
I have tryed to use CreateDirectory method:
Directory.CreateDirectory("~/Images");
But it didn't work for me - folder wasn't created ..
Partly working solution was to create a folder by :
Directory.CreateDirectory(Server.MapPath("~/Images"));
"Items" folder was created, but it is not included in the solution:
How to create folder in solution directory so that it is included in project ?
(I needs to by done by code not by hand)
You need to understand what solution and csproj file is used for
In general, they're being designed and used for development with Visual Studio, and once the project is compiled, all these files will be ignored and excluded from the deployment package
Directory.CreateDirectory(Server.MapPath("~/Images"));
The code above simply create the directory if not existed yet in the deployment package at run-time, so you won't see it in your solution unless you run the project locally (either debug/release mode, it does not matter here). However, everything will run normally in hosted environment (ex: IIS).
For your information, here's the brief of what solution and csproj is
solution (.sln) file: contains information to manage one or many individual projects, contains build environments (for each project), start up mode (useful when you want to start multiple projects in one run), project dependencies and so on. Take a note that VS also read from suo file (solution user options) which is used to defined user-custom preferences (you should not include the .suo file in the version control, because it's custom settings)
csproj file: define the structures of project, what the namespace is, what is static folders, embedded resources, references, packages, etc.
Lastly, if you create the folder manually, VS will auto include that folder into deployment package AND csproj, but depends on the file type, you might need to change the Build Action and Copy To Output Directory in file properties.
Hope it helps.
A deployed web application on a web server doesn't have any notion of Visual Studio solution or projects. So the Directory.CreateDirectory(Server.MapPath("~/Images")) is the correct way to create a folder inside your web application at runtime but we cannot be talking about including it into a solution because this hardly makes sense in a pre-compiled web application. If you create the directory on your local development machine, you could always manually include the folder to the corresponding .csproj file, but at runtime this will not make any difference whatsoever.
The reason I wanted to create a folder (if didn't exist) was to make sure it exits before I try to store image in it.
After reading posts here and a few google searches I have concluded that the proper way to handle image upload would be
To create (In my case) folder "Images" by hand to be sure it exists
Then storing uploaded img in existing folder:
string path =Server.MapPath("~/Images/"+ UploadedImageName);
file.SaveAs(path);

VS2013: never put bin directory in source control, but it caused build fail

I'm new in Visual Studio 2013 (C#). I know in general it should be avoid putting bin directory in source control. However, in my bin directory, there are lots of dll files. Without this dlls, I can't build my project.
I thought maybe I should create a directory, such as 'lib' to store the dlls. But the fact is NuGet always put dll into the bin directory. Should I manually move the dll from bin to lib?
All I want is to simply put all source codes, resource files and dll files to source control (git). Then my team-mates can pull the project then build it in their machine.
edit on 1/4/2015: provide further details
For example, I installed log4net thru NuGet. After finished, I found 3 new files existed in bin directory:
log4net.dll
log4net.xml
log4net.dll.refresh
I tried to remove bin directory from my project, then rebuild my project, and hope to see that a new bin directory would be built and all dll/pdb/etc files would come back. But unfortunately it didn't happen.
In my understanding, the ideal process would be:
add reference/install package, then certain config info would be recorded and according files would be installed to bin directory;
when build, according lib files would be referenced from bin, if such files not found, VS would get the reference info from some place other than bin and download them again. So that's one of the reasons why we don't need to put bin into source control.
However, I found that's not true in my project.
Actually according to the official .gitignore, I found bin directory as build results is filtered ([Bb]in/). That is to say, even VS knows such directory should be ignored.
Maybe some wrong setting within my project?

Visual Studio: different project folder structure for publishing/editing than the physical one

I'd like to add files to a web csproj that are located under its directory, but in a different directory structure than the physical one. Example physical directory structure:
MyProject.csproj
Assets
Settings.config
Now my project needs the following structure when I publish the project (i.e. on the server when deployed) and would be nice if this structure would be visible from Visual Studio too:
MyProject.csproj
Config
Settings.config
Notice that Settings.config isn't in the Assets but in the Config folder.
I don't want to copy the files over, I can't move them and I can't change where these files are loaded from.
Is it possible for such files to be located in the Assets folder while
from VS they appear to be in the Config folder,
and when the project is published they are copied to the Config folder.
The latter one is possible with post-build tasks I think and I believe I can implement it. However I'd like to have the whole development experience, including the folder structure in VS, to show the file being under Config. Is this possible?
I'm looking for something like solution folders, but for projects. Adding files as links would work great, but since the files are in the physical folder of the project I get the error "Cannot add a link to a file that is inside the project tree".

How do you make Visual Studio run assembly files (dependencies) from the bin?

I have a solution made up of multiple projects which have various dependencies on each other. When I build the project/solution, it creates the DLL assembly files in the Project\bin folder(s) as it should. But when I run the program, it copies everything to the temporary files folder and runs it from there:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\ApplicationName
So now there are identical DLL files for each project in the bin folders and in the temporary files folder, where it's being run from. The problem is that I have references to the bin directory(s) hardcoded into my projects in a couple places, so when it looks there, it comes back with a DLL mix-up and errors out. How can I get Visual Studio to run the assembly files from the original bin directory(s) instead of duplicating them and running them from the temporary files?
P.S. Please don't tell me to change the hardcoding - there are reasons why it has to stay as it is; that's why I'm trying to figure out how to run from the bin.
Thanks
This is known as Shadow Copying, and its a design feature. Particularly for ASP.NET sites, it enables domain assemblies to be updated without unloading the application domain, which is important for ASP.NET sites which are designed to run continuously. I would really recommend leaving it on, and re-assessing the requirement for use of the \bin directory.
If you really want to disable shadow copying, add the following to your web.config file:
<hostingEnvironment shadowCopyBinAssemblies="false" />
The side-effect of doing the above, is that your assemblies in your \bin directory will be locked during the lifetime of the application.

Categories

Resources