File: ServiceParser.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (949 lines) | stat: -rw-r--r-- 37,679 bytes parent folder | download | duplicates (7)
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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------

namespace System.ServiceModel.Activation
{
    using System.CodeDom;
    using System.CodeDom.Compiler;
    using System.Collections;
    using System.Collections.Specialized;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Globalization;
    using System.IO;
    using System.Reflection;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Web.Hosting;
    using System.Web.Compilation;
    using System.Web.RegularExpressions;
    using System.ServiceModel.Activation.Diagnostics;
    using System.Security;
    using System.Runtime.Diagnostics;
    using System.Diagnostics.CodeAnalysis;
    using System.Runtime;

    /// <summary>
    /// This class will parse the .svc file and maintains a list of useful information that the build
    /// provider needs in order to compile the file. The parser creates a list of dependent assemblies,
    /// understands the compiler that we need to use, fully parses all the supported directives etc.
    /// </summary>
    /// <remarks>
    /// The class is not thread-safe.
    /// </remarks>
#pragma warning disable 618 // have not moved to the v4 security model yet
    [SecurityCritical(SecurityCriticalScope.Everything)]
#pragma warning restore 618
    class ServiceParser
    {
        // the delimiter for the compiled custom string
        const string Delimiter = ServiceHostingEnvironment.ServiceParserDelimiter;

        // attribute names
        const string DefaultDirectiveName = "ServiceHost";
        const string FactoryAttributeName = "Factory";
        const string ServiceAttributeName = "Service";

        // regular exression for the directive
        readonly static SimpleDirectiveRegex directiveRegex;

        // the build provider we will work with 
        ServiceBuildProvider buildProvider;

        // text for the file
        string serviceText;

        // the class attribute value
        string factoryAttributeValue = string.Empty;

        // the constructorstring
        string serviceAttributeValue = string.Empty;

        // the line number in file currently being parsed
        int lineNumber;

        // the column number in file currently being parsed
        int startColumn;

        // the main directive was found or not
        bool foundMainDirective;

        // the type of the compiler (i.e C#)
        CompilerType compilerType;

        // the string containing the code to be compiled,
        // it will be null when all the code is "behind"
        string sourceString;

        // assemblies to be linked with, we need a unique list
        // of them and we maintain a Dictionary for it.
        HybridDictionary linkedAssemblies;

        // the set of assemblies that the build system is 
        // telling us we will be linked with. There is no unique
        // requirement for them.
        ICollection referencedAssemblies;

        // used to figure out where the new lines start
        static char[] newlineChars = new char[] { '\r', '\n' };

        // source file dependencies
        HybridDictionary sourceDependencies;

        // virtual path for the file that we are parsing
        string virtualPath;

        [SuppressMessage(FxCop.Category.Security, FxCop.Rule.AptcaMethodsShouldOnlyCallAptcaMethods, Justification = "Users cannot pass arbitrary data to this code.")]
        static ServiceParser()
        {
            directiveRegex = new SimpleDirectiveRegex();
        }

        /// <summary>
        /// The Contructor needs the path to the file that it will parse and a reference to
        /// the build provider that we are using. This is necessary because there are things that 
        /// need to be set on the build provider directly as we are parsing...
        /// </summary>
        internal ServiceParser(string virtualPath, ServiceBuildProvider buildProvider)
        {
            if (DiagnosticUtility.ShouldTraceInformation)
            {
                TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.WebHostCompilation, SR.TraceCodeWebHostCompilation,
                    new StringTraceRecord("VirtualPath", virtualPath), this, (Exception)null);
            }

            this.virtualPath = virtualPath;
            this.buildProvider = buildProvider;
        }

        /// <summary>
        /// Constructor that is used when the whole svc file content is provided. This is the case
        /// when the COM+ Admin tool calls into it.
        /// </summary>
        ServiceParser(string serviceText)
        {
            this.serviceText = serviceText;
            this.buildProvider = new ServiceBuildProvider();
        }

