Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx35 / System.ServiceModel.Web / System / ServiceModel / Dispatcher / UriTemplateClientFormatter.cs / 1305376 / UriTemplateClientFormatter.cs
//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- #pragma warning disable 1634, 1691 namespace System.ServiceModel.Dispatcher { using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Globalization; using System.Reflection; using System.ServiceModel; using System.ServiceModel.Channels; using System.ServiceModel.Description; using System.Text; using System.Xml; using System.ServiceModel.Web; class UriTemplateClientFormatter : IClientMessageFormatter { internal DictionarypathMapping; internal Dictionary > queryMapping; Uri baseUri; IClientMessageFormatter inner; bool innerIsUntypedMessage; bool isGet; string method; QueryStringConverter qsc; int totalNumUTVars; UriTemplate uriTemplate; public UriTemplateClientFormatter(OperationDescription operationDescription, IClientMessageFormatter inner, QueryStringConverter qsc, Uri baseUri, bool innerIsUntypedMessage, string contractName) { this.inner = inner; this.qsc = qsc; this.baseUri = baseUri; this.innerIsUntypedMessage = innerIsUntypedMessage; Populate(out this.pathMapping, out this.queryMapping, out this.totalNumUTVars, out this.uriTemplate, operationDescription, qsc, contractName); this.method = WebHttpBehavior.GetWebMethod(operationDescription); isGet = this.method == WebHttpBehavior.GET; } public object DeserializeReply(Message message, object[] parameters) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR2.GetString(SR2.QueryStringFormatterOperationNotSupportedClientSide))); } public Message SerializeRequest(MessageVersion messageVersion, object[] parameters) { object[] innerParameters = new object[parameters.Length - this.totalNumUTVars]; NameValueCollection nvc = new NameValueCollection(); int j = 0; for (int i = 0; i < parameters.Length; ++i) { if (this.pathMapping.ContainsKey(i)) { nvc[this.pathMapping[i]] = parameters[i] as string; } else if (this.queryMapping.ContainsKey(i)) { if (parameters[i] != null) { nvc[this.queryMapping[i].Key] = this.qsc.ConvertValueToString(parameters[i], this.queryMapping[i].Value); } } else { innerParameters[j] = parameters[i]; ++j; } } Message m = inner.SerializeRequest(messageVersion, innerParameters); bool userSetTheToOnMessage = (this.innerIsUntypedMessage && m.Headers.To != null); bool userSetTheToOnOutgoingHeaders = (OperationContext.Current != null && OperationContext.Current.OutgoingMessageHeaders.To != null); if (!userSetTheToOnMessage && !userSetTheToOnOutgoingHeaders) { m.Headers.To = this.uriTemplate.BindByName(this.baseUri, nvc); } if (WebOperationContext.Current != null) { if (isGet) { WebOperationContext.Current.OutgoingRequest.SuppressEntityBody = true; } if (this.method != WebHttpBehavior.WildcardMethod && WebOperationContext.Current.OutgoingRequest.Method != null) { WebOperationContext.Current.OutgoingRequest.Method = this.method; } } else { HttpRequestMessageProperty hrmp; if (m.Properties.ContainsKey(HttpRequestMessageProperty.Name)) { hrmp = m.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty; } else { hrmp = new HttpRequestMessageProperty(); m.Properties.Add(HttpRequestMessageProperty.Name, hrmp); } if (isGet) { hrmp.SuppressEntityBody = true; } if (this.method != WebHttpBehavior.WildcardMethod) { hrmp.Method = this.method; } } return m; } internal static string GetUTStringOrDefault(OperationDescription operationDescription) { string utString = WebHttpBehavior.GetWebUriTemplate(operationDescription); if (utString == null && WebHttpBehavior.GetWebMethod(operationDescription) == WebHttpBehavior.GET) { utString = MakeDefaultGetUTString(operationDescription); } if (utString == null) { utString = operationDescription.Name; // note: not + "/*", see 8988 and 9653 } return utString; } internal static void Populate(out Dictionary pathMapping, out Dictionary > queryMapping, out int totalNumUTVars, out UriTemplate uriTemplate, OperationDescription operationDescription, QueryStringConverter qsc, string contractName) { pathMapping = new Dictionary (); queryMapping = new Dictionary >(); string utString = GetUTStringOrDefault(operationDescription); uriTemplate = new UriTemplate(utString); List neededPathVars = new List (uriTemplate.PathSegmentVariableNames); List neededQueryVars = new List (uriTemplate.QueryValueVariableNames); Dictionary alreadyGotVars = new Dictionary (StringComparer.OrdinalIgnoreCase); totalNumUTVars = neededPathVars.Count + neededQueryVars.Count; for (int i = 0; i < operationDescription.Messages[0].Body.Parts.Count; ++i) { MessagePartDescription mpd = operationDescription.Messages[0].Body.Parts[i]; string parameterName = mpd.XmlName.DecodedName; if (alreadyGotVars.ContainsKey(parameterName)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( SR2.GetString(SR2.UriTemplateVarCaseDistinction, operationDescription.XmlName.DecodedName, contractName, parameterName))); } List neededPathCopy = new List (neededPathVars); foreach (string pathVar in neededPathCopy) { if (string.Compare(parameterName, pathVar, StringComparison.OrdinalIgnoreCase) == 0) { if (mpd.Type != typeof(string)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( SR2.GetString(SR2.UriTemplatePathVarMustBeString, operationDescription.XmlName.DecodedName, contractName, parameterName))); } pathMapping.Add(i, parameterName); alreadyGotVars.Add(parameterName, 0); neededPathVars.Remove(pathVar); } } List neededQueryCopy = new List (neededQueryVars); foreach (string queryVar in neededQueryCopy) { if (string.Compare(parameterName, queryVar, StringComparison.OrdinalIgnoreCase) == 0) { if (!qsc.CanConvert(mpd.Type)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( SR2.GetString(SR2.UriTemplateQueryVarMustBeConvertible, operationDescription.XmlName.DecodedName, contractName, parameterName, mpd.Type, qsc.GetType().Name))); } queryMapping.Add(i, new KeyValuePair (parameterName, mpd.Type)); alreadyGotVars.Add(parameterName, 0); neededQueryVars.Remove(queryVar); } } } if (neededPathVars.Count != 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.GetString( SR2.UriTemplateMissingVar, operationDescription.XmlName.DecodedName, contractName, neededPathVars[0]))); } if (neededQueryVars.Count != 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.GetString (SR2.UriTemplateMissingVar, operationDescription.XmlName.DecodedName, contractName, neededQueryVars[0]))); } } static string MakeDefaultGetUTString(OperationDescription od) { StringBuilder sb = new StringBuilder(od.XmlName.DecodedName); //sb.Append("/*"); // note: not + "/*", see 8988 and 9653 if (!WebHttpBehavior.IsUntypedMessage(od.Messages[0])) { sb.Append("?"); foreach (MessagePartDescription mpd in od.Messages[0].Body.Parts) { string parameterName = mpd.XmlName.DecodedName; sb.Append(parameterName); sb.Append("={"); sb.Append(parameterName); sb.Append("}&"); } sb.Remove(sb.Length - 1, 1); } return sb.ToString(); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- BaseAsyncResult.cs
- keycontainerpermission.cs
- BidirectionalDictionary.cs
- SafeRightsManagementSessionHandle.cs
- Merger.cs
- CultureSpecificStringDictionary.cs
- XmlSerializableServices.cs
- StringPropertyBuilder.cs
- FormsAuthenticationEventArgs.cs
- CompilerCollection.cs
- PanelStyle.cs
- DisplayMemberTemplateSelector.cs
- DataGridTextBoxColumn.cs
- PackageDigitalSignature.cs
- ActiveXHelper.cs
- baseaxisquery.cs
- TransactionProtocol.cs
- AnnotationObservableCollection.cs
- ReachPageContentSerializer.cs
- UnlockInstanceCommand.cs
- Vector3DKeyFrameCollection.cs
- CurrencyWrapper.cs
- PassportAuthenticationEventArgs.cs
- BaseTemplateParser.cs
- XmlILModule.cs
- NullableConverter.cs
- DataReaderContainer.cs
- RuntimeWrappedException.cs
- SuppressMessageAttribute.cs
- PieceNameHelper.cs
- PointLightBase.cs
- CookielessHelper.cs
- securitymgrsite.cs
- PointCollectionConverter.cs
- InvalidFilterCriteriaException.cs
- HtmlToClrEventProxy.cs
- ColorContext.cs
- XmlWriterSettings.cs
- RSACryptoServiceProvider.cs
- SortableBindingList.cs
- OleDbSchemaGuid.cs
- ConcurrentStack.cs
- Int32Collection.cs
- ContentIterators.cs
- XmlDataSource.cs
- XNameConverter.cs
- __ComObject.cs
- QilDataSource.cs
- RightsManagementEncryptedStream.cs
- XPathNavigatorException.cs
- EventMappingSettingsCollection.cs
- ClientFormsAuthenticationCredentials.cs
- SignatureHelper.cs
- StyleModeStack.cs
- ElementInit.cs
- SafeRightsManagementHandle.cs
- TextDecorationUnitValidation.cs
- TrustManagerPromptUI.cs
- Viewport2DVisual3D.cs
- SortedSetDebugView.cs
- CompilerError.cs
- ThicknessAnimationBase.cs
- MetadataCache.cs
- ImportedNamespaceContextItem.cs
- PointUtil.cs
- ColorConverter.cs
- BindUriHelper.cs
- PeerNameRegistration.cs
- LoadedOrUnloadedOperation.cs
- CreateDataSourceDialog.cs
- DebugView.cs
- AppDomain.cs
- FirstMatchCodeGroup.cs
- XmlIgnoreAttribute.cs
- GridErrorDlg.cs
- MethodImplAttribute.cs
- MultilineStringConverter.cs
- XmlSchemas.cs
- CodeGeneratorOptions.cs
- EncryptedKey.cs
- DataGridViewSelectedCellCollection.cs
- TextEncodedRawTextWriter.cs
- EntityParameter.cs
- StateElement.cs
- LineGeometry.cs
- SamlAudienceRestrictionCondition.cs
- ShaderEffect.cs
- CompilerCollection.cs
- ThreadStartException.cs
- LocalFileSettingsProvider.cs
- StopStoryboard.cs
- VersionPair.cs
- ConfigXmlWhitespace.cs
- PropertyTab.cs
- AsymmetricKeyExchangeDeformatter.cs
- ArgumentElement.cs
- SplitterCancelEvent.cs
- DocumentOrderQuery.cs
- ResourceSetExpression.cs
- MetadataPropertyCollection.cs