private static PreflightOperationBehavior RegisterPreflightOperation(OperationDescription operation) { OperationDescription preflightOperation = CreatePreflightOperation(operation); preflightOperation.Behaviors.Add(new DataContractSerializerOperationBehavior(preflightOperation)); string originalUriTemplate = GetPreflightUriTemplate(operation); var wia = new WebInvokeAttribute { UriTemplate = originalUriTemplate, Method = "OPTIONS" }; preflightOperation.Behaviors.Add(wia); string originalMethod = GetHttpMethod(operation); var preflightOperationBehavior = new PreflightOperationBehavior(); preflightOperationBehavior.AddAllowedMethod(originalMethod); preflightOperation.Behaviors.Add(preflightOperationBehavior); operation.DeclaringContract.Operations.Add(preflightOperation); return(preflightOperationBehavior); }
private static void RegisterPreflightOperations(ServiceEndpoint endpoint) { List <OperationDescription> corsEnabledOperations = endpoint.Contract.Operations .Where(x => x.Behaviors.Find <CorsEnabledAttribute>() != null) .ToList(); var uriTemplates = new Dictionary <string, PreflightOperationBehavior>(StringComparer.OrdinalIgnoreCase); foreach (OperationDescription operation in corsEnabledOperations) { if (operation.Behaviors.Find <WebGetAttribute>() != null) { // no need to add preflight operation for GET requests continue; } if (operation.IsOneWay) { // no support for 1-way messages continue; } string originalUriTemplate = GetPreflightUriTemplate(operation); string originalMethod = GetHttpMethod(operation); if (uriTemplates.ContainsKey(originalUriTemplate)) { // there is already an OPTIONS operation for this URI, we can reuse it PreflightOperationBehavior operationBehavior = uriTemplates[originalUriTemplate]; operationBehavior.AddAllowedMethod(originalMethod); } else { PreflightOperationBehavior preflightOperationBehavior = RegisterPreflightOperation(operation); uriTemplates.Add(originalUriTemplate, preflightOperationBehavior); } } }
private static PreflightOperationBehavior RegisterPreflightOperation(OperationDescription operation) { OperationDescription preflightOperation = CreatePreflightOperation(operation); preflightOperation.Behaviors.Add(new DataContractSerializerOperationBehavior(preflightOperation)); string originalUriTemplate = GetPreflightUriTemplate(operation); var wia = new WebInvokeAttribute { UriTemplate = originalUriTemplate, Method = "OPTIONS" }; preflightOperation.Behaviors.Add(wia); string originalMethod = GetHttpMethod(operation); var preflightOperationBehavior = new PreflightOperationBehavior(); preflightOperationBehavior.AddAllowedMethod(originalMethod); preflightOperation.Behaviors.Add(preflightOperationBehavior); operation.DeclaringContract.Operations.Add(preflightOperation); return preflightOperationBehavior; }