        /// <summary>
        /// Parsing the content of the service file and retrieve the serviceAttributeValue attribute for ComPlus.
        /// </summary>
        /// <param name="serviceText">The content of the service file.</param>
        /// <returns>The "serviceAttributeValue" attribute of the Service directive. </returns>
        /// <exception cref="System.Web.HttpParseException"/>
        internal static IDictionary<string, string> ParseServiceDirective(string serviceText)
        {
            ServiceParser parser = new ServiceParser(serviceText);
            parser.ParseString();

            // the list of valid attributes for ComPlus for Service Directive
            IDictionary<string, string> attributeTable = new Dictionary<string, string>(
                StringComparer.OrdinalIgnoreCase);

            if (!string.IsNullOrEmpty(parser.factoryAttributeValue))
                attributeTable.Add(FactoryAttributeName, parser.factoryAttributeValue);

            if (!string.IsNullOrEmpty(parser.serviceAttributeValue))
                attributeTable.Add(ServiceAttributeName, parser.serviceAttributeValue);

            return attributeTable;
        }

        /// <summary>
        /// </summary>

        // various getters for private objects that the build
        // provider will need
        //
        internal CompilerType CompilerType
        {
            get
            {
                return compilerType;
            }
        }

        internal ICollection AssemblyDependencies
        {
            get
            {
                if (linkedAssemblies == null)
                {
                    return null;
                }

                return linkedAssemblies.Keys;
            }
        }

        internal ICollection SourceDependencies
        {
            get
            {
                if (sourceDependencies == null)
                {
                    return null;
                }

                return sourceDependencies.Keys;
            }
        }

        internal bool HasInlineCode
        {
            get
            {
                return (sourceString != null);
            }
        }

        /// <summary>
        /// Parses the code file appropriately. This method is used by the
        /// build provider.
        /// </summary>
        internal void Parse(ICollection referencedAssemblies)
        {
            if (referencedAssemblies == null)
            {
                throw FxTrace.Exception.ArgumentNull("referencedAssemblies");
            }

            this.referencedAssemblies = referencedAssemblies;
            AddSourceDependency(virtualPath);

            using (TextReader reader = buildProvider.OpenReaderInternal())
            {
                this.serviceText = reader.ReadToEnd();
                ParseString();
            }
        }

        /// <summary>
        /// This method returns a code compile unit that will be added
        /// to the other depdnecies in order to compile
        /// </summary>
        internal CodeCompileUnit GetCodeModel()
        {
            // Do we have something to compile?
            //
            if (sourceString == null || sourceString.Length == 0)
                return null;

            CodeSnippetCompileUnit snippetCompileUnit = new CodeSnippetCompileUnit(sourceString);

            // Put in some context so that the file can be debugged.
            //
            string pragmaFile = HostingEnvironmentWrapper.MapPath(virtualPath);
            snippetCompileUnit.LinePragma = new CodeLinePragma(pragmaFile, lineNumber);

            return snippetCompileUnit;
        }

        Exception CreateParseException(string message, string sourceCode)
        {
            return CreateParseException(message, null, sourceCode);
        }

        Exception CreateParseException(Exception innerException, string sourceCode)
        {
            return CreateParseException(innerException.Message, innerException, sourceCode);
        }

        Exception CreateParseException(string message, Exception innerException, string sourceCode)
        {
            return new HttpParseException(message, innerException, this.virtualPath, sourceCode, this.lineNumber);
        }

