コード例 #1
0
        public static void ReplaceFormatterBehavior(OperationDescription operationDescription, EndpointAddress address)
        {
            // look for and remove the DataContract behavior if it is present
            IOperationBehavior formatterBehavior = operationDescription.Behaviors.Remove <DataContractSerializerOperationBehavior>();

            if (formatterBehavior == null)
            {
                // look for and remove the XmlSerializer behavior if it is present
                formatterBehavior = operationDescription.Behaviors.Remove <XmlSerializerOperationBehavior>();
                if (formatterBehavior == null)
                {
                    // look for delegating formatter behavior
                    DelegatingFormatterBehavior existingDelegatingFormatterBehavior = operationDescription.Behaviors.Find <DelegatingFormatterBehavior>();
                    if (existingDelegatingFormatterBehavior == null)
                    {
                        throw new InvalidOperationException("Could not find DataContractFormatter or XmlSerializer on the contract");
                    }
                }
            }

            //remember what the innerFormatterBehavior was
            DelegatingFormatterBehavior delegatingFormatterBehavior = new DelegatingFormatterBehavior(address, formatterBehavior);

            operationDescription.Behaviors.Add(delegatingFormatterBehavior);
        }
コード例 #2
0
        public static void ReplaceFormatterBehavior(OperationDescription operationDescription, EndpointAddress address)
        {
            // look for and remove the DataContract behavior if it is present
            IOperationBehavior formatterBehavior = operationDescription.Behaviors.Remove<DataContractSerializerOperationBehavior>();
            if (formatterBehavior == null)
            {
                // look for and remove the XmlSerializer behavior if it is present
                formatterBehavior = operationDescription.Behaviors.Remove<XmlSerializerOperationBehavior>();
                if (formatterBehavior == null)
                {
                    // look for delegating formatter behavior
                    DelegatingFormatterBehavior existingDelegatingFormatterBehavior = operationDescription.Behaviors.Find<DelegatingFormatterBehavior>();
                    if (existingDelegatingFormatterBehavior == null)
                    {
                        throw new InvalidOperationException("Could not find DataContractFormatter or XmlSerializer on the contract");
                    }
                }
            }

            //remember what the innerFormatterBehavior was
            DelegatingFormatterBehavior delegatingFormatterBehavior = new DelegatingFormatterBehavior(address, formatterBehavior);
            operationDescription.Behaviors.Add(delegatingFormatterBehavior);
        }