当前位置: 首页>>代码示例>>C#>>正文


C# OperationDescription.ToHttpOperationDescription方法代码示例

本文整理汇总了C#中System.ServiceModel.Description.OperationDescription.ToHttpOperationDescription方法的典型用法代码示例。如果您正苦于以下问题:C# OperationDescription.ToHttpOperationDescription方法的具体用法?C# OperationDescription.ToHttpOperationDescription怎么用?C# OperationDescription.ToHttpOperationDescription使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.ServiceModel.Description.OperationDescription的用法示例。


在下文中一共展示了OperationDescription.ToHttpOperationDescription方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetEquivalentOperationDescription

        public static OperationDescription GetEquivalentOperationDescription(OperationDescription operation)
        {
            Fx.Assert(operation != null, "OperationDescription cannnot be null");

            OperationDescription copy = CreateEmptyOperationDescription(operation);
            HttpOperationDescription httpDescription = copy.ToHttpOperationDescription();
            HttpOperationDescription originalHttpDescription = operation.ToHttpOperationDescription();
            UriTemplate template = originalHttpDescription.GetUriTemplate();
            List<string> templateVariables = new List<string>(template.PathSegmentVariableNames.Concat(template.QueryValueVariableNames));

            IEnumerable<HttpParameter> originalRequestBodyParameters = originalHttpDescription.InputParameters.Where(param => param.IsContentParameter);
            IEnumerable<HttpParameter> originalResponseBodyParameters = originalHttpDescription.OutputParameters.Where(param => param.IsContentParameter);
            IEnumerable<HttpParameter> originalUriTemplateParameters = originalHttpDescription.InputParameters.Where(
                (param) =>
                {
                    foreach (string templateVariable in templateVariables)
                    {
                        if (string.Equals(templateVariable, param.Name, StringComparison.OrdinalIgnoreCase))
                        {
                            return true;
                        }
                    }

                    return false;
                });

            httpDescription.ReturnValue = originalHttpDescription.ReturnValue;

            // add UriTemplate parameters
            foreach (HttpParameter parameter in originalUriTemplateParameters)
            {
                httpDescription.InputParameters.Add(parameter);
            }

            // add body parameters
            foreach (HttpParameter parameter in originalRequestBodyParameters)
            {
                httpDescription.InputParameters.Add(parameter);
            }

            int index = httpDescription.InputParameters.Count;
            int requestBodyParametersCount = originalRequestBodyParameters.Count<HttpParameter>();
            int responseBodyParametersCount = originalResponseBodyParameters.Count<HttpParameter>();

            if (requestBodyParametersCount == 0 || responseBodyParametersCount == 0)
            {
                // Special case if any input parameter is HttpRequestMessage or HttpResponseMessage
                foreach (HttpParameter inputParameter in originalHttpDescription.InputParameters)
                {
                    if (requestBodyParametersCount == 0 && originalHttpDescription.GetHttpMethod() != HttpMethod.Get && httpRequestMessageType.IsAssignableFrom(inputParameter.Type))
                    {
                        // add the HttpRequestmessage as a body parameter of type Message
                        HttpParameter parameter = new HttpParameter(new MessagePartDescription(inputParameter.Name, string.Empty) { Type = typeof(Message), Index = index++ });
                        httpDescription.InputParameters.Add(parameter);
                    }

                    if (!operation.IsOneWay && responseBodyParametersCount == 0 && httpResponseMessageType.IsAssignableFrom(inputParameter.Type))
                    {
                        // add the HttpResponsemessage as a return value of type Message
                        httpDescription.ReturnValue = new HttpParameter(new MessagePartDescription(inputParameter.Name, string.Empty) { Type = typeof(Message) });
                    }
                }
            }

            foreach (HttpParameter parameter in originalResponseBodyParameters)
            {
                // cannot do a byRef comparison here
                if (parameter.Type != originalHttpDescription.ReturnValue.Type || !string.Equals(parameter.Name, originalHttpDescription.ReturnValue.Name, StringComparison.OrdinalIgnoreCase))
                {
                    httpDescription.OutputParameters.Add(parameter);
                }
            }

            if (responseBodyParametersCount == 0)
            {
                foreach (HttpParameter outputParameter in originalHttpDescription.OutputParameters)
                {
                    // special case HttpResponseMessage when it is set as an out parameter
                    if (httpResponseMessageType.IsAssignableFrom(outputParameter.Type))
                    {
                        httpDescription.ReturnValue = new HttpParameter(new MessagePartDescription(outputParameter.Name, string.Empty) { Type = typeof(Message) });
                    }
                }
            }

            if (templateVariables.Count > originalUriTemplateParameters.Count<HttpParameter>())
            {
                // this means that we have some UriTemplate variables that are not explicitly bound to an input parameter
                foreach (HttpParameter parameter in originalUriTemplateParameters)
                {
                    templateVariables.Remove(parameter.Name);
                }

                foreach (string variable in templateVariables)
                {
                    HttpParameter parameter = new HttpParameter(new MessagePartDescription(variable, operation.DeclaringContract.Namespace) { Type = typeof(string), Index = index++ });
                    httpDescription.InputParameters.Add(parameter);
                }
            }

//.........这里部分代码省略.........
开发者ID:nuxleus,项目名称:WCFWeb,代码行数:101,代码来源:ClientContractDescriptionHelper.cs


注:本文中的System.ServiceModel.Description.OperationDescription.ToHttpOperationDescription方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。