        /// <summary>
        /// This method returns the custom string that is to be passed to ServiceHostingEnvironment from BuildManager.
        /// </summary>
        /// <param name="compiledAssembly">The full name of the built assembly for inline code.</param>
        internal string CreateParseString(Assembly compiledAssembly)
        {
            Type typeToPreserve = this.GetCompiledType(compiledAssembly);
            string typeToPreserveName = string.Empty;
            if (typeToPreserve != null)
                typeToPreserveName = typeToPreserve.AssemblyQualifiedName;

            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            if (compiledAssembly != null)
            {
                builder.Append(Delimiter);
                builder.Append(compiledAssembly.FullName);
            }

            if (this.referencedAssemblies != null)
            {
                // CSDMain #192135
                // Minimize code change by doing 2 passes to have assembly containing type at the top of the list.
                // As a result, this assembly will get loaded first in ServiceHostFactory.CreateServiceHost.
                // In the multi-targetting scenario this prevents the runtime from trying to load a newer CLR assembly
                // and failing.  In the happy case, duplicate assembly references may occur (no effect on runtime).
                // Note that if the service type is contained in a framework assembly, this does not fix the problem.
                // Future improvement is to write fully qualified type name and let CLR handle load/search.
                if (!string.IsNullOrEmpty(serviceAttributeValue))
                {
                    foreach (Assembly assembly in this.referencedAssemblies)
                    {
                        Type serviceType;
                        try
                        {
                            serviceType = assembly.GetType(serviceAttributeValue, false);
                        }
                        catch (Exception e)
                        {
                            if (System.Runtime.Fx.IsFatal(e))
                            {
                                throw;
                            }

                            // log exception, but do not rethrow
                            DiagnosticUtility.TraceHandledException(e, TraceEventType.Warning);

                            break;
                        }

                        if (serviceType != null)
                        {
                            builder.Append(Delimiter);
                            builder.Append(assembly.FullName);
                            break;
                        }
                    }
                }

                foreach (Assembly assembly in this.referencedAssemblies)
                {
                    builder.Append(Delimiter);
                    builder.Append(assembly.FullName);
                }
            }

            if (this.AssemblyDependencies != null)
            {
                foreach (Assembly assembly in this.AssemblyDependencies)
                {
                    builder.Append(Delimiter);
                    builder.Append(assembly.FullName);
                }
            }
            // use application relative virtualpath instead of the absolute path 
            // so that the compliedcustomstring is applicationame independent
            return string.Concat(VirtualPathUtility.ToAppRelative(virtualPath), Delimiter,
                typeToPreserveName, Delimiter,
                serviceAttributeValue, builder.ToString());
        }

        void AddSourceDependency(string fileName)
        {
            if (sourceDependencies == null)
                sourceDependencies = new HybridDictionary(true);

            sourceDependencies.Add(fileName, fileName);
        }

        Type GetCompiledType(Assembly compiledAssembly)
        {
            if (string.IsNullOrEmpty(factoryAttributeValue))
            {
                return null;
            }

            Type type = null;

            // First, try to get the type from the assembly that has been built (if any)
            if (this.HasInlineCode && (compiledAssembly != null))
            {
                type = compiledAssembly.GetType(factoryAttributeValue);
            }

            // If not, try to get it from other assemblies
            if (type == null)
            {
                type = GetType(factoryAttributeValue);
            }

            return type;
        }

        internal IDictionary GetLinePragmasTable()
        {
            LinePragmaCodeInfo info = new LinePragmaCodeInfo(this.lineNumber, this.startColumn, 1, -1, false);
            IDictionary dictionary = new Hashtable();
            dictionary[this.lineNumber] = info;
            return dictionary;
        }

        /// <summary>
        /// Parses the content of the svc file for each directive line
        /// </summary>
        void ParseString()
        {
            try
            {
                int textPos = 0;
                Match match;
                lineNumber = 1;

                // Check for ending bracket first, MB 45013.
                if (this.serviceText.IndexOf('>') == -1)
                {
                    throw FxTrace.Exception.AsError(new HttpException(SR.Hosting_BuildProviderDirectiveEndBracketMissing(ServiceParser.DefaultDirectiveName)));
                }

                // First, parse all the <%@ ... %> directives
                //
                for (;;)
                {
                    match = directiveRegex.Match(this.serviceText, textPos);

                    // Done with the directives?
                    //
                    if (!match.Success)
                        break;

                    lineNumber += ServiceParserUtilities.LineCount(this.serviceText, textPos, match.Index);
                    textPos = match.Index;

                    // Get all the directives into a bag
                    //
                    IDictionary directive = CollectionsUtil.CreateCaseInsensitiveSortedList();
                    string directiveName = ProcessAttributes(match, directive);

                    // Understand the directive
                    //
                    ProcessDirective(directiveName, directive);
                    lineNumber += ServiceParserUtilities.LineCount(this.serviceText, textPos, match.Index + match.Length);
                    textPos = match.Index + match.Length;

                    // Fixup line and column numbers to have meaninglful errors
                    //
                    int newlineIndex = this.serviceText.LastIndexOfAny(newlineChars, textPos - 1);
                    startColumn = textPos - newlineIndex;
                }

                if (!foundMainDirective)
                {
                    throw FxTrace.Exception.AsError(new HttpException(SR.Hosting_BuildProviderDirectiveMissing(ServiceParser.DefaultDirectiveName)));
                }

                // skip the directives chunk
                //
                string remainingText = this.serviceText.Substring(textPos);

                // If there is something else in the file, it needs to be compiled
                //
                if (!ServiceParserUtilities.IsWhiteSpaceString(remainingText))
                {
                    sourceString = remainingText;
                }
            }
            catch (HttpException e)
            {
                // the string is set in the internal exception, no need to set it again.
                //
                Exception parseException = CreateParseException(e, this.serviceText);
                throw FxTrace.Exception.AsError(
                    new HttpCompileException(parseException.Message, parseException));
            }
        }

