private void SetBehaviors( ) { ValidationBehavior validation = new ValidationBehavior(true, false, ruleSet); endpointBehavior = (IEndpointBehavior)validation; contractBehavior = (IContractBehavior)validation; }
private void SetBehaviors() { ValidationBehavior validation = new ValidationBehavior(true, false, ruleSet); endpointBehavior = (IEndpointBehavior)validation; contractBehavior = (IContractBehavior)validation; operationBehavior = (IOperationBehavior)validation; }
private void ReflectOnCallbackInstance(ServiceEndpoint endpoint) { if (this.callbackType != null) { if (endpoint.Contract.CallbackContractType == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("SfxCallbackTypeCannotBeNull", new object[] { endpoint.Contract.ContractType.FullName }))); } this.TypeLoader.AddBehaviorsFromImplementationType(endpoint, this.callbackType); } else if ((this.CallbackInstance != null) && (this.CallbackInstance.UserObject != null)) { if (endpoint.Contract.CallbackContractType == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("SfxCallbackTypeCannotBeNull", new object[] { endpoint.Contract.ContractType.FullName }))); } object userObject = this.CallbackInstance.UserObject; System.Type implementationType = userObject.GetType(); this.TypeLoader.AddBehaviorsFromImplementationType(endpoint, implementationType); IEndpointBehavior item = userObject as IEndpointBehavior; if (item != null) { endpoint.Behaviors.Add(item); } IContractBehavior behavior2 = userObject as IContractBehavior; if (behavior2 != null) { endpoint.Contract.Behaviors.Add(behavior2); } } }
static void FillBehaviorInfo(IContractBehavior behavior, IWmiInstance existingInstance, out IWmiInstance instance) { Fx.Assert(null != existingInstance, ""); Fx.Assert(null != behavior, ""); instance = null; if (behavior is DeliveryRequirementsAttribute) { instance = existingInstance.NewInstance("DeliveryRequirementsAttribute"); DeliveryRequirementsAttribute specificBehavior = (DeliveryRequirementsAttribute)behavior; instance.SetProperty(AdministrationStrings.QueuedDeliveryRequirements, specificBehavior.QueuedDeliveryRequirements.ToString()); instance.SetProperty(AdministrationStrings.RequireOrderedDelivery, specificBehavior.RequireOrderedDelivery); if (null != specificBehavior.TargetContract) { instance.SetProperty(AdministrationStrings.TargetContract, specificBehavior.TargetContract.ToString()); } } else if (behavior is IWmiInstanceProvider) { IWmiInstanceProvider instanceProvider = (IWmiInstanceProvider)behavior; instance = existingInstance.NewInstance(instanceProvider.GetInstanceType()); instanceProvider.FillInstance(instance); } else { instance = existingInstance.NewInstance("Behavior"); } if (null != instance) { instance.SetProperty(AdministrationStrings.Type, behavior.GetType().FullName); } }
// This method runs validators (both builtin and ones in description). // Precondition: EnsureInvariants() should already have been called. void Validate(bool runOperationValidators, bool isForService) { // contract behaviors ContractDescription contract = Contract; for (int j = 0; j < contract.ContractBehaviors.Count; j++) { IContractBehavior iContractBehavior = contract.ContractBehaviors[j]; iContractBehavior.Validate(contract, this); } // endpoint behaviors for (int j = 0; j < EndpointBehaviors.Count; j++) { IEndpointBehavior ieb = EndpointBehaviors[j]; ieb.Validate(this); } // operation behaviors if (runOperationValidators) { for (int j = 0; j < contract.Operations.Count; j++) { OperationDescription op = contract.Operations[j]; TaskOperationDescriptionValidator.Validate(op, isForService); for (int k = 0; k < op.OperationBehaviors.Count; k++) { IOperationBehavior iob = op.OperationBehaviors[k]; iob.Validate(op); } } } }
private static void FillBehaviorInfo(IContractBehavior behavior, IWmiInstance existingInstance, out IWmiInstance instance) { instance = null; if (behavior is DeliveryRequirementsAttribute) { instance = existingInstance.NewInstance("DeliveryRequirementsAttribute"); DeliveryRequirementsAttribute attribute = (DeliveryRequirementsAttribute)behavior; instance.SetProperty("QueuedDeliveryRequirements", attribute.QueuedDeliveryRequirements.ToString()); instance.SetProperty("RequireOrderedDelivery", attribute.RequireOrderedDelivery); if (null != attribute.TargetContract) { instance.SetProperty("TargetContract", attribute.TargetContract.ToString()); } } else if (behavior is IWmiInstanceProvider) { IWmiInstanceProvider provider = (IWmiInstanceProvider)behavior; instance = existingInstance.NewInstance(provider.GetInstanceType()); provider.FillInstance(instance); } else { instance = existingInstance.NewInstance("Behavior"); } if (instance != null) { instance.SetProperty("Type", behavior.GetType().FullName); } }
private static void FillBehaviorInfo(IContractBehavior behavior, IWmiInstance existingInstance, out IWmiInstance instance) { instance = null; if (behavior is DeliveryRequirementsAttribute) { instance = existingInstance.NewInstance("DeliveryRequirementsAttribute"); DeliveryRequirementsAttribute attribute = (DeliveryRequirementsAttribute) behavior; instance.SetProperty("QueuedDeliveryRequirements", attribute.QueuedDeliveryRequirements.ToString()); instance.SetProperty("RequireOrderedDelivery", attribute.RequireOrderedDelivery); if (null != attribute.TargetContract) { instance.SetProperty("TargetContract", attribute.TargetContract.ToString()); } } else if (behavior is IWmiInstanceProvider) { IWmiInstanceProvider provider = (IWmiInstanceProvider) behavior; instance = existingInstance.NewInstance(provider.GetInstanceType()); provider.FillInstance(instance); } else { instance = existingInstance.NewInstance("Behavior"); } if (instance != null) { instance.SetProperty("Type", behavior.GetType().FullName); } }
private void SetBehaviors() { ValidatingBehavior validation = new ValidatingBehavior(true, false, Ruleset); _endpointBehavior = (IEndpointBehavior)validation; _contractBehavior = (IContractBehavior)validation; _operationBehavior = (IOperationBehavior)validation; }
public GameServiceHost(IContractBehavior contractBehavior, IErrorHandler errorHandler, string contractName, Type serviceType, params Uri[] baseAddresses) : base(serviceType, baseAddresses) { this.errorHandler = errorHandler; ImplementedContracts.Values .First(c => c.Name == contractName) .ContractBehaviors.Add(contractBehavior); }
public ExceptionShieldingAttribute(string exceptionPolicyName) { this.exceptionPolicyName = exceptionPolicyName; ExceptionShieldingBehavior behavior = new ExceptionShieldingBehavior(exceptionPolicyName); this.contractBehavior = (IContractBehavior)behavior; this.serviceBehavior = (IServiceBehavior)behavior; this.errorHandler = new ExceptionShieldingErrorHandler(exceptionPolicyName); }
internal DomainModelHost(DomainModelInstanceProvider instanceProvider, Type serviceType, Uri[] uris) : base(serviceType, uris) { m_InstanceProvider = instanceProvider; foreach (var cd in ImplementedContracts.Values) { cd.Behaviors.Add(m_InstanceProvider); } }
/// <summary> /// Initializes a new instance of the <see cref="T:ExceptionShieldingAttribute"/> class. /// </summary> /// <param name="exceptionPolicyName">Name of the exception policy.</param> public ExceptionShieldingAttribute(string exceptionPolicyName) { this.exceptionPolicyName = exceptionPolicyName; //The ServiceHost applies behaviors in the following order: // Contract // Operation // Endpoint // Service ExceptionShieldingBehavior behavior = new ExceptionShieldingBehavior(exceptionPolicyName); this.contractBehavior = (IContractBehavior)behavior; this.serviceBehavior = (IServiceBehavior)behavior; this.errorHandler = new ExceptionShieldingErrorHandler(exceptionPolicyName); }
private static void ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) { // contract behaviors ContractDescription contractDescription = serviceEndpoint.Contract; for (int i = 0; i < contractDescription.Behaviors.Count; i++) { IContractBehavior behavior = contractDescription.Behaviors[i]; behavior.ApplyClientBehavior(contractDescription, serviceEndpoint, clientRuntime); } // endpoint behaviors BindingInformationEndpointBehavior.Instance.ApplyClientBehavior(serviceEndpoint, clientRuntime); for (int i = 0; i < serviceEndpoint.Behaviors.Count; i++) { IEndpointBehavior behavior = serviceEndpoint.Behaviors[i]; behavior.ApplyClientBehavior(serviceEndpoint, clientRuntime); } // operation behaviors DispatcherBuilder.BindOperations(contractDescription, clientRuntime, null); }
// This method runs validators (both builtin and ones in description). // Precondition: EnsureInvariants() should already have been called. void Validate(bool runOperationValidators, bool isForService) { // contract behaviors ContractDescription contract = this.Contract; for (int j = 0; j < contract.Behaviors.Count; j++) { IContractBehavior iContractBehavior = contract.Behaviors[j]; iContractBehavior.Validate(contract, this); } // endpoint behaviors if (!isForService) { (PartialTrustValidationBehavior.Instance as IEndpointBehavior).Validate(this); #pragma warning disable 0618 (PeerValidationBehavior.Instance as IEndpointBehavior).Validate(this); #pragma warning restore 0618 (TransactionValidationBehavior.Instance as IEndpointBehavior).Validate(this); (SecurityValidationBehavior.Instance as IEndpointBehavior).Validate(this); (System.ServiceModel.MsmqIntegration.MsmqIntegrationValidationBehavior.Instance as IEndpointBehavior).Validate(this); } for (int j = 0; j < this.Behaviors.Count; j++) { IEndpointBehavior ieb = this.Behaviors[j]; ieb.Validate(this); } // operation behaviors if (runOperationValidators) { for (int j = 0; j < contract.Operations.Count; j++) { OperationDescription op = contract.Operations[j]; TaskOperationDescriptionValidator.Validate(op, isForService); for (int k = 0; k < op.Behaviors.Count; k++) { IOperationBehavior iob = op.Behaviors[k]; iob.Validate(op); } } } }
/// <summary> /// Initializes a new instance of the <see cref="ExceptionShieldingAttribute"/> class. /// </summary> /// <param name="exceptionPolicyName">Name of the exception policy.</param> public ExceptionShieldingAttribute( string exceptionPolicyName) { if (exceptionPolicyName.IsNullOrWhiteSpace()) { throw new ArgumentException("The argument cannot be null, empty string or consist of whitespace characters only.", nameof(exceptionPolicyName)); } ExceptionPolicyName = exceptionPolicyName; //The ServiceHost applies behaviors in the following order: // Contract // Operation // Endpoint // Service var behavior = new ExceptionShieldingBehavior(exceptionPolicyName); _contractBehavior = behavior; _serviceBehavior = behavior; _errorHandler = new ExceptionShieldingErrorHandler(ServiceLocator.Current.GetInstance <IWcfContextUtilities>(), exceptionPolicyName); }
internal static void InitializeServiceHost(ServiceHostBase serviceHost) { var description = serviceHost.Description; if (serviceHost.ImplementedContracts != null && serviceHost.ImplementedContracts.Count > 0) { EnsureThereAreApplicationEndpoints(description); } ValidateDescription(serviceHost); var stuffPerListenUriInfo = new Dictionary <ListenUriInfo, StuffPerListenUriInfo>(); var endpointInfosPerEndpointAddress = new Dictionary <EndpointAddress, Collection <EndpointInfo> >(); // Ensure ListenUri and group endpoints per ListenUri for (int i = 0; i < description.Endpoints.Count; i++) { ServiceEndpoint endpoint = description.Endpoints[i]; ListenUriInfo listenUriInfo = GetListenUriInfoForEndpoint(serviceHost, endpoint); if (!stuffPerListenUriInfo.ContainsKey(listenUriInfo)) { stuffPerListenUriInfo.Add(listenUriInfo, new StuffPerListenUriInfo()); } stuffPerListenUriInfo[listenUriInfo].Endpoints.Add(endpoint); } foreach (KeyValuePair <ListenUriInfo, StuffPerListenUriInfo> stuff in stuffPerListenUriInfo) { Uri listenUri = stuff.Key.ListenUri; ListenUriMode listenUriMode = stuff.Key.ListenUriMode; BindingParameterCollection parameters = stuff.Value.Parameters; Binding binding = stuff.Value.Endpoints[0].Binding; EndpointIdentity identity = stuff.Value.Endpoints[0].Address.Identity; // same EndpointAddressTable instance must be shared between channelDispatcher and parameters //ThreadSafeMessageFilterTable<EndpointAddress> endpointAddressTable = new ThreadSafeMessageFilterTable<EndpointAddress>(); //parameters.Add(endpointAddressTable); // add service-level binding parameters foreach (IServiceBehavior behavior in description.Behaviors) { behavior.AddBindingParameters(description, serviceHost, stuff.Value.Endpoints, parameters); } for (int i = 0; i < stuff.Value.Endpoints.Count; i++) { ServiceEndpoint endpoint = stuff.Value.Endpoints[i]; string viaString = listenUri.AbsoluteUri; // ensure all endpoints with this ListenUriInfo have same binding if (endpoint.Binding != binding) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.ABindingInstanceHasAlreadyBeenAssociatedTo1, viaString))); } // ensure all endpoints with this ListenUriInfo have same identity if (!object.Equals(endpoint.Address.Identity, identity)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( SR.Format(SR.SFxWhenMultipleEndpointsShareAListenUriTheyMustHaveSameIdentity, viaString))); } // add binding parameters (endpoint scope and below) AddBindingParametersForSecurityContractInformation(endpoint, parameters); AddBindingParameters(endpoint, parameters); } List <Type> channelTypes = GetSupportedChannelTypes(stuff.Value); var bindingQname = new XmlQualifiedName(binding.Name, binding.Namespace); var channelDispatcher = new ChannelDispatcher(listenUri, binding, bindingQname.ToString(), binding, channelTypes); //channelDispatcher.SetEndpointAddressTable(endpointAddressTable); stuff.Value.ChannelDispatcher = channelDispatcher; for (int i = 0; i < stuff.Value.Endpoints.Count; i++) { ServiceEndpoint endpoint = stuff.Value.Endpoints[i]; string viaString = listenUri.AbsoluteUri; //EndpointFilterProvider provider = new EndpointFilterProvider(); EndpointDispatcher dispatcher = BuildEndpointDispatcher(description, endpoint); if (!endpointInfosPerEndpointAddress.ContainsKey(endpoint.Address)) { endpointInfosPerEndpointAddress.Add(endpoint.Address, new Collection <EndpointInfo>()); } endpointInfosPerEndpointAddress[endpoint.Address].Add(new EndpointInfo(endpoint, dispatcher, /*provider*/ null)); channelDispatcher.Endpoints.Add(dispatcher); } // end foreach "endpoint" serviceHost.ChannelDispatchers.Add(channelDispatcher); } // end foreach "ListenUri/ChannelDispatcher" group // run service behaviors for (int i = 0; i < description.Behaviors.Count; i++) { IServiceBehavior serviceBehavior = description.Behaviors[i]; serviceBehavior.ApplyDispatchBehavior(description, serviceHost); } foreach (KeyValuePair <ListenUriInfo, StuffPerListenUriInfo> stuff in stuffPerListenUriInfo) { for (int i = 0; i < stuff.Value.Endpoints.Count; i++) { ServiceEndpoint endpoint = stuff.Value.Endpoints[i]; // rediscover which dispatcher goes with this endpoint Collection <EndpointInfo> infos = endpointInfosPerEndpointAddress[endpoint.Address]; EndpointInfo info = null; foreach (EndpointInfo ei in infos) { if (ei.Endpoint == endpoint) { info = ei; break; } } EndpointDispatcher dispatcher = info.EndpointDispatcher; // run contract behaviors for (int k = 0; k < endpoint.Contract.Behaviors.Count; k++) { IContractBehavior behavior = endpoint.Contract.Behaviors[k]; behavior.ApplyDispatchBehavior(endpoint.Contract, endpoint, dispatcher.DispatchRuntime); } // run endpoint behaviors ApplyBindingInformationFromEndpointToDispatcher(endpoint, dispatcher); for (int j = 0; j < endpoint.Behaviors.Count; j++) { IEndpointBehavior eb = endpoint.Behaviors[j]; eb.ApplyDispatchBehavior(endpoint, dispatcher); } // run operation behaviors BindOperations(endpoint.Contract, null, dispatcher.DispatchRuntime); } } EnsureRequiredRuntimeProperties(endpointInfosPerEndpointAddress); // Warn about obvious demux conflicts foreach (Collection <EndpointInfo> endpointInfos in endpointInfosPerEndpointAddress.Values) { // all elements of endpointInfos share the same Address (and thus EndpointListener.AddressFilter) if (endpointInfos.Count > 1) { for (int i = 0; i < endpointInfos.Count; i++) { for (int j = i + 1; j < endpointInfos.Count; j++) { // if not same ListenUri, won't conflict // if not same ChannelType, may not conflict (some transports demux based on this) // if they share a ChannelDispatcher, this means same ListenUri and same ChannelType if (endpointInfos[i].EndpointDispatcher.ChannelDispatcher == endpointInfos[j].EndpointDispatcher.ChannelDispatcher) { EndpointFilterProvider iProvider = endpointInfos[i].FilterProvider; EndpointFilterProvider jProvider = endpointInfos[j].FilterProvider; // if not default EndpointFilterProvider, we won't try to throw, you're on your own string commonAction; if (iProvider != null && jProvider != null && HaveCommonInitiatingActions(iProvider, jProvider, out commonAction)) { // you will definitely get a MultipleFiltersMatchedException at runtime, // so let's go ahead and throw now throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new InvalidOperationException( SR.Format(SR.SFxDuplicateInitiatingActionAtSameVia, endpointInfos[i].Endpoint.ListenUri, commonAction))); } } } } } } }
void IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) { IContractBehavior contractBehavior = this; contractBehavior.ApplyDispatchBehavior(null, serviceEndpoint, clientRuntime.CallbackDispatchRuntime); }