File: ComPlusServiceLoader.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 (120 lines) | stat: -rw-r--r-- 5,279 bytes parent folder | download | duplicates (9)
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
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.ComIntegration
{
    using System;
    using System.ServiceModel.Description;
    using System.ServiceModel;
    using System.ServiceModel.Activation;
    using System.Diagnostics;
    using System.ServiceModel.Diagnostics;

    class ComPlusServiceLoader
    {
        ServiceInfo info;
        ConfigLoader configLoader;
        ComPlusTypeLoader typeLoader;

        public ComPlusServiceLoader (ServiceInfo info)
        {
            this.info = info;
            this.typeLoader = new ComPlusTypeLoader(info);
            this.configLoader = new ConfigLoader(typeLoader);
        }
        
        public ServiceDescription Load(ServiceHostBase host)
        {
            ServiceDescription service = new ServiceDescription(this.info.ServiceName);

            // ServiceBehaviorAttribute needs to go first in the behaviors collection (before config stuff)
            AddBehaviors(service);

            this.configLoader.LoadServiceDescription(host, service, this.info.ServiceElement, host.LoadConfigurationSectionHelper);

            ValidateConfigInstanceSettings(service);

            ComPlusServiceHostTrace.Trace(TraceEventType.Information, TraceCode.ComIntegrationServiceHostCreatedServiceEndpoint,
                                SR.TraceCodeComIntegrationServiceHostCreatedServiceEndpoint,  this.info, service.Endpoints);            

            return service;
        }


        void AddBehaviors(ServiceDescription service)
        {
            // The philosophy here is to respect settings from configuration
            // At the moment, none of the settings we care about can be modified
            // through configuration. That may change in the future.
            // However, we never want to silently overwrite a user's configuration.
            // So we should either accept overrides or reject them, but never 
            // silently update them.
            //

            ServiceBehaviorAttribute serviceBehavior = EnsureBehaviorAttribute(service);

            serviceBehavior.InstanceProvider = new ComPlusInstanceProvider(this.info);

            serviceBehavior.InstanceContextMode = InstanceContextMode.Single;

            // SHOULD: There is no reason to not allow concurrency at this level
            serviceBehavior.ConcurrencyMode = ConcurrencyMode.Multiple;
            serviceBehavior.UseSynchronizationContext = false;

            service.Behaviors.Add(new SecurityCookieModeValidator());

            if (AspNetEnvironment.Enabled)
            {
                AspNetCompatibilityRequirementsAttribute aspNetCompatibilityRequirements = service.Behaviors.Find<AspNetCompatibilityRequirementsAttribute>();
                if (aspNetCompatibilityRequirements == null)
                {
                    aspNetCompatibilityRequirements = new AspNetCompatibilityRequirementsAttribute();
                    service.Behaviors.Add(aspNetCompatibilityRequirements);
                }
            }
        }

        ServiceBehaviorAttribute EnsureBehaviorAttribute(ServiceDescription service)
        {
            ServiceBehaviorAttribute serviceBehavior;
            if (service.Behaviors.Contains(typeof(ServiceBehaviorAttribute)))
            {
                serviceBehavior = (ServiceBehaviorAttribute)service.Behaviors[typeof(ServiceBehaviorAttribute)];
            }
            else
            {
                serviceBehavior = new ServiceBehaviorAttribute();
                service.Behaviors.Insert(0, serviceBehavior);
            }
            return serviceBehavior;
        }

        void ValidateConfigInstanceSettings(ServiceDescription service)
        {
            ServiceBehaviorAttribute serviceBehavior = EnsureBehaviorAttribute(service);

            foreach (ServiceEndpoint endpoint in service.Endpoints)
            {
                if (endpoint != null && !endpoint.InternalIsSystemEndpoint(service))
                {
                    if (endpoint.Contract.SessionMode == SessionMode.Required)
                    {
                        if (serviceBehavior.InstanceContextMode == InstanceContextMode.PerCall)
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(System.ServiceModel.ComIntegration.Error.InconsistentSessionRequirements());

                        serviceBehavior.InstanceContextMode = InstanceContextMode.PerSession;
                    }
                    else
                    {
                        if (serviceBehavior.InstanceContextMode == InstanceContextMode.PerSession)
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(System.ServiceModel.ComIntegration.Error.InconsistentSessionRequirements());

                        serviceBehavior.InstanceContextMode = InstanceContextMode.PerCall;
                    }
                }
            }
            if (serviceBehavior.InstanceContextMode == InstanceContextMode.Single)
                serviceBehavior.InstanceContextMode = InstanceContextMode.PerSession;
        }
    }
}