        /// <summary>
        /// Return the directive if it exists or an empty string
        /// </summary>
        string ProcessAttributes(Match match, IDictionary attribs)
        {
            // creates 3 parallel capture collections
            // for the attribute names, the attribute values and the
            // equal signs
            //
            string ret = String.Empty;

            CaptureCollection attrnames = match.Groups["attrname"].Captures;
            CaptureCollection attrvalues = match.Groups["attrval"].Captures;
            CaptureCollection equalsign = match.Groups["equal"].Captures;

            // Iterate through all of them and add then to 
            // the dictionary of attributes
            //
            for (int i = 0; i < attrnames.Count; i++)
            {
                string attribName = attrnames[i].ToString();
                string attribValue = attrvalues[i].ToString();

                // Check if there is an equal sign.
                //
                bool fHasEqual = (equalsign[i].ToString().Length > 0);

                if (attribName != null)
                {
                    // A <%@ %> block can have two formats:
                    // <%@ directive foo=1 bar=hello %>
                    // <%@ foo=1 bar=hello %>
                    // Check if we have the first format
                    //
                    if (!fHasEqual && i == 0)
                    {
                        // return the main directive
                        //
                        ret = attribName;
                        continue;
                    }

                    try
                    {
                        if (attribs != null)
                            attribs.Add(attribName, attribValue);
                    }
                    catch (ArgumentException)
                    {
                        throw FxTrace.Exception.AsError(new HttpException(SR.Hosting_BuildProviderDuplicateAttribute(attribName)));
                    }
                }
            }
            return ret;
        }

        /// <summary>
        /// This method understands the compilation parameters if any ...
        /// </summary>
        [SuppressMessage(FxCop.Category.Security, FxCop.Rule.DoNotIndirectlyExposeMethodsWithLinkDemands, Justification = "This method doesn't allow callers to access sensitive information, operations, or resources that can be used in a destructive manner.")]
        void ProcessCompilationParams(IDictionary directive, CompilerParameters compilParams)
        {
            bool debug = false;
            if (ServiceParserUtilities.GetAndRemoveBooleanAttribute(directive, "debug", ref debug))
            {
                compilParams.IncludeDebugInformation = debug;
            }

            int warningLevel = 0;
            if (ServiceParserUtilities.GetAndRemoveNonNegativeIntegerAttribute(directive, "warninglevel", ref warningLevel))
            {
                compilParams.WarningLevel = warningLevel;
                if (warningLevel > 0)
                    compilParams.TreatWarningsAsErrors = true;
            }

            string compilerOptions = ServiceParserUtilities.GetAndRemoveNonEmptyAttribute(directive, "compileroptions");
            if (compilerOptions != null)
            {
                compilParams.CompilerOptions = compilerOptions;
            }
        }


