internal static Type GetContractTypeAndAttribute(Type interfaceType, out ServiceContractAttribute contractAttribute) { contractAttribute = GetSingleAttribute <ServiceContractAttribute>(interfaceType); if (contractAttribute != null) { return(interfaceType); } List <Type> list = new List <Type>(GetInheritedContractTypes(interfaceType)); if (list.Count == 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("AttemptedToGetContractTypeForButThatTypeIs1", new object[] { interfaceType.Name }))); } foreach (Type type in list) { bool flag = true; foreach (Type type2 in list) { if (!type2.IsAssignableFrom(type)) { flag = false; } } if (flag) { contractAttribute = GetSingleAttribute <ServiceContractAttribute>(type); return(type); } } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("SFxNoMostDerivedContract", new object[] { interfaceType.Name }))); }
public void CreatesServiceContractAttributeWithNoDecoratedClassAndFullConfig() { se.ObjectName = "CreatesServiceContractAttributeWithNoDecoratedClassAndFullConfig"; se.TargetName = "service"; se.CallbackContract = typeof(IDisposable); se.ConfigurationName = "CustomConfigName"; se.Name = "serviceName"; se.Namespace = "http://Spring.Services.Tests"; se.ProtectionLevel = ProtectionLevel.Sign; se.SessionMode = SessionMode.Required; se.AfterPropertiesSet(); Type proxyType = se.GetObject() as Type; Assert.IsNotNull(proxyType); object[] attrs = proxyType.GetCustomAttributes(typeof(ServiceContractAttribute), true); Assert.IsNotEmpty(attrs); Assert.AreEqual(1, attrs.Length); ServiceContractAttribute sca = attrs[0] as ServiceContractAttribute; Assert.AreEqual(se.CallbackContract, sca.CallbackContract); Assert.AreEqual(se.ConfigurationName, sca.ConfigurationName); Assert.AreEqual(se.Name, sca.Name); Assert.AreEqual(se.Namespace, sca.Namespace); Assert.AreEqual(se.ProtectionLevel, sca.ProtectionLevel); Assert.AreEqual(se.SessionMode, sca.SessionMode); }
public static string[] GetOperations(string mexAddress, Type contractType) { if (contractType.IsInterface == false) { Debug.Assert(false, contractType + " is not an interface"); return(new string[] { }); } object[] attributes = contractType.GetCustomAttributes(typeof(ServiceContractAttribute), false); if (attributes.Length == 0) { Debug.Assert(false, "Interface " + contractType + " does not have the ServiceContractAttribute"); return(new string[] { }); } ServiceContractAttribute attribute = attributes[0] as ServiceContractAttribute; if (attribute.Name == null) { attribute.Name = contractType.ToString(); } if (attribute.Namespace == null) { attribute.Namespace = "http://tempuri.org/"; } return(GetOperations(mexAddress, attribute.Namespace, attribute.Name)); }
static Type GenerateInterfaceServiceType(Type type, Type inter, List <Type> assemblyTypes, bool isServer) { if (!type.IsInterface) { throw new Exception("type must be interface"); } var attribs = type.GetCustomAttributes(false); bool isServiceContract = false; ServiceContractAttribute attrib = null; foreach (var item in attribs) { if (item.GetType() == typeof(ServiceContractAttribute)) { attrib = (ServiceContractAttribute)item; isServiceContract = true; break; } } if (!isServiceContract) { throw new Exception("your class is not used ServiceContractAttribute"); } return(GenerateType(type, attrib.Name, inter, assemblyTypes, isServer)); }
static internal Type GetContractTypeAndAttribute(Type interfaceType, out ServiceContractAttribute contractAttribute) { contractAttribute = GetSingleAttribute <ServiceContractAttribute>(interfaceType); if (contractAttribute != null) { return(interfaceType); } List <Type> types = new List <Type>(GetInheritedContractTypes(interfaceType)); if (types.Count == 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.AttemptedToGetContractTypeForButThatTypeIs1, interfaceType.Name))); } foreach (Type potentialContractRoot in types) { bool mayBeTheRoot = true; foreach (Type t in types) { if (!t.IsAssignableFrom(potentialContractRoot)) { mayBeTheRoot = false; } } if (mayBeTheRoot) { contractAttribute = GetSingleAttribute <ServiceContractAttribute>(potentialContractRoot); return(potentialContractRoot); } } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( SR.Format(SR.SFxNoMostDerivedContract, interfaceType.Name))); }
/// <summary> /// Finds the namespace for a proxy type. /// </summary> /// <param name="proxyType">The proxy type to get the namespace for.</param> /// <returns>The namespace of the proxy type.</returns> private string FindTypeNamespace(Type proxyType) { string ans = null; Attribute[] attributes = Attribute.GetCustomAttributes(proxyType, true); ////proxyType.Assembly.GetCustomAttributes(proxyType, false); bool found = false; foreach (Attribute a in attributes) { ServiceContractAttribute attr = a as ServiceContractAttribute; if (attr != null) { ans = attr.Namespace; found = true; break; } } // Check inheritance hierarchy for other places if (!found) { Type[] interfaces = proxyType.GetInterfaces(); foreach (Type t in interfaces) { ans = this.FindTypeNamespace(t); if (ans != null) { break; } } } return(ans); }
private string GetServiceNameByCaption(string caption) { if (!CurrentBotStructureInfo.InitializeServicesFromAttributes) { if (Services.TryGetValue(caption, out Type service)) { ServiceContractAttribute serviceAttribute = service.GetCustomAttribute <ServiceContractAttribute>(); return(serviceAttribute.Name); } else { return(caption); } } foreach (Type service in Services.Values) { ServiceContractAttribute serviceAttribute = service.GetCustomAttribute <ServiceContractAttribute>(); BotDisplayNameAttribute nameAttribute = service.GetCustomAttribute <BotDisplayNameAttribute>(); if (nameAttribute == null) { continue; } else if (nameAttribute.Content.Equals(caption, StringComparison.OrdinalIgnoreCase)) { return(serviceAttribute.Name); } } return(caption); }
private static ServiceContractAttribute ConvertFromServiceModelServiceContractAttribute(object attr) { Fx.Assert(attr.GetType().FullName.Equals(ServiceReflector.SMServiceContractAttributeFullName), "Expected attribute of type S.SM.ServiceContractAttribute"); bool hasProtectionLevel = GetProperty <bool>(attr, "HasProtectionLevel"); if (hasProtectionLevel) { // ProtectionLevel isn't supported yet so if it was set on the S.SM.SCA, then we can't do the mapping so throw throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new PlatformNotSupportedException("System.ServiceModel.ServiceContractAttribute.ProtectionLevel")); } var sca = new ServiceContractAttribute(); string tmpStr = GetProperty <string>(attr, nameof(ServiceContractAttribute.ConfigurationName)); if (!string.IsNullOrEmpty(tmpStr)) { sca.ConfigurationName = tmpStr; } tmpStr = GetProperty <string>(attr, nameof(ServiceContractAttribute.Name)); if (!string.IsNullOrEmpty(tmpStr)) { sca.Name = tmpStr; } sca.Namespace = GetProperty <string>(attr, nameof(ServiceContractAttribute.Namespace)); sca.SessionMode = GetProperty <SessionMode>(attr, nameof(ServiceContractAttribute.SessionMode)); sca.CallbackContract = GetProperty <Type>(attr, nameof(ServiceContractAttribute.CallbackContract)); return(sca); }
public ServiceTypeMetadata AddServiceType(Type serviceType) { if (!serviceType.IsClass || serviceType.IsInterface || serviceType.IsAbstract) { throw new ArgumentException("types that are not supported."); } if (!typeof(IService).IsAssignableFrom(serviceType)) { throw new ArgumentException("the IService interface is not implemented."); } Type interfaceType = null; ServiceContractAttribute serviceContract = null; foreach (var type in serviceType.GetInterfaces()) { serviceContract = type.GetCustomAttribute <ServiceContractAttribute>(); if (serviceContract != null) { interfaceType = type; break; } } if (serviceContract == null) { throw new ArgumentException("not contract interface type."); } var serviceTypeData = CreateServiceTypeMetadata(serviceType, interfaceType); serviceTypes[serviceType] = serviceTypeData; return(serviceTypeData); }
private string GetAction(ServiceContractAttribute serviceContractAttr, string typeName, OperationContractAttribute operationContractAttr, string methodName) { string action = operationContractAttr.Action; if (String.IsNullOrEmpty(action)) { string ns = serviceContractAttr.Namespace; string serviceContractName = serviceContractAttr.Name; string opName = operationContractAttr.Name; if (String.IsNullOrEmpty(ns)) { ns = DefaultServiceContractNS; } if (String.IsNullOrEmpty(serviceContractName)) { serviceContractName = typeName; } if (String.IsNullOrEmpty(opName)) { opName = methodName; } action = String.Format("{0}/{1}/{2}", ns, serviceContractName, opName); } return(action); }
private static Type GetServiceContractType(string contractName, string contractNamespace, Type[] assemblyTypes) { for (int i = 0; i < assemblyTypes.Length; i++) { Type type = assemblyTypes[i]; if (!type.IsInterface) { continue; } ServiceContractAttribute serviceContractAttribute = ReflectionUtils.GetCustomAttribute <ServiceContractAttribute>(type); if (serviceContractAttribute == null) { continue; } XmlQualifiedName xmlQualifiedServiceContractName = GetServiceContractName(type, serviceContractAttribute.Name, serviceContractAttribute.Namespace); if (string.Compare(xmlQualifiedServiceContractName.Name, contractName, StringComparison.OrdinalIgnoreCase) == 0 && string.Compare(xmlQualifiedServiceContractName.Namespace, contractNamespace, StringComparison.OrdinalIgnoreCase) == 0) { return(type); } } return(null); }
public ContractDescription(ServiceDescription service, Type contractType, ServiceContractAttribute attribute) { Service = service; ContractType = contractType; Namespace = attribute.Namespace ?? "http://tempuri.org/"; // Namespace defaults to http://tempuri.org/ Name = attribute.Name ?? ContractType.Name; // Name defaults to the type name _operationsLazy = new Lazy <IEnumerable <OperationDescription> >(ExtractOperationDescriptions); }
public OperationDescriptor(string action, string name, string ns, MethodInfo method, OperationContractAttribute contract, ServiceContractAttribute serviceContract) { Action = action; Name = name; Namespace = ns; Method = method; Contract = contract; ServiceContract = serviceContract; }
private void BuildService(Type type, ServiceContractAttribute sc) { ServiceDescription sd = new ServiceDescription(); sd.IType = type; if (!string.IsNullOrEmpty(sc.Name)) { sd.Name = sc.Name; } Services.Add(sd); }
/// <summary> /// get buttons of all services /// </summary> /// <returns></returns> private List <List <BotButtonInfo> > GetListOfServicesButtons(TelegramClientInfo clientInfo) { List <BotButtonInfo> columns = new List <BotButtonInfo>(); List <List <BotButtonInfo> > rows = new List <List <BotButtonInfo> >(); int columnIndex = 0; List <System.Type> services = _serverBase.GetListOfRegistredTypes().ToList(); for (int i = 0; i < services.Count; i++) { System.Type item = services[i]; ServiceContractAttribute attribute = item.GetCustomAttribute <ServiceContractAttribute>(); if (attribute.ServiceType != ServiceType.HttpService) { continue; } string serviceName = ""; if (CurrentBotStructureInfo.InitializeServicesFromAttributes) { BotDisplayNameAttribute nameAttribute = item.GetCustomAttribute <BotDisplayNameAttribute>(); if (nameAttribute == null) { continue; } //serviceName = nameAttribute.Content; } serviceName = attribute.Name; if (!CurrentBotStructureInfo.OnServiceGenerating(serviceName, clientInfo)) { continue; } if (!Services.ContainsKey(attribute.Name)) { Services.Add(serviceName, item); } if (columnIndex == 3) { columnIndex = 0; rows.Add(columns.ToList()); columns.Clear(); } columns.Add(GetServiceCaption(item)); columnIndex++; } if (rows.Count == 0) { rows.Add(columns); } return(rows); }
private static void CheckObjectMode(Type type, ref WellKnownObjectModeEnum targetMode, ref bool foundFlag) { if (type.GetInterfaces().Length > 0) { foreach (Type interfaceClass in type.GetInterfaces()) { ServiceContractAttribute scAnnotation = TypeHelper.GetAttribute <ServiceContractAttribute>(interfaceClass); if (scAnnotation != null) { // van attribútum if (!foundFlag) { // meg van az első WellKnownObjectMode, ez lesz a referencia a többi számára targetMode = scAnnotation.WellKnownObjectMode; foundFlag = true; } else if (!targetMode.Equals(scAnnotation.WellKnownObjectMode)) { // eltérő throw new InvalidProxyImplementationException(String.Format("Different {0} definitions found on type '{1}'. Contract '{2}' configured to '{3}' and previously found '{4}' mode.", typeof(WellKnownObjectModeEnum).FullName, type.FullName, interfaceClass.FullName, scAnnotation.WellKnownObjectMode, targetMode.ToString())); } } if (!foundFlag) { // még nincs referencia WellKnownObjectMode CheckObjectMode(interfaceClass, ref targetMode, ref foundFlag); } else { // már van WellKnownObjectModeEnum interfaceMode = WellKnownObjectModeEnum.PerSession; bool tempFound = false; CheckObjectMode(interfaceClass, ref interfaceMode, ref tempFound); if (tempFound && foundFlag && !targetMode.Equals(interfaceMode)) { // eltérő throw new InvalidProxyImplementationException(String.Format("Different {0} definitions found on type '{1}'. Contract '{2}' configured to '{3}' and previously found '{4}' mode.", typeof(WellKnownObjectModeEnum).FullName, type.FullName, interfaceClass.FullName, interfaceMode.ToString(), targetMode.ToString())); } if (tempFound && !foundFlag) { // meg van az első WellKnownObjectMode, referencia mentése targetMode = interfaceMode; foundFlag = true; } } } } if (type.BaseType != null && !type.BaseType.Equals(typeof(ProxyBase)) && !type.BaseType.Equals(typeof(Object))) { CheckObjectMode(type.BaseType, ref targetMode, ref foundFlag); } }
public bool Build(IAGMServiceInstanceConfiguration instanceConfiguration, ServiceContractAttribute serviceContractAttribute, ServiceOperation serviceOperation, ServiceOperationFieldAttribute attribute, Type type, IMethodParameterBuilder paramBuilder) { paramBuilder.SetAgmType(AgmValueType.Tuple).SetSchema(schemaBuilder => schemaBuilder .AddParameter("top", AgmValueType.Int) .AddParameter("left", AgmValueType.Int) .AddParameter("width", AgmValueType.Int) .AddParameter("height", AgmValueType.Int)); return(true); }
private Type GetContractType(string contractName, string contractNamespace) { Type[] allTypes = proxyAssembly.GetTypes(); ServiceContractAttribute scAttr = null; Type contractType = null; XmlQualifiedName cName; foreach (Type type in allTypes) { // Is it an interface? if (!type.IsInterface) { continue; } // Is it marked with ServiceContract attribute? object[] attrs = type.GetCustomAttributes( typeof(ServiceContractAttribute), false); if ((attrs == null) || (attrs.Length == 0)) { continue; } // is it the required service contract? scAttr = (ServiceContractAttribute)attrs[0]; cName = GetContractName(type, scAttr.Name, scAttr.Namespace); if (string.Compare(cName.Name, contractName, true) != 0) { continue; } if (string.Compare(cName.Namespace, contractNamespace, true) != 0) { continue; } contractType = type; break; } if (contractType == null) { throw new ArgumentException( Constants.ErrorMessages.UnknownContract); } return(contractType); }
/// <summary> /// 检查一个类定义中是否有WCF contract行为类型定义,如果有返回类型,否则返回null /// </summary> /// <param name="type">待检查的类型定义</param> /// <returns></returns> private bool CheckIsWCFContract(Type type) { object[] customAttributies = type.GetCustomAttributes(false); foreach (object att in customAttributies) { ServiceContractAttribute serviceContractAttribute = att as ServiceContractAttribute; if (null == serviceContractAttribute) { continue; //it is not wcf contract } return(true); } return(false); }
static ContractDescription CreateContractDescription(Type serviceType, string configurationName) { foreach (Type contract in serviceType.GetInterfaces()) { ServiceContractAttribute serviceContractAttribute = contract.GetCustomAttributes(typeof(ServiceContractAttribute), false).FirstOrDefault() as ServiceContractAttribute; if (null != serviceContractAttribute) { string configName = serviceContractAttribute.ConfigurationName ?? contract.Namespace + "." + contract.Name; if (configurationName == configName) { return(ContractDescription.GetContract(contract, serviceType)); } } } return(null); }
public void TestHeaderGeneration() { ProjectMappingManagerSetup.InitializeManager(ServiceProvider, "ProjectMapping.ServiceContractDsl.Tests.xml"); ServiceContract rootElement = CreateRoot(ServiceContractElementName, ServiceContractElementNamespace); string content = RunTemplate(rootElement); Type generatedType = CompileAndGetType(content); Assert.AreEqual <string>("I" + ServiceContractElementName, generatedType.Name); Assert.IsTrue(generatedType.IsInterface); ServiceContractAttribute serviceContract = TypeAsserter.AssertAttribute <ServiceContractAttribute>(generatedType); Assert.AreEqual <string>(ServiceContractElementName, serviceContract.Name); Assert.AreEqual <string>(ServiceContractElementNamespace, serviceContract.Namespace); Assert.AreEqual(ProtectionLevel.None, serviceContract.ProtectionLevel); }
public ContractDescription(ServiceDescription service, Type contractType, ServiceContractAttribute attribute) { Service = service; ContractType = contractType; Namespace = attribute.Namespace ?? "http://tempuri.org/"; Name = attribute.Name ?? ContractType.Name; // Name defaults to the type name var operations = new List<OperationDescription>(); foreach (var operationMethodInfo in ContractType.GetTypeInfo().DeclaredMethods) { foreach (var operationContract in operationMethodInfo.GetCustomAttributes<OperationContractAttribute>()) { operations.Add(new OperationDescription(this, operationMethodInfo, operationContract)); } } Operations = operations; }
public void OverridesExistingServiceContractAttributeWithDecoratedClass() { se.ObjectName = "OverridesExistingServiceContractAttributeWithDecoratedClass"; se.TargetName = "decoratedService"; se.AfterPropertiesSet(); Type proxyType = se.GetObject() as Type; Assert.IsNotNull(proxyType); object[] attrs = proxyType.GetCustomAttributes(typeof(ServiceContractAttribute), true); Assert.IsNotEmpty(attrs); Assert.AreEqual(1, attrs.Length); ServiceContractAttribute sca = attrs[0] as ServiceContractAttribute; Assert.IsNull(sca.Namespace); }
/// <summary> /// Query and gets back the WellKnownObjectMode type of the provided contract /// </summary> /// <param name="type">The type.</param> /// <param name="result">The result.</param> /// <returns>True, if the provided type found, otherwise False.</returns> public static bool GetWellKnownObjectMode(Type type, out WellKnownObjectModeEnum result) { if (type == null) { ThrowHelper.ThrowArgumentNullException("type"); } bool found = false; result = WellKnownObjectModeEnum.PerSession; ServiceContractAttribute scAnnotation = TypeHelper.GetAttribute <ServiceContractAttribute>(type); if (scAnnotation != null) { result = scAnnotation.WellKnownObjectMode; found = true; } if (scAnnotation == null && type.GetInterfaces().Length > 0) { foreach (Type interfaceClass in type.GetInterfaces()) { scAnnotation = TypeHelper.GetAttribute <ServiceContractAttribute>(interfaceClass); if (scAnnotation != null) { result = scAnnotation.WellKnownObjectMode; found = true; } else { found = GetWellKnownObjectMode(interfaceClass, out result); } if (found) { break; } } } if (!found && type.BaseType != null && !type.BaseType.Equals(typeof(ProxyBase)) && !type.BaseType.Equals(typeof(Object))) { found = GetWellKnownObjectMode(type.BaseType, out result); } return(found); }
internal static void VerifyCallback() { Type contractType = typeof(T); Type callbackType = typeof(C); object[] attributes = contractType.GetCustomAttributes(typeof(ServiceContractAttribute), false); if (attributes.Length != 1) { throw new InvalidOperationException("Type of " + contractType + " is not a service contract"); } ServiceContractAttribute serviceContractAttribute = attributes[0] as ServiceContractAttribute; if (callbackType != serviceContractAttribute.CallbackContract) { throw new InvalidOperationException("Type of " + callbackType + " is not configured as callback contract for " + contractType); } }
public void RegisterClientService(Type serviceType) { ServiceContractAttribute service = serviceType.GetClientServiceAttribute(); if (service != null) { string name = service.GetServiceName(false).ToLower(); if (!RegisteredServiceTypes.ContainsKey(name)) { RegisteredServiceTypes.TryAdd(name, serviceType); } } else { throw new NotSupportedException("your service is not type of ServerService or HttpService or StreamService"); } }
public void TestWFCHeaderGeneration() { ProjectMappingManagerSetup.InitializeManager(ServiceProvider, "ProjectMapping.ServiceContractDsl.Tests.xml"); ServiceContract rootElement = CreateRoot(ServiceContractElementName, ServiceContractElementNamespace); WCFServiceContract extender = new WCFServiceContract(); extender.SessionMode = SessionMode.Required; extender.ModelElement = rootElement; rootElement.ObjectExtender = extender; string content = RunTemplate(rootElement); Type generatedType = CompileAndGetType(content); ServiceContractAttribute serviceContract = TypeAsserter.AssertAttribute <ServiceContractAttribute>(generatedType); Assert.AreEqual <string>(ServiceContractElementName, serviceContract.Name); Assert.AreEqual <string>(ServiceContractElementNamespace, serviceContract.Namespace); Assert.AreEqual <SessionMode>(SessionMode.Required, serviceContract.SessionMode); }
private string GetServiceCaption(Type service) { ServiceContractAttribute serviceAttribute = service.GetCustomAttribute <ServiceContractAttribute>(); if (CurrentBotStructureInfo.InitializeServicesFromAttributes) { BotDisplayNameAttribute nameAttribute = service.GetCustomAttribute <BotDisplayNameAttribute>(); if (nameAttribute == null) { return(serviceAttribute.Name); } return(nameAttribute.Content); } else { return(serviceAttribute.Name); } }
public static string GetMessageNamespace(this Type type) { ServiceContractAttribute attribute = type.GetCustomAttribute <ServiceContractAttribute>(); string name; if (attribute == null || string.IsNullOrEmpty(attribute.Name)) { name = type.Name; } else { name = attribute.Name; } string ns = type.Namespace; return(ns + "." + name); }
private static Type GenerateInterfaceServiceType(Type type, Type inter, List <Type> assemblyTypes, bool isServer) { if (!type.IsInterface) { throw new Exception("type must be interface"); } IEnumerable <ServiceContractAttribute> attribs = type.GetCustomAttributes <ServiceContractAttribute>(true).Where(x => x.ServiceType == ServiceType.ServerService || x.ServiceType == ServiceType.ClientService); bool isServiceContract = false; ServiceContractAttribute attrib = attribs.FirstOrDefault(); isServiceContract = attrib != null; if (!isServiceContract) { throw new Exception("your class is not used ServiceContractAttribute that have ServiceType.SeverService"); } return(GenerateType(type, attrib.Name, inter, assemblyTypes, isServer)); }