Commit 6bae4abb authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading
+23.5 KiB

File added.

No diff preview for this file type.

+9 −0
Changes for PWS - Sources/ServiceModelEx/App.Config: 9 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?xml version = "1.0" encoding = "utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name = "ServiceModelEx.Properties.Settings.PublishSubscribeConnectionString"
            connectionString="Data Source=(local);Initial Catalog=PublishSubscribe;Integrated Security=True" />
    </connectionStrings>
</configuration>
 No newline at end of file
+194 −0
Changes for PWS - Sources/ServiceModelEx/BindingRequirementAttribute.cs: 194 added lines, 0 removed lines.
Original line number Diff line number Diff line
// © 2007 IDesign Inc. All rights reserved 
//Questions? Comments? go to 
//http://www.idesign.net

using System;
using System.ServiceModel;
using System.Diagnostics;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.Collections.ObjectModel;
using System.ServiceModel.Dispatcher;

namespace ServiceModelEx
{
   [AttributeUsage(AttributeTargets.Class)]
   public class BindingRequirementAttribute : Attribute,IServiceBehavior,IEndpointBehavior
   {
      bool m_TransactionFlowEnabled;
      bool m_WCFOnly;
      bool m_ReliabilityRequired;

      public bool ReliabilityRequired
      {
         get
         {
            return m_ReliabilityRequired;
         }
         set
         {
            m_ReliabilityRequired = value;
         }
      }

      public bool WCFOnly
      {
         get 
         { 
            return m_WCFOnly; 
         }
         set 
         {
            m_WCFOnly = value; 
         }
      }

      public bool TransactionFlowEnabled
      {
         get
         {
            return m_TransactionFlowEnabled;
         }
         set
         {
            m_TransactionFlowEnabled = value;
         }
      }

      static void ValidateWCF(ServiceEndpoint endpoint)
      {
         if(endpoint.Binding is NetTcpBinding || 
            endpoint.Binding is NetNamedPipeBinding ||
            endpoint.Binding is NetMsmqBinding)
         {
            return;
         }
         throw new InvalidOperationException("BindingRequirementAttribute requires WCF-to-WCF binding only, but binding for the endpoint with contract " + endpoint.Contract.ContractType + " in not.");
      }
      static void ValidateReliability(ServiceEndpoint endpoint)
      {
         if(endpoint.Binding is NetNamedPipeBinding)//Inherently reliable
         {
            return;
         }
         if(endpoint.Binding is WSDualHttpBinding)//Always reliable
         {
            return;
         }             
         if(endpoint.Binding is NetTcpBinding)
         {
            NetTcpBinding tcpBinding = endpoint.Binding as NetTcpBinding;
            if(tcpBinding.ReliableSession.Enabled)
            {
               return;
            }
         }
         if(endpoint.Binding is WSHttpBindingBase)
         {
            WSHttpBindingBase wsBinding = endpoint.Binding as WSHttpBindingBase;
            if(wsBinding.ReliableSession.Enabled)
            {
               return;
            }
         }
         throw new InvalidOperationException("BindingRequirementAttribute requires reliability enabled, but binding for the endpoint with contract " + endpoint.Contract.ContractType + " has does not support reliability or has it disabled");
      }
      static void ValidateTransactionFlow(ServiceEndpoint endpoint)
      {
         Exception exception = new InvalidOperationException("BindingRequirementAttribute requires transaction flow enabled, but binding for the endpoint with contract " + endpoint.Contract.ContractType + " has it disabled");

         foreach(OperationDescription operation in endpoint.Contract.Operations)
         {
            foreach(IOperationBehavior behavior in operation.Behaviors)
            {
               if(behavior is TransactionFlowAttribute)
               {
                  TransactionFlowAttribute attribute = behavior as TransactionFlowAttribute;
                  if(attribute.Transactions == TransactionFlowOption.Allowed)
                  {
                     if(endpoint.Binding is NetTcpBinding)
                     {
                        NetTcpBinding tcpBinding = endpoint.Binding as NetTcpBinding;
                        if(tcpBinding.TransactionFlow == false)
                        {
                           throw exception;
                        }
                        break;
                     }
                     if(endpoint.Binding is NetNamedPipeBinding)
                     {
                        NetNamedPipeBinding ipcBinding = endpoint.Binding as NetNamedPipeBinding;
                        if(ipcBinding.TransactionFlow == false)
                        {
                           throw exception;
                        }
                        break;
                     }
                     if(endpoint.Binding is WSHttpBindingBase)
                     {
                        WSHttpBindingBase wsBinding = endpoint.Binding as WSHttpBindingBase;
                        if(wsBinding.TransactionFlow == false)
                        {
                           throw exception;
                        }
                        break;
                     }
                     if(endpoint.Binding is WSDualHttpBinding)
                     {
                        WSDualHttpBinding wsDualBinding = endpoint.Binding as WSDualHttpBinding;
                        if(wsDualBinding.TransactionFlow == false)
                        {
                           throw exception;
                        }
                        break;
                     }
                     throw new InvalidOperationException("BindingRequirementAttribute requires transaction flow enabled, but binding for the endpoint with contract " + endpoint.Contract.ContractType + " does not support transaction flow");
                  }
               }
            }
         }
      }
      void IServiceBehavior.Validate(ServiceDescription description,ServiceHostBase host) 
      {
         IEndpointBehavior endpointBehavior = this;
         foreach(ServiceEndpoint endpoint in description.Endpoints)
         {
            endpointBehavior.Validate(endpoint);
         }
      }
      void IServiceBehavior.AddBindingParameters(ServiceDescription description,ServiceHostBase host,Collection<ServiceEndpoint> endpoints,BindingParameterCollection parameters)
      {}
      void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription description,ServiceHostBase host)
      {}