        /// <summary>
        /// Processes a directive block
        /// </summary>
        void ProcessDirective(string directiveName, IDictionary directive)
        {
            // Throw on empy, no directive specified
            //
            if (directiveName.Length == 0)
            {
                throw FxTrace.Exception.AsError(new HttpException(SR.Hosting_BuildProviderDirectiveNameMissing));
            }

            // Check for the main directive
            //
            if (string.Compare(directiveName, ServiceParser.DefaultDirectiveName, StringComparison.OrdinalIgnoreCase) == 0)
            {
                // Make sure the main directive was not already specified
                //
                if (foundMainDirective)
                {
                    throw FxTrace.Exception.AsError(new HttpException(SR.Hosting_BuildProviderDuplicateDirective(ServiceParser.DefaultDirectiveName)));
                }

                foundMainDirective = true;

                // Ignore 'codebehind' attribute (ASURT 4591)
                //
                directive.Remove("codebehind");

                string language = ServiceParserUtilities.GetAndRemoveNonEmptyAttribute(directive, "language");

                // Get the compiler for the specified language (if any)
                // or get the one from config
                //
                if (language != null)
                {
                    compilerType = buildProvider.GetDefaultCompilerTypeForLanguageInternal(language);
                }
                else
                {
                    compilerType = buildProvider.GetDefaultCompilerTypeInternal();
                }


                if (directive.Contains(FactoryAttributeName))
                {
                    factoryAttributeValue = ServiceParserUtilities.GetAndRemoveNonEmptyAttribute(directive, FactoryAttributeName);
                    serviceAttributeValue = ServiceParserUtilities.GetAndRemoveNonEmptyAttribute(directive, ServiceAttributeName);
                }
                else if (directive.Contains(ServiceAttributeName))
                {
                    serviceAttributeValue = ServiceParserUtilities.GetAndRemoveNonEmptyAttribute(directive, ServiceAttributeName);
                }
                else
                {
                    throw FxTrace.Exception.AsError(new HttpException(SR.Hosting_BuildProviderMainAttributeMissing));
                }
                // parse the parameters that are related to the compiler
                //
                ProcessCompilationParams(directive, compilerType.CompilerParameters);
            }
            else if (string.Compare(directiveName, "assembly", StringComparison.OrdinalIgnoreCase) == 0)
            {
                if (directive.Contains("name") && directive.Contains("src"))
                {
                    throw FxTrace.Exception.AsError(new HttpException(SR.Hosting_BuildProviderMutualExclusiveAttributes("src", "name")));
                }
                else if (directive.Contains("name"))
                {
                    string assemblyName = ServiceParserUtilities.GetAndRemoveNonEmptyAttribute(directive, "name");
                    if (assemblyName != null)
                    {
                        AddAssemblyDependency(assemblyName);
                    }
                    else
                        throw FxTrace.Exception.AsError(new HttpException(SR.Hosting_BuildProviderAttributeEmpty("name")));
                }
                else if (directive.Contains("src"))
                {
                    string srcPath = ServiceParserUtilities.GetAndRemoveNonEmptyAttribute(directive, "src");
                    if (srcPath != null)
                    {
                        ImportSourceFile(srcPath);
                    }
                    else
                        throw FxTrace.Exception.AsError(new HttpException(SR.Hosting_BuildProviderAttributeEmpty("src")));
                }
                else
                { // if (!directive.Contains("name") && !directive.Contains("src"))
                    throw FxTrace.Exception.AsError(new HttpException(SR.Hosting_BuildProviderRequiredAttributesMissing("src", "name")));
                }
            }
            else
            {
                throw FxTrace.Exception.AsError(new HttpException(SR.Hosting_BuildProviderUnknownDirective(directiveName)));
            }

            // check if there are any directives that you did not process 
            //
            if (directive.Count > 0)
                throw FxTrace.Exception.AsError(new HttpException(SR.Hosting_BuildProviderUnknownAttribute(ServiceParserUtilities.FirstDictionaryKey(directive))));
        }

        void ImportSourceFile(string path)
        {
            // Get a full path to the source file, compile it to an assembly
            // add the depedency to the assembly
            //
            string baseVirtualDir = VirtualPathUtility.GetDirectory(virtualPath);
            string fullVirtualPath = VirtualPathUtility.Combine(baseVirtualDir, path);

            AddSourceDependency(fullVirtualPath);
            Assembly a = BuildManager.GetCompiledAssembly(fullVirtualPath);
            AddAssemblyDependency(a);
        }

        void AddAssemblyDependency(string assemblyName)
        {
            // Load and keep track of the assembly
            //
            Assembly a = Assembly.Load(assemblyName);
            AddAssemblyDependency(a);
        }

        void AddAssemblyDependency(Assembly assembly)
        {
            if (linkedAssemblies == null)
                linkedAssemblies = new HybridDictionary(false);

            linkedAssemblies.Add(assembly, null);
        }

