Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create strongly typed web.config references?

Is it possible to create strongly typed references in a web.config or app.config so that the build will break when those type references haven't been added to the project?

For example, say I have a WhizzBangerFactory which looks in the web.config for declared WhizzBangers. I might have configuration like,

<whizBangs>
<add name="SuperDooper" type="MyProject.WhizzBangers.SuperDooperWhizzBanger, MyProject.WhizzBangers" />
<add name="Fantastical" type="AnotherProject.WhizzBangers.FantasticalWhizzBanger, AnotherProject.WhizzBangers" />
</whizBangs>

This will compile perfectly fine even if there are no references to MyProject.WhizzBangers andAnotherProject.WhizzBangers. What I would like is for the build to fail and inform me that I've missed the references.

like image 373
Craig Bovis Avatar asked Nov 23 '12 08:11

Craig Bovis


2 Answers

The web.config is runtime only resolution. To late for build success or failure.

If the same build builds the types that are referenced in the web.config, parse the web.config file to verify that all the files exist ( this would require that all the needed binaries for the web site end up in a common directory. For something like tfs this would be done as a post compile step and only on success are the binaries copied to the drop directory otherwise the build is failed

If the other types are provided by another build and included as part of an installer the same sort of config file parsing could be run as a standalone app ( part of the install ) as a sanity check

The other way ( would be to include another project in your build that references all the types you might need, create an instance of the type so that the compiler does not optimize away ) and add this project as a dependency of your web project. That way if the type project fails then web build project fails as well

like image 182
Mike Beeler Avatar answered Oct 19 '22 09:10

Mike Beeler


Two points:

Firsly, how does your project compile even if the references are missing? That implies you don't really need them! How can you have a strongly typed reference to something you don't need? You're asking to be warned when you have missed off an unnecessary reference?

Secondly,

I'm still not clear on exactly what you want to do, but I think what you want can be achieved by creating a pre-build step. You might have to write a little C# console program to do the processing and checking of the web.config file.

It is similar to the way that Visual Studio generates a class to represent your resources or properties if you add them using the tooling. For example, if you go to Project -> Properties, Resources tab, then create a resource, you will find that Visual Studio will spit out a Resources.Designer.cs file with a class in that you can then refer to in your code as

Properties.Resources.MyTestResource

Interestingly, inside the generated code is this comment

/// <summary>
///   A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.

It sounds like you might be able to do something similar in your project, perhaps by adding a pre-build step that generates the class for you?

The StronglyTypedResourceBuilder class has a reference page here:

http://msdn.microsoft.com/en-us/library/system.resources.tools.stronglytypedresourcebuilder.aspx

Looks reasonably easy to use. This may not be applicable in your scenario though, I am just using it as an example. It sounds more like you want to generate a proxy type or something...

like image 2
Philip Daniels Avatar answered Oct 19 '22 09:10

Philip Daniels