public UriTemplateDispatchFormatter(OperationDescription operationDescription, IDispatchMessageFormatter inner, QueryStringConverter qsc, string contractName, Uri baseAddress) { this.inner = inner; this.qsc = qsc; this.baseAddress = baseAddress; this.operationName = operationDescription.Name; UriTemplateClientFormatter.Populate(out this.pathMapping, out this.queryMapping, out this.totalNumUTVars, out this.uriTemplate, operationDescription, qsc, contractName); }
protected virtual IClientMessageFormatter GetRequestClientFormatter(OperationDescription operationDescription, ServiceEndpoint endpoint) { WebMessageFormat requestFormat = GetRequestFormat(operationDescription); bool useJson = (requestFormat == WebMessageFormat.Json); WebMessageEncodingBindingElement webEncoding = (useJson) ? endpoint.Binding.CreateBindingElements().Find<WebMessageEncodingBindingElement>() : null; IClientMessageFormatter innerFormatter = null; // get some validation errors by creating "throwAway" formatter // validate that endpoint.Address is not null before accessing the endpoint.Address.Uri. This is to avoid throwing a NullRefException while constructing a UriTemplateClientFormatter if (endpoint.Address == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( SR2.GetString(SR2.ServiceEndpointMustHaveNonNullAddress, typeof(ServiceEndpoint), typeof(ChannelFactory), typeof(WebHttpEndpoint), AddressPropertyName, typeof(ServiceEndpoint)))); } UriTemplateClientFormatter throwAway = new UriTemplateClientFormatter(operationDescription, null, GetQueryStringConverter(operationDescription), endpoint.Address.Uri, false, endpoint.Contract.Name); int numUriVariables = throwAway.pathMapping.Count + throwAway.queryMapping.Count; bool isStream = false; HideReplyMessage(operationDescription, delegate() { WebMessageBodyStyle style = GetBodyStyle(operationDescription); bool isUntypedWhenUriParamsNotConsidered = false; Effect doBodyFormatter = delegate() { if (numUriVariables != 0) { EnsureNotUntypedMessageNorMessageContract(operationDescription); } // get body formatter ValidateBodyParameters(operationDescription, true); IClientMessageFormatter baseFormatter; Type parameterType; if (TryGetStreamParameterType(operationDescription.Messages[0], operationDescription, true, out parameterType)) { isStream = true; baseFormatter = new HttpStreamFormatter(operationDescription); } else if (UseBareRequestFormatter(style, operationDescription, out parameterType)) { baseFormatter = SingleBodyParameterMessageFormatter.CreateClientFormatter(operationDescription, parameterType, true, useJson, this.xmlSerializerManager); } else { baseFormatter = GetDefaultClientFormatter(operationDescription, useJson, !IsBareRequest(style)); } innerFormatter = baseFormatter; isUntypedWhenUriParamsNotConsidered = IsUntypedMessage(operationDescription.Messages[0]); }; if (numUriVariables == 0) { if (IsUntypedMessage(operationDescription.Messages[0])) { ValidateBodyParameters(operationDescription, true); innerFormatter = new MessagePassthroughFormatter(); isUntypedWhenUriParamsNotConsidered = true; } else if (IsTypedMessage(operationDescription.Messages[0])) { ValidateBodyParameters(operationDescription, true); innerFormatter = GetDefaultClientFormatter(operationDescription, useJson, !IsBareRequest(style)); } else { doBodyFormatter(); } } else { HideRequestUriTemplateParameters(operationDescription, throwAway, delegate() { CloneMessageDescriptionsBeforeActing(operationDescription, delegate() { doBodyFormatter(); }); }); } innerFormatter = new UriTemplateClientFormatter(operationDescription, innerFormatter, GetQueryStringConverter(operationDescription), endpoint.Address.Uri, isUntypedWhenUriParamsNotConsidered, endpoint.Contract.Name); }); string defaultContentType = GetDefaultContentType(isStream, useJson, webEncoding); if (!string.IsNullOrEmpty(defaultContentType)) { innerFormatter = new ContentTypeSettingClientMessageFormatter(defaultContentType, innerFormatter); } return innerFormatter; }
static void HideRequestUriTemplateParameters(OperationDescription operationDescription, UriTemplateClientFormatter throwAway, Effect effect) { HideRequestUriTemplateParameters(operationDescription, throwAway.pathMapping, throwAway.queryMapping, effect); }
public WebHttpDispatchOperationSelector(ServiceEndpoint endpoint) { if (endpoint == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("endpoint"); } if (endpoint.Address == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( SR2.GetString(SR2.EndpointAddressCannotBeNull))); } #pragma warning disable 56506 // [....], endpoint.Address.Uri is never null Uri baseUri = endpoint.Address.Uri; this.methodSpecificTables = new Dictionary <string, UriTemplateTable>(); this.templates = new Dictionary <string, UriTemplate>(); #pragma warning restore 56506 WebHttpBehavior webHttpBehavior = endpoint.Behaviors.Find <WebHttpBehavior>(); if (webHttpBehavior != null && webHttpBehavior.HelpEnabled) { this.helpUriTable = new UriTemplateTable(endpoint.ListenUri, HelpPage.GetOperationTemplatePairs()); } Dictionary <WCFKey, string> alreadyHaves = new Dictionary <WCFKey, string>(); #pragma warning disable 56506 // [....], endpoint.Contract is never null foreach (OperationDescription od in endpoint.Contract.Operations) #pragma warning restore 56506 { // ignore callback operations if (od.Messages[0].Direction == MessageDirection.Input) { string method = WebHttpBehavior.GetWebMethod(od); string path = UriTemplateClientFormatter.GetUTStringOrDefault(od); // if (UriTemplateHelpers.IsWildcardPath(path) && (method == WebHttpBehavior.WildcardMethod)) { if (this.catchAllOperationName != "") { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new InvalidOperationException( SR2.GetString(SR2.MultipleOperationsInContractWithPathMethod, endpoint.Contract.Name, path, method))); } this.catchAllOperationName = od.Name; } UriTemplate ut = new UriTemplate(path); WCFKey wcfKey = new WCFKey(ut, method); if (alreadyHaves.ContainsKey(wcfKey)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new InvalidOperationException( SR2.GetString(SR2.MultipleOperationsInContractWithPathMethod, endpoint.Contract.Name, path, method))); } alreadyHaves.Add(wcfKey, od.Name); UriTemplateTable methodSpecificTable; if (!methodSpecificTables.TryGetValue(method, out methodSpecificTable)) { methodSpecificTable = new UriTemplateTable(baseUri); methodSpecificTables.Add(method, methodSpecificTable); } methodSpecificTable.KeyValuePairs.Add(new KeyValuePair <UriTemplate, object>(ut, od.Name)); this.templates.Add(od.Name, ut); } } if (this.methodSpecificTables.Count == 0) { this.methodSpecificTables = null; } else { // freeze all the tables because they should not be modified after this point foreach (UriTemplateTable table in this.methodSpecificTables.Values) { table.MakeReadOnly(true /* allowDuplicateEquivalentUriTemplates */); } if (!methodSpecificTables.TryGetValue(WebHttpBehavior.WildcardMethod, out wildcardTable)) { wildcardTable = null; } } }