internal static new bool TryCreate(BindingElementCollection bindingElements, out Binding binding)
        {
            if (bindingElements == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("bindingElements");
            }

            binding = null;

            ContextBindingElement contextBindingElement = bindingElements.Find <ContextBindingElement>();

            if (contextBindingElement != null)
            {
                BindingElementCollection bindingElementsWithoutContext = new BindingElementCollection(bindingElements);
                bindingElementsWithoutContext.Remove <ContextBindingElement>();
                Binding wsHttpBinding;
                if (WSHttpBinding.TryCreate(bindingElementsWithoutContext, out wsHttpBinding))
                {
                    bool allowCookies = ((WSHttpBinding)wsHttpBinding).AllowCookies;
                    if (allowCookies && contextBindingElement.ContextExchangeMechanism == ContextExchangeMechanism.HttpCookie ||
                        !allowCookies && contextBindingElement.ContextExchangeMechanism == ContextExchangeMechanism.ContextSoapHeader)
                    {
                        WSHttpContextBinding contextBinding = new WSHttpContextBinding((WSHttpBinding)wsHttpBinding);
                        contextBinding.ContextProtectionLevel   = contextBindingElement.ProtectionLevel;
                        contextBinding.ContextManagementEnabled = contextBindingElement.ContextManagementEnabled;
                        binding = contextBinding;
                    }
                }
            }

            return(binding != null);
        }
Ejemplo n.º 2
0
        internal static bool TryCreate(BindingElementCollection bindingElements, out Binding binding)
        {
            if (bindingElements == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("bindingElements");
            }
            binding = null;
            ContextBindingElement element = bindingElements.Find <ContextBindingElement>();

            if ((element != null) && (element.ContextExchangeMechanism != ContextExchangeMechanism.HttpCookie))
            {
                Binding binding2;
                BindingElementCollection elements = new BindingElementCollection(bindingElements);
                elements.Remove <ContextBindingElement>();
                if (NetTcpBinding.TryCreate(elements, out binding2))
                {
                    NetTcpContextBinding binding3 = new NetTcpContextBinding((NetTcpBinding)binding2)
                    {
                        ContextProtectionLevel   = element.ProtectionLevel,
                        ContextManagementEnabled = element.ContextManagementEnabled
                    };
                    binding = binding3;
                }
            }
            return(binding != null);
        }
Ejemplo n.º 3
0
        internal static bool TryCreate(BindingElementCollection bindingElements, out Binding binding)
        {
            if (bindingElements == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("bindingElements");
            }
            binding = null;
            ContextBindingElement element = bindingElements.Find <ContextBindingElement>();

            if (element != null)
            {
                Binding binding2;
                BindingElementCollection elements = new BindingElementCollection(bindingElements);
                elements.Remove <ContextBindingElement>();
                if (WSHttpBindingBase.TryCreate(elements, out binding2))
                {
                    bool allowCookies = ((WSHttpBinding)binding2).AllowCookies;
                    if ((allowCookies && (element.ContextExchangeMechanism == ContextExchangeMechanism.HttpCookie)) || (!allowCookies && (element.ContextExchangeMechanism == ContextExchangeMechanism.ContextSoapHeader)))
                    {
                        WSHttpContextBinding binding3 = new WSHttpContextBinding((WSHttpBinding)binding2)
                        {
                            ContextProtectionLevel   = element.ProtectionLevel,
                            ContextManagementEnabled = element.ContextManagementEnabled
                        };
                        binding = binding3;
                    }
                }
            }
            return(binding != null);
        }
        internal static new bool TryCreate(BindingElementCollection bindingElements, out Binding binding)
        {
            if (bindingElements == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("bindingElements");
            }

            binding = null;

            ContextBindingElement contextBindingElement = bindingElements.Find <ContextBindingElement>();

            if (contextBindingElement != null && contextBindingElement.ContextExchangeMechanism != ContextExchangeMechanism.HttpCookie)
            {
                BindingElementCollection bindingElementsWithoutContext = new BindingElementCollection(bindingElements);
                bindingElementsWithoutContext.Remove <ContextBindingElement>();
                Binding netTcpBinding;
                if (NetTcpBinding.TryCreate(bindingElementsWithoutContext, out netTcpBinding))
                {
                    NetTcpContextBinding contextBinding = new NetTcpContextBinding((NetTcpBinding)netTcpBinding);
                    contextBinding.ContextProtectionLevel   = contextBindingElement.ProtectionLevel;
                    contextBinding.ContextManagementEnabled = contextBindingElement.ContextManagementEnabled;
                    binding = contextBinding;
                }
            }

            return(binding != null);
        }