        /// <summary>
        /// Look for a type by name in the assemblies available to this page
        /// </summary>
        Type GetType(string typeName)
        {
            Type type;

            // If it contains an assembly name, just call Type.GetType (ASURT 53589)
            //
            if (ServiceParserUtilities.TypeNameIncludesAssembly(typeName))
            {
                try
                {
                    type = Type.GetType(typeName, true);
                }
                catch (ArgumentException e)
                {
                    Exception parseException = CreateParseException(e, this.sourceString);
                    throw FxTrace.Exception.AsError(
                        new HttpCompileException(parseException.Message, parseException));
                }
                catch (TargetInvocationException e)
                {
                    Exception parseException = CreateParseException(e, this.sourceString);
                    throw FxTrace.Exception.AsError(
                        new HttpCompileException(parseException.Message, parseException));
                }
                catch (TypeLoadException e)
                {
                    Exception parseException = CreateParseException(SR.Hosting_BuildProviderCouldNotCreateType(typeName), e, this.sourceString);
                    throw FxTrace.Exception.AsError(
                        new HttpCompileException(parseException.Message, parseException));
                }

                return type;
            }

            try
            {
                type = ServiceParserUtilities.GetTypeFromAssemblies(referencedAssemblies, typeName, false /*ignoreCase*/);
                if (type != null)
                    return type;

                type = ServiceParserUtilities.GetTypeFromAssemblies(AssemblyDependencies, typeName, false /*ignoreCase*/);
                if (type != null)
                    return type;
            }
            catch (HttpException e)
            {
                Exception parseException = CreateParseException(SR.Hosting_BuildProviderCouldNotCreateType(typeName), e, this.sourceString);
                throw FxTrace.Exception.AsError(
                        new HttpCompileException(parseException.Message, parseException));
            }

            Exception exception = CreateParseException(SR.Hosting_BuildProviderCouldNotCreateType(typeName), this.sourceString);
            throw FxTrace.Exception.AsError(
                        new HttpCompileException(exception.Message, exception));
        }

        /// <summary>
        /// This class contains static methods that are necessary to manipulate the 
        /// structures that contain the directives. The logic assumes that the parser will
        /// create a dictionary that contains all the directives and we can pull certain directives as
        /// necessary while processing/compiling the page. The directives are strings.
        /// 
        /// </summary>
        static class ServiceParserUtilities
        {
            /// <summary>
            /// Return the first key of the dictionary as a string.  Throws if it's
            /// empty or if the key is not a string.
            /// </summary>
            internal static string FirstDictionaryKey(IDictionary dictionary)
            {
                // assume that the caller has checked the dictionary before calling
                //
                IDictionaryEnumerator e = dictionary.GetEnumerator();
                e.MoveNext();
                return (string)e.Key;
            }

            /// <summary>
            /// Get a string value from a dictionary, and remove 
            /// it from the dictionary of attributes if it exists.
            /// </summary>
            /// <remarks>Returns null if the value was not there ...</remarks>
            static string GetAndRemove(IDictionary dictionary, string key)
            {
                string val = (string)dictionary[key];

                if (val != null)
                {
                    dictionary.Remove(key);
                    val = val.Trim();
                }
                else
                    return string.Empty;

                return val;
            }

            /// <summary>
            /// Get a value from a dictionary, and remove it from the dictionary if
            /// it exists.  Throw an exception if the value is a whitespace string.
            /// However, don't complain about null, which simply means the value is not
            /// in the dictionary.
            /// </summary>
            internal static string GetAndRemoveNonEmptyAttribute(IDictionary directives, string key, bool required)
            {
                string val = ServiceParserUtilities.GetAndRemove(directives, key);

                if (val.Length == 0)
                {
                    if (required)
                    {
                        throw FxTrace.Exception.AsError(new HttpException(SR.Hosting_BuildProviderAttributeMissing(key)));
                    }
                    return null;
                }

                return val;
            }


            internal static string GetAndRemoveNonEmptyAttribute(IDictionary directives, string key)
            {
                return GetAndRemoveNonEmptyAttribute(directives, key, false /*required*/);
            }

