AppCodeCompiler.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. //
  2. // System.Web.Compilation.AppCodeCompiler: A compiler for the App_Code folder
  3. //
  4. // Authors:
  5. // Marek Habersack ([email protected])
  6. //
  7. // (C) 2006 Marek Habersack
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System;
  31. using System.CodeDom;
  32. using System.CodeDom.Compiler;
  33. using System.Configuration;
  34. using System.Collections;
  35. using System.Collections.Generic;
  36. using System.Globalization;
  37. using System.IO;
  38. using System.Reflection;
  39. using System.Web;
  40. using System.Web.Configuration;
  41. using System.Web.Profile;
  42. using System.Web.Util;
  43. namespace System.Web.Compilation
  44. {
  45. class AssemblyPathResolver
  46. {
  47. static Dictionary <string, string> assemblyCache;
  48. static AssemblyPathResolver ()
  49. {
  50. assemblyCache = new Dictionary <string, string> ();
  51. }
  52. public static string GetAssemblyPath (string assemblyName)
  53. {
  54. lock (assemblyCache) {
  55. if (assemblyCache.ContainsKey (assemblyName))
  56. return assemblyCache [assemblyName];
  57. Assembly asm = null;
  58. Exception error = null;
  59. if (assemblyName.IndexOf (',') != -1) {
  60. try {
  61. asm = Assembly.Load (assemblyName);
  62. } catch (Exception e) {
  63. error = e;
  64. }
  65. }
  66. if (asm == null) {
  67. try {
  68. asm = Assembly.LoadWithPartialName (assemblyName);
  69. } catch (Exception e) {
  70. error = e;
  71. }
  72. }
  73. if (asm == null)
  74. throw new HttpException (String.Format ("Unable to find assembly {0}", assemblyName), error);
  75. assemblyCache.Add (assemblyName, asm.Location);
  76. return asm.Location;
  77. }
  78. }
  79. }
  80. internal class AppCodeAssembly
  81. {
  82. private List<string> files;
  83. private List<CodeCompileUnit> units;
  84. private string name;
  85. private string path;
  86. private bool validAssembly;
  87. private string outputAssemblyName;
  88. public string OutputAssemblyName
  89. {
  90. get {
  91. return outputAssemblyName;
  92. }
  93. }
  94. public bool IsValid
  95. {
  96. get { return validAssembly; }
  97. }
  98. public string SourcePath
  99. {
  100. get { return path; }
  101. }
  102. // temporary
  103. public string Name
  104. {
  105. get { return name; }
  106. }
  107. public List<string> Files
  108. {
  109. get { return files; }
  110. }
  111. // temporary
  112. public AppCodeAssembly (string name, string path)
  113. {
  114. this.files = new List<string> ();
  115. this.units = new List<CodeCompileUnit> ();
  116. this.validAssembly = true;
  117. this.name = name;
  118. this.path = path;
  119. }
  120. public void AddFile (string path)
  121. {
  122. files.Add (path);
  123. }
  124. public void AddUnit (CodeCompileUnit unit)
  125. {
  126. units.Add (unit);
  127. }
  128. object OnCreateTemporaryAssemblyFile (string path)
  129. {
  130. FileStream f = new FileStream (path, FileMode.CreateNew);
  131. f.Close ();
  132. return path;
  133. }
  134. // Build and add the assembly to the BuildManager's
  135. // CodeAssemblies collection
  136. public void Build (string[] binAssemblies)
  137. {
  138. Type compilerProvider = null;
  139. CompilerInfo compilerInfo = null, cit;
  140. string extension, language, cpfile = null;
  141. List<string> knownfiles = new List<string>();
  142. List<string> unknownfiles = new List<string>();
  143. // First make sure all the files are in the same
  144. // language
  145. bool known = false;
  146. foreach (string f in files) {
  147. known = true;
  148. language = null;
  149. extension = Path.GetExtension (f);
  150. if (String.IsNullOrEmpty (extension) || !CodeDomProvider.IsDefinedExtension (extension))
  151. known = false;
  152. if (known) {
  153. language = CodeDomProvider.GetLanguageFromExtension(extension);
  154. if (!CodeDomProvider.IsDefinedLanguage (language))
  155. known = false;
  156. }
  157. if (!known || language == null) {
  158. unknownfiles.Add (f);
  159. continue;
  160. }
  161. cit = CodeDomProvider.GetCompilerInfo (language);
  162. if (cit == null || !cit.IsCodeDomProviderTypeValid)
  163. continue;
  164. if (compilerProvider == null) {
  165. cpfile = f;
  166. compilerProvider = cit.CodeDomProviderType;
  167. compilerInfo = cit;
  168. } else if (compilerProvider != cit.CodeDomProviderType)
  169. throw new HttpException (
  170. String.Format (
  171. "Files {0} and {1} are in different languages - they cannot be compiled into the same assembly",
  172. Path.GetFileName (cpfile),
  173. Path.GetFileName (f)));
  174. knownfiles.Add (f);
  175. }
  176. CodeDomProvider provider = null;
  177. CompilationSection compilationSection = WebConfigurationManager.GetSection ("system.web/compilation") as CompilationSection;
  178. if (compilerInfo == null) {
  179. if (!CodeDomProvider.IsDefinedLanguage (compilationSection.DefaultLanguage))
  180. throw new HttpException ("Failed to retrieve default source language");
  181. compilerInfo = CodeDomProvider.GetCompilerInfo (compilationSection.DefaultLanguage);
  182. if (compilerInfo == null || !compilerInfo.IsCodeDomProviderTypeValid)
  183. throw new HttpException ("Internal error while initializing application");
  184. provider = compilerInfo.CreateProvider ();
  185. if (provider == null)
  186. throw new HttpException ("A code provider error occurred while initializing application.");
  187. }
  188. provider = compilerInfo.CreateProvider ();
  189. if (provider == null)
  190. throw new HttpException ("A code provider error occurred while initializing application.");
  191. AssemblyBuilder abuilder = new AssemblyBuilder (provider);
  192. foreach (string file in knownfiles)
  193. abuilder.AddCodeFile (file);
  194. foreach (CodeCompileUnit unit in units)
  195. abuilder.AddCodeCompileUnit (unit);
  196. BuildProvider bprovider;
  197. CompilerParameters parameters = compilerInfo.CreateDefaultCompilerParameters ();
  198. parameters.IncludeDebugInformation = compilationSection.Debug;
  199. if (binAssemblies != null && binAssemblies.Length > 0)
  200. parameters.ReferencedAssemblies.AddRange (binAssemblies);
  201. if (compilationSection != null) {
  202. foreach (AssemblyInfo ai in compilationSection.Assemblies)
  203. if (ai.Assembly != "*") {
  204. try {
  205. parameters.ReferencedAssemblies.Add (
  206. AssemblyPathResolver.GetAssemblyPath (ai.Assembly));
  207. } catch (Exception ex) {
  208. throw new HttpException (
  209. String.Format ("Could not find assembly {0}.", ai.Assembly),
  210. ex);
  211. }
  212. }
  213. BuildProviderCollection buildProviders = compilationSection.BuildProviders;
  214. foreach (string file in unknownfiles) {
  215. bprovider = GetBuildProviderFor (file, buildProviders);
  216. if (bprovider == null)
  217. continue;
  218. bprovider.GenerateCode (abuilder);
  219. }
  220. }
  221. if (knownfiles.Count == 0 && unknownfiles.Count == 0 && units.Count == 0)
  222. return;
  223. outputAssemblyName = (string)FileUtils.CreateTemporaryFile (
  224. AppDomain.CurrentDomain.SetupInformation.DynamicBase,
  225. name, "dll", OnCreateTemporaryAssemblyFile);
  226. parameters.OutputAssembly = outputAssemblyName;
  227. foreach (Assembly a in BuildManager.TopLevelAssemblies)
  228. parameters.ReferencedAssemblies.Add (a.Location);
  229. CompilerResults results = abuilder.BuildAssembly (parameters);
  230. if (results.NativeCompilerReturnValue == 0) {
  231. BuildManager.CodeAssemblies.Add (results.CompiledAssembly);
  232. BuildManager.TopLevelAssemblies.Add (results.CompiledAssembly);
  233. HttpRuntime.WritePreservationFile (results.CompiledAssembly, name);
  234. } else {
  235. if (HttpContext.Current.IsCustomErrorEnabled)
  236. throw new HttpException ("An error occurred while initializing application.");
  237. throw new CompilationException (null, results.Errors, null);
  238. }
  239. }
  240. private string PhysicalToVirtual (string file)
  241. {
  242. return file.Replace (HttpRuntime.AppDomainAppPath, "/").Replace (Path.DirectorySeparatorChar, '/');
  243. }
  244. private BuildProvider GetBuildProviderFor (string file, BuildProviderCollection buildProviders)
  245. {
  246. if (file == null || file.Length == 0 || buildProviders == null || buildProviders.Count == 0)
  247. return null;
  248. BuildProvider ret = buildProviders.GetProviderForExtension (Path.GetExtension (file));
  249. if (ret != null && IsCorrectBuilderType (ret)) {
  250. ret.SetVirtualPath (PhysicalToVirtual (file));
  251. return ret;
  252. }
  253. return null;
  254. }
  255. private bool IsCorrectBuilderType (BuildProvider bp)
  256. {
  257. if (bp == null)
  258. return false;
  259. Type type;
  260. object[] attrs;
  261. type = bp.GetType ();
  262. attrs = type.GetCustomAttributes (true);
  263. if (attrs == null)
  264. return false;
  265. BuildProviderAppliesToAttribute bpAppliesTo;
  266. bool attributeFound = false;
  267. foreach (object attr in attrs) {
  268. bpAppliesTo = attr as BuildProviderAppliesToAttribute;
  269. if (bpAppliesTo == null)
  270. continue;
  271. attributeFound = true;
  272. if ((bpAppliesTo.AppliesTo & BuildProviderAppliesTo.All) == BuildProviderAppliesTo.All ||
  273. (bpAppliesTo.AppliesTo & BuildProviderAppliesTo.Code) == BuildProviderAppliesTo.Code)
  274. return true;
  275. }
  276. if (attributeFound)
  277. return false;
  278. return true;
  279. }
  280. }
  281. internal class AppCodeCompiler
  282. {
  283. static private bool _alreadyCompiled;
  284. internal static string DefaultAppCodeAssemblyName;
  285. // A dictionary that contains an entry per an assembly that will
  286. // be produced by compiling App_Code. There's one main assembly
  287. // and an optional number of assemblies as defined by the
  288. // codeSubDirectories sub-element of the compilation element in
  289. // the system.web section of the app's config file.
  290. // Each entry's value is an AppCodeAssembly instance.
  291. //
  292. // Assemblies are named as follows:
  293. //
  294. // 1. main assembly: App_Code.{HASH}
  295. // 2. subdir assemblies: App_SubCode_{DirName}.{HASH}
  296. //
  297. // If any of the assemblies contains files that would be
  298. // compiled with different compilers, a System.Web.HttpException
  299. // is thrown.
  300. //
  301. // Files for which there is no explicit builder are ignored
  302. // silently
  303. //
  304. // Files for which exist BuildProviders but which have no
  305. // unambiguous language assigned to them (e.g. .wsdl files), are
  306. // built using the default website compiler.
  307. private List<AppCodeAssembly> assemblies;
  308. string providerTypeName = null;
  309. public AppCodeCompiler ()
  310. {
  311. assemblies = new List<AppCodeAssembly>();
  312. }
  313. bool ProcessAppCodeDir (string appCode, AppCodeAssembly defasm)
  314. {
  315. // First process the codeSubDirectories
  316. CompilationSection cs = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
  317. if (cs != null) {
  318. string aname;
  319. for (int i = 0; i < cs.CodeSubDirectories.Count; i++) {
  320. aname = String.Concat ("App_SubCode_", cs.CodeSubDirectories[i].DirectoryName);
  321. assemblies.Add (new AppCodeAssembly (
  322. aname,
  323. Path.Combine (appCode, cs.CodeSubDirectories[i].DirectoryName)));
  324. }
  325. }
  326. return CollectFiles (appCode, defasm);
  327. }
  328. CodeTypeReference GetProfilePropertyType (string type)
  329. {
  330. if (String.IsNullOrEmpty (type))
  331. throw new ArgumentException ("String size cannot be 0", "type");
  332. return new CodeTypeReference (type);
  333. }
  334. string FindProviderTypeName (ProfileSection ps, string providerName)
  335. {
  336. if (ps.Providers == null || ps.Providers.Count == 0)
  337. return null;
  338. ProviderSettings pset = ps.Providers [providerName];
  339. if (pset == null)
  340. return null;
  341. return pset.Type;
  342. }
  343. void GetProfileProviderAttribute (ProfileSection ps, CodeAttributeDeclarationCollection collection,
  344. string providerName)
  345. {
  346. if (String.IsNullOrEmpty (providerName))
  347. providerTypeName = FindProviderTypeName (ps, ps.DefaultProvider);
  348. else
  349. providerTypeName = FindProviderTypeName (ps, providerName);
  350. if (providerTypeName == null)
  351. throw new HttpException (String.Format ("Profile provider type not defined: {0}",
  352. providerName));
  353. collection.Add (
  354. new CodeAttributeDeclaration (
  355. "ProfileProvider",
  356. new CodeAttributeArgument (
  357. new CodePrimitiveExpression (providerTypeName)
  358. )
  359. )
  360. );
  361. }
  362. void GetProfileSettingsSerializeAsAttribute (ProfileSection ps, CodeAttributeDeclarationCollection collection,
  363. SerializationMode mode)
  364. {
  365. string parameter = String.Concat ("SettingsSerializeAs.", mode.ToString ());
  366. collection.Add (
  367. new CodeAttributeDeclaration (
  368. "SettingsSerializeAs",
  369. new CodeAttributeArgument (
  370. new CodeSnippetExpression (parameter)
  371. )
  372. )
  373. );
  374. }
  375. void AddProfileClassGetProfileMethod (CodeTypeDeclaration profileClass)
  376. {
  377. CodeMethodReferenceExpression mref = new CodeMethodReferenceExpression (
  378. new CodeTypeReferenceExpression (typeof (System.Web.Profile.ProfileBase)),
  379. "Create");
  380. CodeMethodInvokeExpression minvoke = new CodeMethodInvokeExpression (
  381. mref,
  382. new CodeExpression[] { new CodeVariableReferenceExpression ("username") }
  383. );
  384. CodeCastExpression cast = new CodeCastExpression ();
  385. cast.TargetType = new CodeTypeReference ("ProfileCommon");
  386. cast.Expression = minvoke;
  387. CodeMethodReturnStatement ret = new CodeMethodReturnStatement ();
  388. ret.Expression = cast;
  389. CodeMemberMethod method = new CodeMemberMethod ();
  390. method.Name = "GetProfile";
  391. method.ReturnType = new CodeTypeReference ("ProfileCommon");
  392. method.Parameters.Add (new CodeParameterDeclarationExpression("System.String", "username"));
  393. method.Statements.Add (ret);
  394. method.Attributes = MemberAttributes.Public;
  395. profileClass.Members.Add (method);
  396. }
  397. void AddProfileClassProperty (ProfileSection ps, CodeTypeDeclaration profileClass, ProfilePropertySettings pset)
  398. {
  399. string name = pset.Name;
  400. if (String.IsNullOrEmpty (name))
  401. throw new HttpException ("Profile property 'Name' attribute cannot be null.");
  402. CodeMemberProperty property = new CodeMemberProperty ();
  403. string typeName = pset.Type;
  404. if (typeName == "string")
  405. typeName = "System.String";
  406. property.Name = name;
  407. property.Type = GetProfilePropertyType (typeName);
  408. property.Attributes = MemberAttributes.Public;
  409. CodeAttributeDeclarationCollection collection = new CodeAttributeDeclarationCollection();
  410. GetProfileProviderAttribute (ps, collection, pset.Provider);
  411. GetProfileSettingsSerializeAsAttribute (ps, collection, pset.SerializeAs);
  412. property.CustomAttributes = collection;
  413. CodeMethodReturnStatement ret = new CodeMethodReturnStatement ();
  414. CodeCastExpression cast = new CodeCastExpression ();
  415. ret.Expression = cast;
  416. CodeMethodReferenceExpression mref = new CodeMethodReferenceExpression (
  417. new CodeThisReferenceExpression (),
  418. "GetPropertyValue");
  419. CodeMethodInvokeExpression minvoke = new CodeMethodInvokeExpression (
  420. mref,
  421. new CodeExpression[] { new CodePrimitiveExpression (name) }
  422. );
  423. cast.TargetType = new CodeTypeReference (typeName);
  424. cast.Expression = minvoke;
  425. property.GetStatements.Add (ret);
  426. if (!pset.ReadOnly) {
  427. mref = new CodeMethodReferenceExpression (
  428. new CodeThisReferenceExpression (),
  429. "SetPropertyValue");
  430. minvoke = new CodeMethodInvokeExpression (
  431. mref,
  432. new CodeExpression[] { new CodePrimitiveExpression (name), new CodeSnippetExpression ("value") }
  433. );
  434. property.SetStatements.Add (minvoke);
  435. }
  436. profileClass.Members.Add (property);
  437. }
  438. void AddProfileClassGroupProperty (string groupName, string memberName, CodeTypeDeclaration profileClass)
  439. {
  440. CodeMemberProperty property = new CodeMemberProperty ();
  441. property.Name = memberName;
  442. property.Type = new CodeTypeReference (groupName);
  443. property.Attributes = MemberAttributes.Public;
  444. CodeMethodReturnStatement ret = new CodeMethodReturnStatement ();
  445. CodeCastExpression cast = new CodeCastExpression ();
  446. ret.Expression = cast;
  447. CodeMethodReferenceExpression mref = new CodeMethodReferenceExpression (
  448. new CodeThisReferenceExpression (),
  449. "GetProfileGroup");
  450. CodeMethodInvokeExpression minvoke = new CodeMethodInvokeExpression (
  451. mref,
  452. new CodeExpression[] { new CodePrimitiveExpression (memberName) }
  453. );
  454. cast.TargetType = new CodeTypeReference (groupName);
  455. cast.Expression = minvoke;
  456. property.GetStatements.Add (ret);
  457. profileClass.Members.Add (property);
  458. }
  459. void BuildProfileClass (ProfileSection ps, string className, ProfilePropertySettingsCollection psc,
  460. CodeNamespace ns, string baseClass, bool baseIsGlobal,
  461. SortedList <string, string> groupProperties)
  462. {
  463. CodeTypeDeclaration profileClass = new CodeTypeDeclaration (className);
  464. CodeTypeReference cref = new CodeTypeReference (baseClass);
  465. if (baseIsGlobal)
  466. cref.Options |= CodeTypeReferenceOptions.GlobalReference;
  467. profileClass.BaseTypes.Add (cref);
  468. profileClass.TypeAttributes = TypeAttributes.Public;
  469. ns.Types.Add (profileClass);
  470. foreach (ProfilePropertySettings pset in psc)
  471. AddProfileClassProperty (ps, profileClass, pset);
  472. if (groupProperties != null && groupProperties.Count > 0)
  473. foreach (KeyValuePair <string, string> group in groupProperties)
  474. AddProfileClassGroupProperty (group.Key, group.Value, profileClass);
  475. AddProfileClassGetProfileMethod (profileClass);
  476. }
  477. string MakeGroupName (string name)
  478. {
  479. return String.Concat ("ProfileGroup", name);
  480. }
  481. // FIXME: there should be some validation of syntactic correctness of the member/class name
  482. // for the groups/properties. For now it's left to the compiler to report errors.
  483. //
  484. // CodeGenerator.IsValidLanguageIndependentIdentifier (id) - use that
  485. //
  486. bool ProcessCustomProfile (ProfileSection ps, AppCodeAssembly defasm)
  487. {
  488. CodeCompileUnit unit = new CodeCompileUnit ();
  489. CodeNamespace ns = new CodeNamespace (null);
  490. unit.Namespaces.Add (ns);
  491. defasm.AddUnit (unit);
  492. ns.Imports.Add (new CodeNamespaceImport ("System"));
  493. ns.Imports.Add (new CodeNamespaceImport ("System.Configuration"));
  494. ns.Imports.Add (new CodeNamespaceImport ("System.Web"));
  495. ns.Imports.Add (new CodeNamespaceImport ("System.Web.Profile"));
  496. RootProfilePropertySettingsCollection props = ps.PropertySettings;
  497. if (props == null)
  498. return true;
  499. SortedList<string, string> groupProperties = new SortedList<string, string> ();
  500. string groupName;
  501. foreach (ProfileGroupSettings pgs in props.GroupSettings) {
  502. groupName = MakeGroupName (pgs.Name);
  503. groupProperties.Add (groupName, pgs.Name);
  504. BuildProfileClass (ps, groupName, pgs.PropertySettings, ns,
  505. "System.Web.Profile.ProfileGroupBase", true, null);
  506. }
  507. string baseType = ps.Inherits;
  508. bool baseIsGlobal = false;
  509. if (String.IsNullOrEmpty (baseType)) {
  510. baseType = "System.Web.Profile.ProfileBase";
  511. baseIsGlobal = true;
  512. }
  513. BuildProfileClass (ps, "ProfileCommon", props, ns, baseType, baseIsGlobal, groupProperties);
  514. return true;
  515. }
  516. // void PutCustomProfileInContext (HttpContext context, string assemblyName)
  517. // {
  518. // Type type = Type.GetType (String.Format ("ProfileCommon, {0}",
  519. // Path.GetFileNameWithoutExtension (assemblyName)));
  520. // ProfileBase pb = Activator.CreateInstance (type) as ProfileBase;
  521. // if (pb != null)
  522. // context.Profile = pb;
  523. // }
  524. public static bool HaveCustomProfile (ProfileSection ps)
  525. {
  526. if (ps == null || !ps.Enabled)
  527. return false;
  528. if (!String.IsNullOrEmpty (ps.Inherits) || (ps.PropertySettings != null && ps.PropertySettings.Count > 0))
  529. return true;
  530. return false;
  531. }
  532. public void Compile ()
  533. {
  534. if (_alreadyCompiled)
  535. return;
  536. string appCode = Path.Combine (HttpRuntime.AppDomainAppPath, "App_Code");
  537. ProfileSection ps = WebConfigurationManager.GetSection ("system.web/profile") as ProfileSection;
  538. bool haveAppCodeDir = Directory.Exists (appCode);
  539. bool haveCustomProfile = HaveCustomProfile (ps);
  540. if (!haveAppCodeDir && !haveCustomProfile)
  541. return;
  542. AppCodeAssembly defasm = new AppCodeAssembly ("App_Code", appCode);
  543. assemblies.Add (defasm);
  544. bool haveCode = false;
  545. if (haveAppCodeDir)
  546. haveCode = ProcessAppCodeDir (appCode, defasm);
  547. if (haveCustomProfile)
  548. if (ProcessCustomProfile (ps, defasm))
  549. haveCode = true;
  550. if (!haveCode)
  551. return;
  552. HttpRuntime.EnableAssemblyMapping (true);
  553. string[] binAssemblies = HttpApplication.BinDirectoryAssemblies;
  554. foreach (AppCodeAssembly aca in assemblies)
  555. aca.Build (binAssemblies);
  556. _alreadyCompiled = true;
  557. DefaultAppCodeAssemblyName = Path.GetFileNameWithoutExtension (defasm.OutputAssemblyName);
  558. if (haveCustomProfile && providerTypeName != null) {
  559. if (Type.GetType (providerTypeName, false) == null) {
  560. foreach (Assembly asm in BuildManager.TopLevelAssemblies) {
  561. if (asm == null)
  562. continue;
  563. if (asm.GetType (providerTypeName, false) != null)
  564. return;
  565. }
  566. } else
  567. return;
  568. if (HttpApplication.LoadTypeFromBin (providerTypeName) == null)
  569. throw new HttpException (String.Format ("Profile provider type not found: {0}",
  570. providerTypeName));
  571. }
  572. }
  573. private bool CollectFiles (string dir, AppCodeAssembly aca)
  574. {
  575. bool haveFiles = false;
  576. AppCodeAssembly curaca = aca;
  577. foreach (string f in Directory.GetFiles (dir)) {
  578. aca.AddFile (f);
  579. haveFiles = true;
  580. }
  581. foreach (string d in Directory.GetDirectories (dir)) {
  582. foreach (AppCodeAssembly a in assemblies)
  583. if (a.SourcePath == d) {
  584. curaca = a;
  585. break;
  586. }
  587. if (CollectFiles (d, curaca))
  588. haveFiles = true;
  589. curaca = aca;
  590. }
  591. return haveFiles;
  592. }
  593. }
  594. }
  595. #endif