void SetFormatAndContentType(WebMessageFormat format, string contentType)
        {
            OutgoingWebResponseContext outgoingResponse = WebOperationContext.Current.OutgoingResponse;

            outgoingResponse.Format = format;
            outgoingResponse.AutomatedFormatSelectionContentType = contentType;

            if (DiagnosticUtility.ShouldTraceInformation)
            {
                TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.AutomaticFormatSelectedRequestBased, SR2.GetString(SR2.TraceCodeAutomaticFormatSelectedRequestBased, format.ToString(), contentType));
            }
        }
        public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
        {
            WebOperationContext        currentContext   = WebOperationContext.Current;
            OutgoingWebResponseContext outgoingResponse = null;

            if (currentContext != null)
            {
                outgoingResponse = currentContext.OutgoingResponse;
            }

            WebMessageFormat format = this.defaultFormat;

            if (outgoingResponse != null)
            {
                WebMessageFormat?nullableFormat = outgoingResponse.Format;
                if (nullableFormat.HasValue)
                {
                    format = nullableFormat.Value;
                }
            }

            if (!this.formatters.ContainsKey(format))
            {
                string operationName = "<null>";

                if (OperationContext.Current != null)
                {
                    MessageProperties messageProperties = OperationContext.Current.IncomingMessageProperties;
                    if (messageProperties.ContainsKey(WebHttpDispatchOperationSelector.HttpOperationNamePropertyName))
                    {
                        operationName = messageProperties[WebHttpDispatchOperationSelector.HttpOperationNamePropertyName] as string;
                    }
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR2.GetString(SR2.OperationDoesNotSupportFormat, operationName, format.ToString())));
            }

            if (outgoingResponse != null && string.IsNullOrEmpty(outgoingResponse.ContentType))
            {
                string automatedSelectionContentType = outgoingResponse.AutomatedFormatSelectionContentType;
                if (!string.IsNullOrEmpty(automatedSelectionContentType))
                {
                    // Don't set the content-type if it is default xml for backwards compatiabilty
                    if (!string.Equals(automatedSelectionContentType, defaultContentTypes[WebMessageFormat.Xml], StringComparison.OrdinalIgnoreCase))
                    {
                        outgoingResponse.ContentType = automatedSelectionContentType;
                    }
                }
                else
                {
                    // Don't set the content-type if it is default xml for backwards compatiabilty
                    if (format != WebMessageFormat.Xml)
                    {
                        outgoingResponse.ContentType = defaultContentTypes[format];
                    }
                }
            }

            Message message = this.formatters[format].SerializeReply(messageVersion, parameters, result);

            return(message);
        }
        void SetFormatFromDefault(string operationName, string acceptHeader)
        {
            Fx.Assert(this.formatters.ContainsKey(operationName), "The calling method is responsible for ensuring that the 'operationName' key exists in the formatters dictionary.");
            WebMessageFormat format = this.formatters[operationName].DefaultFormat;

            if (!string.IsNullOrEmpty(acceptHeader))
            {
                this.caches[operationName].AddOrUpdate(acceptHeader.ToUpperInvariant(), new FormatContentTypePair(format, null));
            }

            WebOperationContext.Current.OutgoingResponse.Format = format;

            if (DiagnosticUtility.ShouldTraceInformation)
            {
                TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.AutomaticFormatSelectedOperationDefault, SR2.GetString(SR2.TraceCodeAutomaticFormatSelectedOperationDefault, format.ToString()));
            }
        }
        /// <summary>
        /// Create the entire proxy method as C# code.
        /// </summary>
        public override void Generate(CodeTypeDeclaration declaringType, CodeGenerator generator)
        {
            // Prepare method
            var methodDecl = CreateProxyMethodDeclaration();

            // Add code
            methodDecl.Statements.Add(new CodeVariableDeclarationStatement(new CodeTypeReference(typeof(UriTemplate)), "uriTemplate", new CodeObjectCreateExpression(typeof(UriTemplate),
                                                                                                                                                                     new CodePrimitiveExpression(uriTemplate))));

            methodDecl.Statements.Add(new CodeVariableDeclarationStatement(new CodeTypeReference(typeof(NameValueCollection)), "nameValues", new CodeObjectCreateExpression(typeof(NameValueCollection))));

            foreach (var inputArgument in inputArguments)
            {
                methodDecl.Statements.Add(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("nameValues"), "Add", new CodePrimitiveExpression(inputArgument), new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(inputArgument), "ToString()")));
            }

            methodDecl.Statements.Add(new CodeVariableDeclarationStatement(new CodeTypeReference(typeof(Uri)), "uri", new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("uriTemplate"), "BindByName", new CodeTypeReferenceExpression("baseAddress"), new CodeTypeReferenceExpression("nameValues"))));

            methodDecl.Statements.Add(new CodeVariableDeclarationStatement(new CodeTypeReference(typeof(Stream)), "inStream", new CodeSnippetExpression("null")));
            methodDecl.Statements.Add(new CodeVariableDeclarationStatement(new CodeTypeReference(typeof(Stream)), "outStream", new CodeSnippetExpression("null")));

            var codeTryCatchFinallyStatement = new CodeTryCatchFinallyStatement();

            methodDecl.Statements.Add(codeTryCatchFinallyStatement);

            if (typeSerializerBuilder != null)
            {
                var arrayPostfix  = serializerTypeIsArray ? "Array" : "";
                var serializeCode = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("SerializationHelper"), typeSerializerBuilder.MethodName + arrayPostfix,
                                                                   new CodeTypeReferenceExpression(toSerializeArgument), new CodeTypeReferenceExpression("SerializationHelper.SerializationFormat." + serializationFormat.ToString()), new CodeTypeReferenceExpression("System.ServiceModel.Web.WebMessageFormat." + requestFormat.ToString()));
                codeTryCatchFinallyStatement.TryStatements.Add(new CodeAssignStatement(new CodeTypeReferenceExpression("inStream"), serializeCode));
            }

            var webCode = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("WebHelper"), "WebInvoke",
                                                         new CodeTypeReferenceExpression("uri"), new CodeTypeReferenceExpression("WebHelper.Verb." + webMode.ToString()), new CodeTypeReferenceExpression("inStream"), new CodeTypeReferenceExpression("System.ServiceModel.Web.WebMessageFormat." + requestFormat.ToString()), new CodeSnippetExpression(typeDeserializerBuilder != null ? "true" : "false"));

            codeTryCatchFinallyStatement.TryStatements.Add(new CodeAssignStatement(new CodeTypeReferenceExpression("outStream"), webCode));

            if (typeDeserializerBuilder != null)
            {
                var arrayPostfix = deserializerTypeIsArray ? "Array" : "";
                codeTryCatchFinallyStatement.TryStatements.Add(new CodeMethodReturnStatement(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("SerializationHelper"), typeDeserializerBuilder.MethodName + arrayPostfix, new CodeTypeReferenceExpression("outStream"),
                                                                                                                            new CodeTypeReferenceExpression("SerializationHelper.SerializationFormat." + serializationFormat.ToString()), new CodeTypeReferenceExpression("System.ServiceModel.Web.WebMessageFormat." + responseFormat.ToString()))));
            }

            codeTryCatchFinallyStatement.FinallyStatements.Add(new CodeConditionStatement(new CodeSnippetExpression("inStream != null"), new CodeExpressionStatement(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("inStream"), "Dispose"))));
            codeTryCatchFinallyStatement.FinallyStatements.Add(new CodeConditionStatement(new CodeSnippetExpression("outStream != null"), new CodeExpressionStatement(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("outStream"), "Dispose"))));


            // Add method to declaring type
            declaringType.Members.Add(methodDecl);
        }
 private static WebBodyFormatMessageProperty GetBodyFormat(WebMessageFormat webMessageFormat)
 {
     return new WebBodyFormatMessageProperty((WebContentFormat) Enum.Parse(typeof(WebContentFormat), webMessageFormat.ToString()));
 }
예제 #6
0
 private static WebBodyFormatMessageProperty GetBodyFormat(WebMessageFormat webMessageFormat)
 {
     return(new WebBodyFormatMessageProperty((WebContentFormat)Enum.Parse(typeof(WebContentFormat), webMessageFormat.ToString())));
 }
        void SetFormatAndContentType(WebMessageFormat format, string contentType)
        {
            OutgoingWebResponseContext outgoingResponse = WebOperationContext.Current.OutgoingResponse;
            outgoingResponse.Format = format;
            outgoingResponse.AutomatedFormatSelectionContentType = contentType;

            if (DiagnosticUtility.ShouldTraceInformation)
            {
                TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.AutomaticFormatSelectedRequestBased, SR2.GetString(SR2.TraceCodeAutomaticFormatSelectedRequestBased, format.ToString(), contentType));
            }
        }