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