      void IEndpointBehavior.AddBindingParameters(ServiceEndpoint serviceEndpoint,BindingParameterCollection bindingParameters)
      {}

      void IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint serviceEndpoint,ClientRuntime behavior)
      {}

      void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint serviceEndpoint,EndpointDispatcher endpointDispatcher)
      {}

      void IEndpointBehavior.Validate(ServiceEndpoint endpoint)
      {
         if(WCFOnly)
         {
            ValidateWCF(endpoint);
         }
         if(TransactionFlowEnabled)
         {
            ValidateTransactionFlow(endpoint);
         }
         if(ReliabilityRequired)
         {
            ValidateReliability(endpoint);
         }
      }
   }
} 




+1111 −0

File added.

Preview size limit exceeded, changes collapsed.

+44 −0
Changes for PWS - Sources/ServiceModelEx/Data/DataTableHelper.cs: 44 added lines, 0 removed lines.
Original line number Diff line number Diff line
//  2007 IDesign Inc. All rights reserved 
//Questions? Comments? go to 
//http://www.idesign.net

using System;
using System.Text;
using System.Data;
using System.Runtime.Serialization;
using System.Diagnostics;
using System.Collections;

namespace ServiceModelEx
{
   public static class DataTableHelper
   {
      public static T[] ToArray<R,T>(DataTable table,Converter<R,T> converter) where R : DataRow  
      {
         if(table.Rows.Count == 0)
         {
            return new T[]{};
         }
         //Verify [DataContract] or [Serializable] on T
         Debug.Assert(IsDataContract(typeof(T)) || typeof(T).IsSerializable);

         //Verify table contains correct rows 
         Debug.Assert(MatchingTableRow<R>(table));

         return Collection.UnsafeToArray(table.Rows,converter);
      }
      static bool IsDataContract(Type type)
      {
         object[] attributes = type.GetCustomAttributes(typeof(DataContractAttribute),false);
         return attributes.Length == 1;
      }
      static bool MatchingTableRow<R>(DataTable table)
      {
         if(table.Rows.Count == 0)
         {
            return true;
         }
         return table.Rows[0] is R;
      }
   }
}
Loading