Ejemplo n.º 5
0
        //The heart of the custom binding
        public override BindingElementCollection CreateBindingElements()
        {
            BindingElement element = new ContextBindingElement(ContextProtectionLevel, ContextExchangeMechanism.ContextSoapHeader);

            BindingElementCollection elements = base.CreateBindingElements();

            elements.Insert(0, element);

            return(elements);
        }
        public static void ExportRequireContextAssertion(ContextBindingElement bindingElement, PolicyAssertionCollection assertions)
        {
            if (bindingElement == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("bindingElement");
            }
            if (assertions == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("assertions");
            }
            if (bindingElement.ContextExchangeMechanism != ContextExchangeMechanism.ContextSoapHeader)
            {
                XmlElement item = Document.CreateElement(null, "HttpUseCookie", "http://schemas.xmlsoap.org/soap/http");
                assertions.Add(item);
            }
            else
            {
                XmlElement element = Document.CreateElement(null, "IncludeContext", "http://schemas.microsoft.com/ws/2006/05/context");
                System.Xml.XmlAttribute node = Document.CreateAttribute("ProtectionLevel");
                switch (bindingElement.ProtectionLevel)
                {
                    case ProtectionLevel.Sign:
                        node.Value = "Sign";
                        break;

                    case ProtectionLevel.EncryptAndSign:
                        node.Value = "EncryptAndSign";
                        break;

                    default:
                        node.Value = "None";
                        break;
                }
                element.Attributes.Append(node);
                assertions.Add(element);
            }
        }
        public static void ExportRequireContextAssertion(ContextBindingElement bindingElement, PolicyAssertionCollection assertions)
        {
            if (bindingElement == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("bindingElement");
            }
            if (assertions == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("assertions");
            }

            if (bindingElement.ContextExchangeMechanism == ContextExchangeMechanism.ContextSoapHeader)
            {
                XmlElement assertion = Document.CreateElement(null, IncludeContextName, WscNamespace);
                XmlAttribute protectionLevelAttribute = Document.CreateAttribute(ProtectionLevelName);
                switch (bindingElement.ProtectionLevel)
                {
                    case ProtectionLevel.EncryptAndSign:
                        protectionLevelAttribute.Value = EncryptAndSignName;
                        break;
                    case ProtectionLevel.Sign:
                        protectionLevelAttribute.Value = SignName;
                        break;
                    default:
                        protectionLevelAttribute.Value = NoneName;
                        break;
                }
                assertion.Attributes.Append(protectionLevelAttribute);

                assertions.Add(assertion);
            }
            else
            {
                XmlElement assertion = Document.CreateElement(null, HttpUseCookieName, HttpNamespace);
                assertions.Add(assertion);
            }
        }
 public static bool TryImportRequireContextAssertion(PolicyAssertionCollection assertions, out ContextBindingElement bindingElement)
 {
     if (assertions == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("assertions");
     }
     bindingElement = null;
     foreach (XmlElement element in assertions)
     {
         if (((element.LocalName == "IncludeContext") && (element.NamespaceURI == "http://schemas.microsoft.com/ws/2006/05/context")) && ContainOnlyWhitespaceChild(element))
         {
             string attribute = element.GetAttribute("ProtectionLevel");
             if ("EncryptAndSign".Equals(attribute, StringComparison.Ordinal))
             {
                 bindingElement = new ContextBindingElement(ProtectionLevel.EncryptAndSign);
             }
             else if ("Sign".Equals(attribute, StringComparison.Ordinal))
             {
                 bindingElement = new ContextBindingElement(ProtectionLevel.Sign);
             }
             else if ("None".Equals(attribute, StringComparison.Ordinal))
             {
                 bindingElement = new ContextBindingElement(ProtectionLevel.None);
             }
             if (bindingElement != null)
             {
                 assertions.Remove(element);
                 return true;
             }
         }
     }
     return false;
 }
        public static bool TryImportRequireContextAssertion(PolicyAssertionCollection assertions, out ContextBindingElement bindingElement)
        {
            if (assertions == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("assertions");
            }

            bindingElement = null;

            foreach (XmlElement assertion in assertions)
            {
                if (assertion.LocalName == IncludeContextName
                    && assertion.NamespaceURI == WscNamespace
                    && ContainOnlyWhitespaceChild(assertion))
                {  
                    string protectionLevelAttribute = assertion.GetAttribute(ProtectionLevelName);
                    if (EncryptAndSignName.Equals(protectionLevelAttribute, StringComparison.Ordinal))
                    {
                        bindingElement = new ContextBindingElement(ProtectionLevel.EncryptAndSign);
                    }
                    else if (SignName.Equals(protectionLevelAttribute, StringComparison.Ordinal))
                    {
                        bindingElement = new ContextBindingElement(ProtectionLevel.Sign);
                    }
                    else if (NoneName.Equals(protectionLevelAttribute, StringComparison.Ordinal))
                    {
                        bindingElement = new ContextBindingElement(ProtectionLevel.None);
                    }

                    if (bindingElement != null)
                    {
                        assertions.Remove(assertion);
                        return true;
                    }
                }
            }

            return false;
        }
        public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            if (serviceDescription == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serviceDescription");
            }

            ContextBindingElement.ValidateContextBindingElementOnAllEndpointsWithSessionfulContract(serviceDescription, this);

            if (serviceDescription.Behaviors != null)
            {
                ServiceBehaviorAttribute serviceBehavior = serviceDescription.Behaviors.Find <ServiceBehaviorAttribute>();

                if (serviceBehavior != null)
                {
                    if (serviceBehavior.InstanceContextMode != InstanceContextMode.PerSession)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                                  new InvalidOperationException(
                                      SR2.GetString(SR2.InstanceContextModeMustBePerSession, serviceBehavior.InstanceContextMode)));
                    }

                    if (serviceBehavior.ConcurrencyMode == ConcurrencyMode.Multiple)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                                  new InvalidOperationException(
                                      SR2.GetString(SR2.ConcurrencyMultipleNotSupported)));
                    }

                    if (serviceBehavior.ConcurrencyMode == ConcurrencyMode.Reentrant &&
                        this.UnknownExceptionAction == UnknownExceptionAction.AbortInstance)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                                  new InvalidOperationException(
                                      SR2.GetString(SR2.ConcurrencyReentrantAndAbortNotSupported)));
                    }
                }
            }

            bool foundSessionfulContract = false;

            foreach (ServiceEndpoint serviceEndpoint in serviceDescription.Endpoints)
            {
                if (serviceEndpoint != null && !serviceEndpoint.InternalIsSystemEndpoint(serviceDescription))
                {
                    if (serviceEndpoint.Contract.SessionMode != SessionMode.NotAllowed)
                    {
                        foundSessionfulContract = true;
                    }

                    foreach (OperationDescription operation in serviceEndpoint.Contract.Operations)
                    {
                        DurableOperationAttribute durableBehavior =
                            operation.Behaviors.Find <DurableOperationAttribute>();

                        if (durableBehavior == null)
                        {
                            durableBehavior = defaultDurableOperationBehavior;
                        }

                        if (serviceEndpoint.Contract.SessionMode == SessionMode.NotAllowed)
                        {
                            if (!durableBehavior.CanCreateInstanceForOperation(operation.IsOneWay))
                            {
                                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                                          new InvalidOperationException(
                                              SR2.GetString(
                                                  SR2.CanCreateInstanceMustBeTrue,
                                                  serviceEndpoint.Contract.Name,
                                                  operation.Name)));
                            }
                        }
                        else
                        {
                            if (operation.IsOneWay &&
                                durableBehavior.CanCreateInstanceForOperation(operation.IsOneWay))
                            {
                                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                                          new InvalidOperationException(
                                              SR2.GetString(
                                                  SR2.CanCreateInstanceMustBeTwoWay,
                                                  serviceEndpoint.Contract.Name,
                                                  serviceEndpoint.Contract.SessionMode,
                                                  operation.Name)));
                            }
                        }

                        if (this.saveStateInOperationTransaction)
                        {
                            bool hasTransaction = false;

                            OperationBehaviorAttribute operationBehavior = operation.Behaviors.Find <OperationBehaviorAttribute>();

                            if (operationBehavior != null)
                            {
                                if (operationBehavior.TransactionScopeRequired)
                                {
                                    hasTransaction = true;
                                }
                            }

                            TransactionFlowAttribute transactionBehavior = operation.Behaviors.Find <TransactionFlowAttribute>();

                            if (transactionBehavior != null)
                            {
                                if (transactionBehavior.Transactions == TransactionFlowOption.Mandatory)
                                {
                                    hasTransaction = true;
                                }
                            }

                            if (!hasTransaction)
                            {
                                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                                          new InvalidOperationException(
                                              SR2.GetString(
                                                  SR2.SaveStateInTransactionValidationFailed,
                                                  operation.Name,
                                                  serviceEndpoint.ListenUri)));
                            }
                        }
                    }
                }
            }

            if (!foundSessionfulContract)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                          new InvalidOperationException(
                              SR2.GetString(SR2.SessionfulContractNotFound)));
            }
        }
 public void Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
 {
     ContextBindingElement.ValidateContextBindingElementOnAllEndpointsWithSessionfulContract(description, this);
 }