AppCodeCompiler.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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;
  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. }
  186. }
  187. outputAssemblyName = (string)FileUtils.CreateTemporaryFile (
  188. AppDomain.CurrentDomain.SetupInformation.DynamicBase,
  189. name, "dll", OnCreateTemporaryAssemblyFile);
  190. parameters.OutputAssembly = outputAssemblyName;
  191. foreach (Assembly a in BuildManager.TopLevelAssemblies)
  192. parameters.ReferencedAssemblies.Add (a.Location);
  193. CompilerResults results = abuilder.BuildAssembly (parameters);
  194. if (results.Errors.Count == 0) {
  195. BuildManager.CodeAssemblies.Add (results.CompiledAssembly);
  196. BuildManager.TopLevelAssemblies.Add (results.CompiledAssembly);
  197. HttpRuntime.WritePreservationFile (results.CompiledAssembly, name);
  198. } else {
  199. if (HttpContext.Current.IsCustomErrorEnabled)
  200. throw new HttpException ("An error occurred while initializing application.");
  201. throw new CompilationException (null, results.Errors, null);
  202. }
  203. }
  204. private string PhysicalToVirtual (string file)
  205. {
  206. return file.Replace (HttpRuntime.AppDomainAppPath, "/").Replace (Path.DirectorySeparatorChar, '/');
  207. }
  208. private BuildProvider GetBuildProviderFor (string file, BuildProviderCollection buildProviders)
  209. {
  210. if (file == null || file.Length == 0 || buildProviders == null || buildProviders.Count == 0)
  211. return null;
  212. BuildProvider ret = buildProviders.GetProviderForExtension (Path.GetExtension (file));
  213. if (ret != null && IsCorrectBuilderType (ret)) {
  214. ret.SetVirtualPath (PhysicalToVirtual (file));
  215. return ret;
  216. }
  217. return null;
  218. }
  219. private bool IsCorrectBuilderType (BuildProvider bp)
  220. {
  221. if (bp == null)
  222. return false;
  223. Type type;
  224. object[] attrs;
  225. type = bp.GetType ();
  226. attrs = type.GetCustomAttributes (true);
  227. if (attrs == null)
  228. return false;
  229. BuildProviderAppliesToAttribute bpAppliesTo;
  230. bool attributeFound = false;
  231. foreach (object attr in attrs) {
  232. bpAppliesTo = attr as BuildProviderAppliesToAttribute;
  233. if (bpAppliesTo == null)
  234. continue;
  235. attributeFound = true;
  236. if ((bpAppliesTo.AppliesTo & BuildProviderAppliesTo.All) == BuildProviderAppliesTo.All ||
  237. (bpAppliesTo.AppliesTo & BuildProviderAppliesTo.Code) == BuildProviderAppliesTo.Code)
  238. return true;
  239. }
  240. if (attributeFound)
  241. return false;
  242. return true;
  243. }
  244. }
  245. internal class AppCodeCompiler
  246. {
  247. static private bool _alreadyCompiled;
  248. internal static string DefaultAppCodeAssemblyName;
  249. // A dictionary that contains an entry per an assembly that will
  250. // be produced by compiling App_Code. There's one main assembly
  251. // and an optional number of assemblies as defined by the
  252. // codeSubDirectories sub-element of the compilation element in
  253. // the system.web section of the app's config file.
  254. // Each entry's value is an AppCodeAssembly instance.
  255. //
  256. // Assemblies are named as follows:
  257. //
  258. // 1. main assembly: App_Code.{HASH}
  259. // 2. subdir assemblies: App_SubCode_{DirName}.{HASH}
  260. //
  261. // If any of the assemblies contains files that would be
  262. // compiled with different compilers, a System.Web.HttpException
  263. // is thrown.
  264. //
  265. // Files for which there is no explicit builder are ignored
  266. // silently
  267. //
  268. // Files for which exist BuildProviders but which have no
  269. // unambiguous language assigned to them (e.g. .wsdl files), are
  270. // built using the default website compiler.
  271. private List<AppCodeAssembly> assemblies;
  272. string _bindir = null;
  273. string providerTypeName = null;
  274. public AppCodeCompiler ()
  275. {
  276. assemblies = new List<AppCodeAssembly>();
  277. }
  278. bool ProcessAppCodeDir (string appCode, AppCodeAssembly defasm)
  279. {
  280. // First process the codeSubDirectories
  281. CompilationSection cs = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
  282. if (cs != null) {
  283. string aname;
  284. for (int i = 0; i < cs.CodeSubDirectories.Count; i++) {
  285. aname = String.Format ("App_SubCode_{0}", cs.CodeSubDirectories[i].DirectoryName);
  286. assemblies.Add (new AppCodeAssembly (
  287. aname,
  288. Path.Combine (appCode, cs.CodeSubDirectories[i].DirectoryName)));
  289. }
  290. }
  291. return CollectFiles (appCode, defasm);
  292. }
  293. CodeTypeReference GetProfilePropertyType (string type)
  294. {
  295. if (String.IsNullOrEmpty (type))
  296. throw new ArgumentException ("String size cannot be 0", "type");
  297. return new CodeTypeReference (type);
  298. }
  299. Type GetTypeFromBin (string typeName)
  300. {
  301. string bindir = BinDir;
  302. if (!Directory.Exists (bindir))
  303. return null;
  304. string [] binDlls = Directory.GetFiles (bindir, "*.dll");
  305. Type ret = null;
  306. foreach (string dll in binDlls) {
  307. try {
  308. Assembly asm = Assembly.LoadFrom (dll);
  309. ret = asm.GetType (typeName, false);
  310. if (ret != null)
  311. break;
  312. } catch (Exception) {
  313. continue;
  314. }
  315. }
  316. return ret;
  317. }
  318. string FindProviderTypeName (ProfileSection ps, string providerName)
  319. {
  320. if (ps.Providers == null || ps.Providers.Count == 0)
  321. return null;
  322. ProviderSettings pset = ps.Providers [providerName];
  323. if (pset == null)
  324. return null;
  325. return pset.Type;
  326. }
  327. void GetProfileProviderAttribute (ProfileSection ps, CodeAttributeDeclarationCollection collection,
  328. string providerName)
  329. {
  330. if (String.IsNullOrEmpty (providerName))
  331. providerTypeName = FindProviderTypeName (ps, ps.DefaultProvider);
  332. else
  333. providerTypeName = FindProviderTypeName (ps, providerName);
  334. if (providerTypeName == null)
  335. throw new HttpException (String.Format ("Profile provider type not defined: {0}",
  336. providerName));
  337. collection.Add (
  338. new CodeAttributeDeclaration (
  339. "ProfileProvider",
  340. new CodeAttributeArgument (
  341. new CodePrimitiveExpression (providerTypeName)
  342. )
  343. )
  344. );
  345. }
  346. void GetProfileSettingsSerializeAsAttribute (ProfileSection ps, CodeAttributeDeclarationCollection collection,
  347. SerializationMode mode)
  348. {
  349. string parameter = String.Format ("SettingsSerializeAs.{0}", mode.ToString ());
  350. collection.Add (
  351. new CodeAttributeDeclaration (
  352. "SettingsSerializeAs",
  353. new CodeAttributeArgument (
  354. new CodeSnippetExpression (parameter)
  355. )
  356. )
  357. );
  358. }
  359. void AddProfileClassGetProfileMethod (CodeTypeDeclaration profileClass)
  360. {
  361. CodeMethodReferenceExpression mref = new CodeMethodReferenceExpression (
  362. new CodeTypeReferenceExpression (typeof (System.Web.Profile.ProfileBase)),
  363. "Create");
  364. CodeMethodInvokeExpression minvoke = new CodeMethodInvokeExpression (
  365. mref,
  366. new CodeExpression[] { new CodeVariableReferenceExpression ("username") }
  367. );
  368. CodeCastExpression cast = new CodeCastExpression ();
  369. cast.TargetType = new CodeTypeReference ("ProfileCommon");
  370. cast.Expression = minvoke;
  371. CodeMethodReturnStatement ret = new CodeMethodReturnStatement ();
  372. ret.Expression = cast;
  373. CodeMemberMethod method = new CodeMemberMethod ();
  374. method.Name = "GetProfile";
  375. method.ReturnType = new CodeTypeReference ("ProfileCommon");
  376. method.Parameters.Add (new CodeParameterDeclarationExpression("System.String", "username"));
  377. method.Statements.Add (ret);
  378. method.Attributes = MemberAttributes.Public;
  379. profileClass.Members.Add (method);
  380. }
  381. void AddProfileClassProperty (ProfileSection ps, CodeTypeDeclaration profileClass, ProfilePropertySettings pset)
  382. {
  383. string name = pset.Name;
  384. if (String.IsNullOrEmpty (name))
  385. throw new HttpException ("Profile property 'Name' attribute cannot be null.");
  386. CodeMemberProperty property = new CodeMemberProperty ();
  387. string typeName = pset.Type;
  388. if (typeName == "string")
  389. typeName = "System.String";
  390. property.Name = name;
  391. property.Type = GetProfilePropertyType (typeName);
  392. property.Attributes = MemberAttributes.Public;
  393. CodeAttributeDeclarationCollection collection = new CodeAttributeDeclarationCollection();
  394. GetProfileProviderAttribute (ps, collection, pset.Provider);
  395. GetProfileSettingsSerializeAsAttribute (ps, collection, pset.SerializeAs);
  396. property.CustomAttributes = collection;
  397. CodeMethodReturnStatement ret = new CodeMethodReturnStatement ();
  398. CodeCastExpression cast = new CodeCastExpression ();
  399. ret.Expression = cast;
  400. CodeMethodReferenceExpression mref = new CodeMethodReferenceExpression (
  401. new CodeThisReferenceExpression (),
  402. "GetPropertyValue");
  403. CodeMethodInvokeExpression minvoke = new CodeMethodInvokeExpression (
  404. mref,
  405. new CodeExpression[] { new CodePrimitiveExpression (name) }
  406. );
  407. cast.TargetType = new CodeTypeReference (typeName);
  408. cast.Expression = minvoke;
  409. property.GetStatements.Add (ret);
  410. if (!pset.ReadOnly) {
  411. mref = new CodeMethodReferenceExpression (
  412. new CodeThisReferenceExpression (),
  413. "SetPropertyValue");
  414. minvoke = new CodeMethodInvokeExpression (
  415. mref,
  416. new CodeExpression[] { new CodePrimitiveExpression (name), new CodeSnippetExpression ("value") }
  417. );
  418. property.SetStatements.Add (minvoke);
  419. }
  420. profileClass.Members.Add (property);
  421. }
  422. void AddProfileClassGroupProperty (string groupName, string memberName, CodeTypeDeclaration profileClass)
  423. {
  424. CodeMemberProperty property = new CodeMemberProperty ();
  425. property.Name = memberName;
  426. property.Type = new CodeTypeReference (groupName);
  427. property.Attributes = MemberAttributes.Public;
  428. CodeMethodReturnStatement ret = new CodeMethodReturnStatement ();
  429. CodeCastExpression cast = new CodeCastExpression ();
  430. ret.Expression = cast;
  431. CodeMethodReferenceExpression mref = new CodeMethodReferenceExpression (
  432. new CodeThisReferenceExpression (),
  433. "GetProfileGroup");
  434. CodeMethodInvokeExpression minvoke = new CodeMethodInvokeExpression (
  435. mref,
  436. new CodeExpression[] { new CodePrimitiveExpression (memberName) }
  437. );
  438. cast.TargetType = new CodeTypeReference (groupName);
  439. cast.Expression = minvoke;
  440. property.GetStatements.Add (ret);
  441. profileClass.Members.Add (property);
  442. }
  443. void BuildProfileClass (ProfileSection ps, string className, ProfilePropertySettingsCollection psc,
  444. CodeNamespace ns, string baseClass, bool baseIsGlobal,
  445. SortedList <string, string> groupProperties)
  446. {
  447. CodeTypeDeclaration profileClass = new CodeTypeDeclaration (className);
  448. CodeTypeReference cref = new CodeTypeReference (baseClass);
  449. if (baseIsGlobal)
  450. cref.Options |= CodeTypeReferenceOptions.GlobalReference;
  451. profileClass.BaseTypes.Add (cref);
  452. profileClass.TypeAttributes = TypeAttributes.Public;
  453. ns.Types.Add (profileClass);
  454. foreach (ProfilePropertySettings pset in psc)
  455. AddProfileClassProperty (ps, profileClass, pset);
  456. if (groupProperties != null && groupProperties.Count > 0)
  457. foreach (KeyValuePair <string, string> group in groupProperties)
  458. AddProfileClassGroupProperty (group.Key, group.Value, profileClass);
  459. AddProfileClassGetProfileMethod (profileClass);
  460. }
  461. string MakeGroupName (string name)
  462. {
  463. return String.Format ("ProfileGroup{0}", name);
  464. }
  465. // FIXME: there should be some validation of syntactic correctness of the member/class name
  466. // for the groups/properties. For now it's left to the compiler to report errors.
  467. //
  468. // CodeGenerator.IsValidLanguageIndependentIdentifier (id) - use that
  469. //
  470. bool ProcessCustomProfile (ProfileSection ps, AppCodeAssembly defasm)
  471. {
  472. CodeCompileUnit unit = new CodeCompileUnit ();
  473. CodeNamespace ns = new CodeNamespace (null);
  474. unit.Namespaces.Add (ns);
  475. defasm.AddUnit (unit);
  476. ns.Imports.Add (new CodeNamespaceImport ("System"));
  477. ns.Imports.Add (new CodeNamespaceImport ("System.Configuration"));
  478. ns.Imports.Add (new CodeNamespaceImport ("System.Web"));
  479. ns.Imports.Add (new CodeNamespaceImport ("System.Web.Profile"));
  480. RootProfilePropertySettingsCollection props = ps.PropertySettings;
  481. if (props == null || props.Count == 0)
  482. return true;
  483. SortedList<string, string> groupProperties = new SortedList<string, string> ();
  484. string groupName;
  485. foreach (ProfileGroupSettings pgs in props.GroupSettings) {
  486. groupName = MakeGroupName (pgs.Name);
  487. groupProperties.Add (groupName, pgs.Name);
  488. BuildProfileClass (ps, groupName, pgs.PropertySettings, ns,
  489. "System.Web.Profile.ProfileGroupBase", true, null);
  490. }
  491. string baseType = ps.Inherits;
  492. bool baseIsGlobal = false;
  493. if (String.IsNullOrEmpty (baseType)) {
  494. baseType = "System.Web.Profile.ProfileBase";
  495. baseIsGlobal = true;
  496. }
  497. BuildProfileClass (ps, "ProfileCommon", props, ns, baseType, baseIsGlobal, groupProperties);
  498. return true;
  499. }
  500. // void PutCustomProfileInContext (HttpContext context, string assemblyName)
  501. // {
  502. // Type type = Type.GetType (String.Format ("ProfileCommon, {0}",
  503. // Path.GetFileNameWithoutExtension (assemblyName)));
  504. // ProfileBase pb = Activator.CreateInstance (type) as ProfileBase;
  505. // if (pb != null)
  506. // context.Profile = pb;
  507. // }
  508. public void Compile ()
  509. {
  510. if (_alreadyCompiled)
  511. return;
  512. _alreadyCompiled = false;
  513. string appCode = Path.Combine (HttpRuntime.AppDomainAppPath, "App_Code");
  514. ProfileSection ps = WebConfigurationManager.GetSection ("system.web/profile") as ProfileSection;
  515. bool haveAppCodeDir = Directory.Exists (appCode);
  516. bool haveCustomProfile = ps != null ? ps.PropertySettings.Count > 0 : false;
  517. if (!haveAppCodeDir && !haveCustomProfile)
  518. return;
  519. AppCodeAssembly defasm = new AppCodeAssembly ("App_Code", appCode);
  520. assemblies.Add (defasm);
  521. bool haveCode = false;
  522. if (haveAppCodeDir)
  523. haveCode = ProcessAppCodeDir (appCode, defasm);
  524. if (haveCustomProfile)
  525. if (ProcessCustomProfile (ps, defasm))
  526. haveCode = true;
  527. if (!haveCode)
  528. return;
  529. HttpRuntime.EnableAssemblyMapping (true);
  530. string bindir = BinDir;
  531. string[] binAssemblies = null;
  532. if (Directory.Exists (bindir))
  533. binAssemblies = Directory.GetFiles (bindir, "*.dll");
  534. foreach (AppCodeAssembly aca in assemblies)
  535. aca.Build (binAssemblies);
  536. DefaultAppCodeAssemblyName = Path.GetFileNameWithoutExtension (defasm.OutputAssemblyName);
  537. if (haveCustomProfile && providerTypeName != null) {
  538. if (Type.GetType (providerTypeName, false) == null) {
  539. foreach (Assembly asm in BuildManager.TopLevelAssemblies) {
  540. if (asm == null)
  541. continue;
  542. if (asm.GetType (providerTypeName, false) != null)
  543. return;
  544. }
  545. } else
  546. return;
  547. if (GetTypeFromBin (providerTypeName) == null)
  548. throw new HttpException (String.Format ("Profile provider type not found: {0}",
  549. providerTypeName));
  550. }
  551. }
  552. private bool CollectFiles (string dir, AppCodeAssembly aca)
  553. {
  554. bool haveFiles = false;
  555. AppCodeAssembly curaca = aca;
  556. foreach (string f in Directory.GetFiles (dir)) {
  557. aca.AddFile (f);
  558. haveFiles = true;
  559. }
  560. foreach (string d in Directory.GetDirectories (dir)) {
  561. foreach (AppCodeAssembly a in assemblies)
  562. if (a.SourcePath == d) {
  563. curaca = a;
  564. break;
  565. }
  566. if (CollectFiles (d, curaca))
  567. haveFiles = true;
  568. curaca = aca;
  569. }
  570. return haveFiles;
  571. }
  572. private string BinDir {
  573. get {
  574. if (_bindir != null)
  575. return _bindir;
  576. AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
  577. _bindir = Path.Combine (setup.ApplicationBase, setup.PrivateBinPath);
  578. return _bindir;
  579. }
  580. }
  581. }
  582. }
  583. #endif