|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.ServiceModel; |
| 5 | +using System.ServiceModel.Activation; |
| 6 | +using System.ServiceModel.Channels; |
| 7 | +using System.ServiceModel.Description; |
| 8 | +using System.ServiceModel.Web; |
| 9 | +using System.Text; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +namespace CorsEnabledService |
| 13 | +{ |
| 14 | + public class CorsEnabledServiceHostFactory : ServiceHostFactory |
| 15 | + { |
| 16 | + protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) |
| 17 | + { |
| 18 | + return new CorsEnabledServiceHost(serviceType, baseAddresses); |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + public class CorsEnabledServiceHost : ServiceHost |
| 23 | + { |
| 24 | + Type contractType; |
| 25 | + public CorsEnabledServiceHost(Type serviceType, params Uri[] baseAddresses) |
| 26 | + : base(serviceType, baseAddresses) |
| 27 | + { |
| 28 | + this.contractType = GetContractType(serviceType); |
| 29 | + } |
| 30 | + |
| 31 | + protected override void OnOpening() |
| 32 | + { |
| 33 | + ServiceEndpoint endpoint = this.AddServiceEndpoint(this.contractType, new WebHttpBinding(), ""); |
| 34 | + |
| 35 | + List<OperationDescription> corsEnabledOperations = endpoint.Contract.Operations |
| 36 | + .Where(o => o.Behaviors.Find<CorsEnabledAttribute>() != null) |
| 37 | + .ToList(); |
| 38 | + |
| 39 | + AddPreflightOperations(endpoint, corsEnabledOperations); |
| 40 | + |
| 41 | + var webHttpBheavior = new WebHttpBehavior(); |
| 42 | + webHttpBheavior.HelpEnabled = true; |
| 43 | + endpoint.Behaviors.Add(webHttpBheavior); |
| 44 | + endpoint.Behaviors.Add(new EnableCorsEndpointBehavior()); |
| 45 | + |
| 46 | + base.OnOpening(); |
| 47 | + } |
| 48 | + |
| 49 | + private Type GetContractType(Type serviceType) |
| 50 | + { |
| 51 | + if (HasServiceContract(serviceType)) |
| 52 | + { |
| 53 | + return serviceType; |
| 54 | + } |
| 55 | + |
| 56 | + Type[] possibleContractTypes = serviceType.GetInterfaces() |
| 57 | + .Where(i => HasServiceContract(i)) |
| 58 | + .ToArray(); |
| 59 | + |
| 60 | + switch (possibleContractTypes.Length) |
| 61 | + { |
| 62 | + case 0: |
| 63 | + throw new InvalidOperationException("Service type " + serviceType.FullName + " does not implement any interface decorated with the ServiceContractAttribute."); |
| 64 | + case 1: |
| 65 | + return possibleContractTypes[0]; |
| 66 | + default: |
| 67 | + throw new InvalidOperationException("Service type " + serviceType.FullName + " implements multiple interfaces decorated with the ServiceContractAttribute, not supported by this factory."); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private static bool HasServiceContract(Type type) |
| 72 | + { |
| 73 | + return Attribute.IsDefined(type, typeof(ServiceContractAttribute), false); |
| 74 | + } |
| 75 | + |
| 76 | + private void AddPreflightOperations(ServiceEndpoint endpoint, List<OperationDescription> corsOperations) |
| 77 | + { |
| 78 | + Dictionary<string, PreflightOperationBehavior> uriTemplates = new Dictionary<string, PreflightOperationBehavior>(StringComparer.OrdinalIgnoreCase); |
| 79 | + |
| 80 | + foreach (var operation in corsOperations) |
| 81 | + { |
| 82 | + if (operation.Behaviors.Find<WebGetAttribute>() != null) |
| 83 | + { |
| 84 | + // no need to add preflight operation for GET requests |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + if (operation.IsOneWay) |
| 89 | + { |
| 90 | + // no support for 1-way messages |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + string originalUriTemplate; |
| 95 | + WebInvokeAttribute originalWia = operation.Behaviors.Find<WebInvokeAttribute>(); |
| 96 | + |
| 97 | + if (originalWia != null && originalWia.UriTemplate != null) |
| 98 | + { |
| 99 | + originalUriTemplate = NormalizeTemplate(originalWia.UriTemplate); |
| 100 | + } |
| 101 | + else |
| 102 | + { |
| 103 | + originalUriTemplate = operation.Name; |
| 104 | + } |
| 105 | + |
| 106 | + string originalMethod = originalWia != null && originalWia.Method != null ? originalWia.Method : "POST"; |
| 107 | + |
| 108 | + if (uriTemplates.ContainsKey(originalUriTemplate)) |
| 109 | + { |
| 110 | + // there is already an OPTIONS operation for this URI, we can reuse it |
| 111 | + PreflightOperationBehavior operationBehavior = uriTemplates[originalUriTemplate]; |
| 112 | + operationBehavior.AddAllowedMethod(originalMethod); |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + ContractDescription contract = operation.DeclaringContract; |
| 117 | + OperationDescription preflightOperation = new OperationDescription(operation.Name + CorsConstants.PreflightSuffix, contract); |
| 118 | + MessageDescription inputMessage = new MessageDescription(operation.Messages[0].Action + CorsConstants.PreflightSuffix, MessageDirection.Input); |
| 119 | + inputMessage.Body.Parts.Add(new MessagePartDescription("input", contract.Namespace) { Index = 0, Type = typeof(Message) }); |
| 120 | + preflightOperation.Messages.Add(inputMessage); |
| 121 | + MessageDescription outputMessage = new MessageDescription(operation.Messages[1].Action + CorsConstants.PreflightSuffix, MessageDirection.Output); |
| 122 | + outputMessage.Body.ReturnValue = new MessagePartDescription(preflightOperation.Name + "Return", contract.Namespace) { Type = typeof(Message) }; |
| 123 | + preflightOperation.Messages.Add(outputMessage); |
| 124 | + |
| 125 | + WebInvokeAttribute wia = new WebInvokeAttribute(); |
| 126 | + wia.UriTemplate = originalUriTemplate; |
| 127 | + wia.Method = "OPTIONS"; |
| 128 | + |
| 129 | + preflightOperation.Behaviors.Add(wia); |
| 130 | + preflightOperation.Behaviors.Add(new DataContractSerializerOperationBehavior(preflightOperation)); |
| 131 | + PreflightOperationBehavior preflightOperationBehavior = new PreflightOperationBehavior(preflightOperation); |
| 132 | + preflightOperationBehavior.AddAllowedMethod(originalMethod); |
| 133 | + preflightOperation.Behaviors.Add(preflightOperationBehavior); |
| 134 | + uriTemplates.Add(originalUriTemplate, preflightOperationBehavior); |
| 135 | + |
| 136 | + contract.Operations.Add(preflightOperation); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private string NormalizeTemplate(string uriTemplate) |
| 142 | + { |
| 143 | + int queryIndex = uriTemplate.IndexOf('?'); |
| 144 | + if (queryIndex >= 0) |
| 145 | + { |
| 146 | + // no query string used for this |
| 147 | + uriTemplate = uriTemplate.Substring(0, queryIndex); |
| 148 | + } |
| 149 | + |
| 150 | + int paramIndex; |
| 151 | + while ((paramIndex = uriTemplate.IndexOf('{')) >= 0) |
| 152 | + { |
| 153 | + // Replacing all named parameters with wildcards |
| 154 | + int endParamIndex = uriTemplate.IndexOf('}', paramIndex); |
| 155 | + if (endParamIndex >= 0) |
| 156 | + { |
| 157 | + uriTemplate = uriTemplate.Substring(0, paramIndex) + '*' + uriTemplate.Substring(endParamIndex + 1); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + return uriTemplate; |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments