File: ApplyHostConfigurationBehavior.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 (127 lines) | stat: -rw-r--r-- 5,290 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
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.Activation
{
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ServiceModel;
    using System.ServiceModel.Description;
    using System.ServiceModel.Dispatcher;
    using System.ServiceModel.Channels;

    //This is the behavior is intended to auto apply IIS/AspNet configuration to WCF post design time
    class ApplyHostConfigurationBehavior : IServiceBehavior
    {
        internal ApplyHostConfigurationBehavior()
        {
        }

        void IServiceBehavior.Validate(ServiceDescription description, ServiceHostBase service)
        {
            if (service.Description.Endpoints != null && ServiceHostingEnvironment.MultipleSiteBindingsEnabled)
            {
                FailActivationIfEndpointsHaveAbsoluteAddress(service);
            }
        }

        void IServiceBehavior.AddBindingParameters(ServiceDescription description, ServiceHostBase service, Collection<ServiceEndpoint> endpoints, BindingParameterCollection parameters)
        {
        }

        void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase service)
        {
            if (ServiceHostingEnvironment.MultipleSiteBindingsEnabled)
            {
                SetEndpointAddressFilterToIgnorePort(service);
            }
        }

        void SetEndpointAddressFilterToIgnorePort(ServiceHostBase service)
        {
            for (int i = 0; i < service.ChannelDispatchers.Count; i++)
            {
                ChannelDispatcher channelDispatcher = service.ChannelDispatchers[i] as ChannelDispatcher;
                if (channelDispatcher != null)
                {
                    if (IsSchemeHttpOrHttps(channelDispatcher.Listener.Uri.Scheme))
                    {
                        for (int j = 0; j < channelDispatcher.Endpoints.Count; j++)
                        {
                            EndpointDispatcher endpointDispatcher = channelDispatcher.Endpoints[j];
                            EndpointAddressMessageFilter endpointAddressMessageFilter = endpointDispatcher.AddressFilter as EndpointAddressMessageFilter;
                            if (endpointAddressMessageFilter != null)
                            {
                                endpointAddressMessageFilter.ComparePort = false;
                            }
                        }
                    }
                }
            }
        }

        void FailActivationIfEndpointsHaveAbsoluteAddress(ServiceHostBase service)
        {
            foreach (ServiceEndpoint endpoint in service.Description.Endpoints)
            {
                if (IsSchemeHttpOrHttps(endpoint.Binding.Scheme))
                {
                    if (endpoint.UnresolvedListenUri != null)
                    {
                        ThrowIfAbsolute(endpoint.UnresolvedListenUri);
                    }
                    else
                    {
                        //If the listen URI is not null, we shouldn't care about address. Because there are 
                        //customers who have following config (for load balancer scenarios) - Note ExtraFolder and https
                        //listen URI - http://localhost/App1/x.svc
                        //Address -    https://externalhost/ExtranFolder/App1/x.svc
                        ThrowIfAbsolute(endpoint.UnresolvedAddress);
                    }
                }
            }

            ServiceDebugBehavior debugBehavior = service.Description.Behaviors.Find<ServiceDebugBehavior>();
            if (debugBehavior != null)
            {
                if (debugBehavior.HttpHelpPageEnabled)
                {
                    ThrowIfAbsolute(debugBehavior.HttpHelpPageUrl);
                }
                if (debugBehavior.HttpsHelpPageEnabled)
                {
                    ThrowIfAbsolute(debugBehavior.HttpsHelpPageUrl);
                }
            }

            ServiceMetadataBehavior metadataBehavior = service.Description.Behaviors.Find<ServiceMetadataBehavior>();
            if (metadataBehavior != null)
            {
                if (metadataBehavior.HttpGetEnabled)
                {
                    ThrowIfAbsolute(metadataBehavior.HttpGetUrl);
                }
                if (metadataBehavior.HttpsGetEnabled)
                {
                    ThrowIfAbsolute(metadataBehavior.HttpsGetUrl);
                }
            }
        }

        static void ThrowIfAbsolute(Uri uri)
        {
            if (uri != null && uri.IsAbsoluteUri)
            {
                throw FxTrace.Exception.AsError(new InvalidOperationException(SR.Hosting_SharedEndpointRequiresRelativeEndpoint(uri.ToString())));
            }
        }

        static bool IsSchemeHttpOrHttps(string scheme)
        {
            return string.Compare(scheme, Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase) == 0 ||
                   string.Compare(scheme, Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase) == 0;
        }
    }
}