0

Suppose I need to deploy a single assembly(Requirement). I will merge all assemblies using ILMerge. But lot of my assemblies have PreApplicationStartMethodAttribute(I don't have any control on all these assemblies). ILMerge will create a single assembly and PreApplicationStartMethodAttribute is only allowed once in a assembly. What should I do?

Imran Qadir Baksh - Baloch
  • 32,612
  • 68
  • 179
  • 322
  • Could you do something like ZIP up all the assemblies, then create a wrapper assembly that would contain the ZIP file as a resource, as well as the code to extract the other assemblies from the ZIP file? – John Saunders Sep 27 '13 at 19:22
  • @user960567 Effectively, what John Saunders is saying is "don't create a single assembly, keep multiple assemblies, but hide them in a single file". This may or may not meet your requirements. –  Sep 27 '13 at 19:35
  • 1
    You haven't said why you must have only a single assembly, so I assume it's that you have to _deploy_ only a single file. My thought is to put the multiple files into a single file and have the single file deploy the rest. If you must have only a single assembly at runtime, then this won't work. – John Saunders Sep 27 '13 at 19:38
  • 2
    BTW, if .NET 4.5 is an option, PreApplicationStartMethod is now allowed to appear multiple times. Compare [.NET 4.5](http://msdn.microsoft.com/en-us/library/system.web.preapplicationstartmethodattribute.aspx): `AllowMultiple = true` versus [.NET 4](http://msdn.microsoft.com/en-us/library/system.web.preapplicationstartmethodattribute%28v=vs.100%29.aspx): `AllowMultiple = false` –  Sep 27 '13 at 19:39
  • OK Thanks all.Means .Net 4.5 solve this issue. – Imran Qadir Baksh - Baloch Sep 28 '13 at 05:47
  • @hav, can you paste this as answer – Imran Qadir Baksh - Baloch Sep 28 '13 at 05:53
  • @user960567 Sure, done. –  Sep 28 '13 at 12:46

1 Answers1

2

(from the comments)

In .NET 4.0, PreApplicationStartMethod can only be specified once per assembly:

PreApplicationStartMethodAttribute Class

[AttributeUsageAttribute(AttributeTargets.Assembly, AllowMultiple = false)]
public sealed class PreApplicationStartMethodAttribute : Attribute

but in .NET 4.5, multiple attributes in a single assembly are okay:

PreApplicationStartMethodAttribute Class

[AttributeUsageAttribute(AttributeTargets.Assembly, AllowMultiple = true)]
public sealed class PreApplicationStartMethodAttribute : Attribute

so you can avoid the problem by upgrading to .NET 4.5.

  • I'm wondering what made CLR team to implement it with this restriction in case when multiple such attributes are defined : `There is no guarantee of the order in which the assembly-defined application start methods are called. As a result, each registered start method should be coded to run in isolation and should not depend on side effects from other registered start methods` Why can they NOT guaranteed the execution in the order in which it is defined in the assemblyinfo.cs file? – RBT Mar 02 '16 at 10:34
  • @RasikBihariTiwari You're assuming that when the attributes are retrieved, even if they're defined in the same source file, it's possible to retrieve them in the source code order. [There's no requirement for that.](http://stackoverflow.com/a/480079) You're also assuming they're both defined in the AssemblyInfo.cs file. There's no requirement for that either: they could be defined in separate files where no order is more sensible than any other. –  Mar 02 '16 at 11:17
  • I completely missed the case when this attribute can be mentioned in multiple AssemblyInfo.cs files across various assemblies getting loaded in an app domain. I was thinking that I would generally have this attribute ONLY in my startup project to initialize things like service registration in DI containers etc. Agree with you. Appreciate your quick response! – RBT Mar 02 '16 at 12:00