            /// <summary>
            /// Get a string value from a dictionary, and convert it to bool.  Throw an
            /// exception if it's not a valid bool string.
            /// However, don't complain about null, which simply means the value is not
            /// in the dictionary.
            /// The value is returned through a REF param (unchanged if null)
            /// </summary>
            /// <returns>True if attrib exists, false otherwise</returns>
            internal static bool GetAndRemoveBooleanAttribute(IDictionary directives, string key, ref bool val)
            {
                string s = ServiceParserUtilities.GetAndRemove(directives, key);

                if (s.Length == 0)
                    return false;

                try
                {
                    val = bool.Parse(s);
                }
                catch (FormatException)
                {
                    throw FxTrace.Exception.AsError(new HttpException(SR.Hosting_BuildProviderInvalidValueForBooleanAttribute(s, key)));
                }

                return true;
            }

            /// <summary>
            /// Get a string value from a dictionary, and convert it to integer.  Throw an
            /// exception if it's not a valid positive integer string.
            /// However, don't complain about null, which simply means the value is not
            /// in the dictionary.
            /// The value is returned through a REF param (unchanged if null)
            /// </summary>
            /// <returns>True if attrib exists, false otherwise</returns>
            internal static bool GetAndRemoveNonNegativeIntegerAttribute(IDictionary directives, string key, ref int val)
            {
                string s = ServiceParserUtilities.GetAndRemove(directives, key);

                if (s.Length == 0)
                    return false;

                val = GetNonNegativeIntegerAttribute(key, s);
                return true;
            }

            /// <summary>
            /// Parse a string attribute into a non-negative integer
            /// </summary>
            /// <param name="name">Name of the attribute, used only for the error messages</param>
            /// <param name="value">Value to convert to int</param>
            static int GetNonNegativeIntegerAttribute(string name, string value)
            {
                int ret;

                try
                {
                    ret = int.Parse(value, CultureInfo.InvariantCulture);
                }
                catch (FormatException)
                {
                    throw FxTrace.Exception.AsError(new HttpException(SR.Hosting_BuildProviderInvalidValueForNonNegativeIntegerAttribute(value, name)));
                }

                // Make sure it's not negative
                //
                if (ret < 0)
                {
                    throw FxTrace.Exception.AsError(new HttpException(SR.Hosting_BuildProviderInvalidValueForNonNegativeIntegerAttribute(value, name)));
                }

                return ret;
            }

            internal static bool IsWhiteSpaceString(string s)
            {
                return (s.Trim().Length == 0);
            }

            /// <summary>
            /// This method takes the code that will be compiled as a string and it 
            /// will count how many lines exist between the given offset and the final
            /// offset. 
            /// </summary>
            /// <param name="text">The text that contains the source code</param>
            /// <param name="offset">Starting offset for lookup</param>
            /// <param name="newoffset">Ending offset</param>
            /// <returns>The number of lines</returns>
            internal static int LineCount(string text, int offset, int newoffset)
            {
                int linecount = 0;

                while (offset < newoffset)
                {
                    if (text[offset] == '\r' || (text[offset] == '\n' && (offset == 0 || text[offset - 1] != '\r')))
                        linecount++;

                    offset++;
                }

                return linecount;
            }

            /// <summary>
            /// Parses a string that contains a type trying to figure out if the assembly info is there.
            /// </summary>
            /// <param name="typeName">The string to search</param>
            internal static bool TypeNameIncludesAssembly(string typeName)
            {
                return (typeName.IndexOf(",", StringComparison.Ordinal) >= 0);
            }

            /// <summary>
            /// Loops through a list of assemblies that are already collected by the parser/provider and 
            /// looks for the specified type.
            /// </summary>
            /// <param name="assemblies">The collection of assemblies</param>
            /// <param name="typeName">The type name</param>
            /// <param name="ignoreCase">Case sensitivity knob</param>
            /// <returns></returns>
            internal static Type GetTypeFromAssemblies(ICollection assemblies, string typeName, bool ignoreCase)
            {
                if (assemblies == null)
                    return null;

                Type type = null;

                foreach (Assembly assembly in assemblies)
                {
                    Type t = assembly.GetType(typeName, false /*throwOnError*/, ignoreCase);

                    if (t == null)
                        continue;

                    // If we had already found a different one, it's an ambiguous type reference
                    //
                    if (type != null && t != type)
                    {
                        throw FxTrace.Exception.AsError(new HttpException(
                            SR.Hosting_BuildProviderAmbiguousType(typeName, type.Assembly.FullName, t.Assembly.FullName)));
                    }

                    // Keep track of it
                    //
                    type = t;
                }

                return type;
            }
        }
    }
}