AppCodeCompiler.cs 21 KB

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