/// <summary> /// Gets the set of <see cref="ContractDescription"/> that describe the available contracts of the service. /// </summary> /// <param name="serviceType">The service type.</param> /// <returns>An enumerable of <see cref="ContractDescription"/>.</returns> private static IEnumerable <ContractDescription> GetContracts(Type serviceType) { var collection = new ReflectedContractCollection(); //Type[] types = serviceType.GetInterfaces(); //foreach (var contract in types.Where(t => t != HostedServiceType)) //{ // if (!collection.Contains(contract)) // { // try // { // var cd = ContractDescription.GetContract(contract, serviceType); // collection.Add(cd); // } // catch (InvalidOperationException) // { // Debug.WriteLine("Warning: Contract " + contract + " can't found contract description in " + serviceType); // } // //foreach (var icd in cd.GetInheritedContracts()) // //{ // // if (!collection.Contains(icd.ContractType)) // // collection.Add(icd); // //} // } //} var attrs = (ExportServiceAttribute[])serviceType.GetCustomAttributes(typeof(ExportServiceAttribute), true); if (!attrs.Any()) { throw new InvalidOperationException("ExportServiceAttribute must be defined!"); } foreach (var contract in attrs) { if (!collection.Contains(contract.ContactType)) { try { var cd = ContractDescription.GetContract(contract.ContactType, serviceType); collection.Add(cd); } catch (InvalidOperationException) { Debug.WriteLine("Warning: Contract " + contract + " can't found contract description in " + serviceType); } } } return(collection); }
internal string GetConfigKey(Type implementedContract) { if (reflectedContracts.Contains(implementedContract)) { return(ReflectedContractCollection.GetConfigKey(reflectedContracts[implementedContract])); } //if (this.behaviors.Contains(typeof(ServiceMetadataBehavior)) && ServiceMetadataBehavior.IsMetadataImplementedType(implementedContract)) //{ // return ServiceMetadataBehavior.MexContractName; //} throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.SfxReflectedContractKeyNotFound2, implementedContract.FullName, string.Empty))); }
/// <summary> /// Gets the set of <see cref="ContractDescription"/> that describe the available contracts of the service. /// </summary> /// <param name="serviceType">The service type.</param> /// <returns>An enumerable of <see cref="ContractDescription"/>.</returns> private static IEnumerable <ContractDescription> GetContracts(Type serviceType) { var collection = new ReflectedContractCollection(); foreach (var contract in serviceType.GetInterfaces().Where(t => t != HostedServiceType)) { if (!collection.Contains(contract)) { var cd = ContractDescription.GetContract(contract, serviceType); collection.Add(cd); foreach (var icd in cd.GetInheritedContracts()) { if (!collection.Contains(icd.ContractType)) { collection.Add(icd); } } } } return(collection); }
protected override ServiceDescription CreateDescription(out IDictionary <string, ContractDescription> implementedContracts) { ServiceDescription description; TService instance = _serviceProvider.GetService <TService>(); if (instance != null) { description = ServiceDescription.GetService(instance); } else { description = ServiceDescription.GetService <TService>(); } // Any user supplied IServiceBehaviors can be applied now var serviceBehaviors = _serviceProvider.GetServices <IServiceBehavior>(); foreach (var behavior in serviceBehaviors) { description.Behaviors.Add(behavior); } ServiceBehaviorAttribute serviceBehavior = description.Behaviors.Find <ServiceBehaviorAttribute>(); object serviceInstanceUsedAsABehavior = serviceBehavior.GetWellKnownSingleton(); if (serviceInstanceUsedAsABehavior == null) { serviceInstanceUsedAsABehavior = serviceBehavior.GetHiddenSingleton(); _disposableInstance = serviceInstanceUsedAsABehavior as IDisposable; } if ((typeof(IServiceBehavior).IsAssignableFrom(typeof(TService)) || typeof(IContractBehavior).IsAssignableFrom(typeof(TService))) && serviceInstanceUsedAsABehavior == null) { if (instance == null) { serviceInstanceUsedAsABehavior = ServiceDescription.CreateImplementation <TService>(); } else { serviceInstanceUsedAsABehavior = instance; } _disposableInstance = serviceInstanceUsedAsABehavior as IDisposable; } if (instance != null) { if (serviceBehavior.InstanceContextMode == InstanceContextMode.Single) { SingletonInstance = instance; } else { serviceBehavior.InstanceProvider = new DependencyInjectionInstanceProvider(_serviceProvider, typeof(TService)); if (serviceInstanceUsedAsABehavior == null && instance is IDisposable disposable) { disposable.Dispose(); } } } if (instance == null) { if (serviceInstanceUsedAsABehavior is IServiceBehavior) { description.Behaviors.Add((IServiceBehavior)serviceInstanceUsedAsABehavior); } } ReflectedContractCollection reflectedContracts = new ReflectedContractCollection(); List <Type> interfaces = ServiceReflector.GetInterfaces <TService>(); for (int i = 0; i < interfaces.Count; i++) { Type contractType = interfaces[i]; if (!reflectedContracts.Contains(contractType)) { ContractDescription contract = null; if (serviceInstanceUsedAsABehavior != null) { contract = ContractDescription.GetContract <TService>(contractType, serviceInstanceUsedAsABehavior); } else { contract = ContractDescription.GetContract <TService>(contractType); } reflectedContracts.Add(contract); Collection <ContractDescription> inheritedContracts = contract.GetInheritedContracts(); for (int j = 0; j < inheritedContracts.Count; j++) { ContractDescription inheritedContract = inheritedContracts[j]; if (!reflectedContracts.Contains(inheritedContract.ContractType)) { reflectedContracts.Add(inheritedContract); } } } } ReflectedContracts = reflectedContracts; implementedContracts = reflectedContracts.ToImplementedContracts(); return(description); }
public ReflectedAndBehaviorContractCollection(ReflectedContractCollection reflectedContracts, KeyedByTypeCollection <IServiceBehavior> behaviors) { this.reflectedContracts = reflectedContracts; this.behaviors = behaviors; }
protected override System.ServiceModel.Description.ServiceDescription CreateDescription(out IDictionary <string, ContractDescription> implementedContracts) { System.ServiceModel.Description.ServiceDescription service; if (this.serviceType == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("SFxServiceHostCannotCreateDescriptionWithoutServiceType"))); } if (this.SingletonInstance != null) { service = System.ServiceModel.Description.ServiceDescription.GetService(this.SingletonInstance); } else { service = System.ServiceModel.Description.ServiceDescription.GetService(this.serviceType); } ServiceBehaviorAttribute attribute = service.Behaviors.Find <ServiceBehaviorAttribute>(); object wellKnownSingleton = attribute.GetWellKnownSingleton(); if (wellKnownSingleton == null) { wellKnownSingleton = attribute.GetHiddenSingleton(); this.disposableInstance = wellKnownSingleton as IDisposable; } if ((typeof(IServiceBehavior).IsAssignableFrom(this.serviceType) || typeof(IContractBehavior).IsAssignableFrom(this.serviceType)) && (wellKnownSingleton == null)) { wellKnownSingleton = System.ServiceModel.Description.ServiceDescription.CreateImplementation(this.serviceType); this.disposableInstance = wellKnownSingleton as IDisposable; } if ((this.SingletonInstance == null) && (wellKnownSingleton is IServiceBehavior)) { service.Behaviors.Add((IServiceBehavior)wellKnownSingleton); } ReflectedContractCollection contracts = new ReflectedContractCollection(); List <System.Type> interfaces = ServiceReflector.GetInterfaces(this.serviceType); for (int i = 0; i < interfaces.Count; i++) { System.Type key = interfaces[i]; if (!contracts.Contains(key)) { ContractDescription item = null; if (wellKnownSingleton != null) { item = ContractDescription.GetContract(key, wellKnownSingleton); } else { item = ContractDescription.GetContract(key, this.serviceType); } contracts.Add(item); Collection <ContractDescription> inheritedContracts = item.GetInheritedContracts(); for (int j = 0; j < inheritedContracts.Count; j++) { ContractDescription description3 = inheritedContracts[j]; if (!contracts.Contains(description3.ContractType)) { contracts.Add(description3); } } } } this.reflectedContracts = contracts; implementedContracts = contracts.ToImplementedContracts(); return(service); }
protected override ServiceDescription CreateDescription(out IDictionary <string, ContractDescription> implementedContracts) { ServiceDescription description = _serviceProvider.GetService <ServiceDescription <TService> >(); ServiceBehaviorAttribute serviceBehavior = description.Behaviors.Find <ServiceBehaviorAttribute>(); TService serviceInstanceUsedAsABehavior = (TService)serviceBehavior.GetWellKnownSingleton(); if (serviceInstanceUsedAsABehavior == null) { serviceInstanceUsedAsABehavior = (TService)serviceBehavior.GetHiddenSingleton(); _disposableInstance = serviceInstanceUsedAsABehavior as IDisposable; } // serviceInstanceUsedAsBehavior will be null when InstanceContextMode != Single // In this case, we need to check if the service type is a behavior and if it is, create an instance to apply to behaviors if ((typeof(IServiceBehavior).IsAssignableFrom(typeof(TService)) || typeof(IContractBehavior).IsAssignableFrom(typeof(TService))) && serviceInstanceUsedAsABehavior == null) { serviceInstanceUsedAsABehavior = _serviceProvider.GetService <TService>(); // First try DI to get an instance if (serviceInstanceUsedAsABehavior == null) // Not in DI so create the old WCF way using reflection { serviceInstanceUsedAsABehavior = ServiceDescription.CreateImplementation <TService>(); } _disposableInstance = serviceInstanceUsedAsABehavior as IDisposable; } if (serviceBehavior.InstanceContextMode == InstanceContextMode.Single) { serviceBehavior.ServicePovider = _serviceProvider; // If using Single, then ServiceBehavior fetched/created the instance and need to set on SingletonInstance Debug.Assert(serviceInstanceUsedAsABehavior != null, "Service behavior should have created a singleton instance"); SingletonInstance = serviceInstanceUsedAsABehavior; } else { serviceBehavior.InstanceProvider = new DependencyInjectionWithLegacyFallbackInstanceProvider(_serviceProvider, typeof(TService)); } if (serviceInstanceUsedAsABehavior is IServiceBehavior behavior) { description.Behaviors.Add(behavior); } ReflectedContractCollection reflectedContracts = new ReflectedContractCollection(); List <Type> interfaces = ServiceReflector.GetInterfaces <TService>(); for (int i = 0; i < interfaces.Count; i++) { Type contractType = interfaces[i]; if (!reflectedContracts.Contains(contractType)) { ContractDescription contract; if (serviceInstanceUsedAsABehavior != null) { contract = ContractDescription.GetContract <TService>(contractType, serviceInstanceUsedAsABehavior); } else { contract = ContractDescription.GetContract <TService>(contractType); } reflectedContracts.Add(contract); Collection <ContractDescription> inheritedContracts = contract.GetInheritedContracts(); for (int j = 0; j < inheritedContracts.Count; j++) { ContractDescription inheritedContract = inheritedContracts[j]; if (!reflectedContracts.Contains(inheritedContract.ContractType)) { reflectedContracts.Add(inheritedContract); } } } } ReflectedContracts = reflectedContracts; implementedContracts = reflectedContracts.ToImplementedContracts(); return(description); }
protected override ServiceDescription CreateDescription(out IDictionary <string, ContractDescription> implementedContracts) { ServiceDescription description; if (SingletonInstance != null) { description = ServiceDescription.GetService(SingletonInstance); } else { description = ServiceDescription.GetService <TService>(); } ServiceBehaviorAttribute serviceBehavior = description.Behaviors.Find <ServiceBehaviorAttribute>(); TService serviceInstanceUsedAsABehavior = (TService)serviceBehavior.GetWellKnownSingleton(); if (serviceInstanceUsedAsABehavior == null) { serviceInstanceUsedAsABehavior = (TService)serviceBehavior.GetHiddenSingleton(); _disposableInstance = serviceInstanceUsedAsABehavior as IDisposable; } if ((typeof(IServiceBehavior).IsAssignableFrom(typeof(TService)) || typeof(IContractBehavior).IsAssignableFrom(typeof(TService))) && serviceInstanceUsedAsABehavior == null) { serviceInstanceUsedAsABehavior = ServiceDescription.CreateImplementation <TService>(); _disposableInstance = serviceInstanceUsedAsABehavior as IDisposable; } if (SingletonInstance == null) { if (serviceInstanceUsedAsABehavior is IServiceBehavior) { description.Behaviors.Add((IServiceBehavior)serviceInstanceUsedAsABehavior); } } ReflectedContractCollection reflectedContracts = new ReflectedContractCollection(); List <Type> interfaces = ServiceReflector.GetInterfaces <TService>(); for (int i = 0; i < interfaces.Count; i++) { Type contractType = interfaces[i]; if (!reflectedContracts.Contains(contractType)) { ContractDescription contract = null; if (serviceInstanceUsedAsABehavior != null) { contract = ContractDescription.GetContract(contractType, serviceInstanceUsedAsABehavior); } else { contract = ContractDescription.GetContract <TService>(contractType); } reflectedContracts.Add(contract); Collection <ContractDescription> inheritedContracts = contract.GetInheritedContracts(); for (int j = 0; j < inheritedContracts.Count; j++) { ContractDescription inheritedContract = inheritedContracts[j]; if (!reflectedContracts.Contains(inheritedContract.ContractType)) { reflectedContracts.Add(inheritedContract); } } } } ReflectedContracts = reflectedContracts; implementedContracts = reflectedContracts.ToImplementedContracts(); return(description); }