private static void DefineServiceKnownTypesAttribute(ServiceKnownTypeAttribute knownTypeSource, TypeBuilder asyncInterfaceBuilder) { CustomAttributeBuilder knownTypesAttribute = null; if (!string.IsNullOrEmpty(knownTypeSource.MethodName) && knownTypeSource.DeclaringType != null) { knownTypesAttribute = CreateAttribute <ServiceKnownTypeAttribute>( new[] { typeof(string), typeof(Type) }, new object[] { knownTypeSource.MethodName, knownTypeSource.DeclaringType }); } else if (knownTypeSource.Type != null) { knownTypesAttribute = CreateAttribute <ServiceKnownTypeAttribute>( new[] { typeof(Type) }, new object[] { knownTypeSource.Type }); } else if (!string.IsNullOrEmpty(knownTypeSource.MethodName)) { knownTypesAttribute = CreateAttribute <ServiceKnownTypeAttribute>( new[] { typeof(string) }, new object[] { knownTypeSource.MethodName }); } if (knownTypesAttribute == null) { throw new InvalidOperationException("Could not find matching ConstructorInfo"); } asyncInterfaceBuilder.SetCustomAttribute(knownTypesAttribute); }
public void ShouldGenerateKnownTypeAttributeWithXsdExtendedTypes() { ProjectMappingManagerSetup.InitializeManager(ServiceProvider, "ProjectMapping.ServiceContractDsl.Tests.xml"); ServiceContract rootElement = CreateRoot(ServiceContractElementName, ServiceContractElementNamespace); rootElement.ServiceContractModel.ImplementationTechnology = new ServiceContractWCFExtensionProvider(); rootElement.ServiceContractModel.SerializerType = Microsoft.Practices.ServiceFactory.ServiceContracts.SerializerType.DataContractSerializer; Operation op1 = new Operation(Store); op1.ObjectExtender = new WCFOperationContract(); op1.Name = "op1"; op1.Action = "op1"; op1.ServiceContract = rootElement; XsdMessage request = new XsdMessage(Store); request.Name = "Request1"; request.Element = @"xsd://SampleData/BaseTypes.xsd?LandmarkPoint"; request.ServiceContractModel = rootElement.ServiceContractModel; WCFXsdMessageContract wcfXsdMc = new WCFXsdMessageContract(); wcfXsdMc.ModelElement = request; request.ObjectExtender = wcfXsdMc; op1.Request = request; string content = RunTemplate(rootElement); EnsureType(ref content, "Request1"); EnsureType(ref content, "LandmarkBase"); Type generatedType = CompileAndGetType(content); MethodInfo method = TypeAsserter.AssertMethod(op1.Name, generatedType); ServiceKnownTypeAttribute attribute = TypeAsserter.AssertAttribute <ServiceKnownTypeAttribute>(method); Assert.AreEqual <string>("LandmarkBase", attribute.Type.Name); }
/// <summary> /// Gets the service known types. /// </summary> /// <param name="contractType">The contract types.</param> /// <returns>Service known types.</returns> private static IList <Type> GetServiceKnownTypes(Type contractType) { List <Type> ans = new List <Type>(); object[] attrs = contractType.GetCustomAttributes(typeof(ServiceKnownTypeAttribute), false); foreach (object attr in attrs) { ServiceKnownTypeAttribute skta = (ServiceKnownTypeAttribute)attr; ans.Add(skta.Type); } return(ans); }
//SPRNET-1262 public void ProxiesDeclarativeAttributeWithConstructorArguments() { XmlObjectFactory objectFactory = null; const string xml = @"<?xml version='1.0' encoding='UTF-8' ?> <objects xmlns='http://www.springframework.net'> <object id='theService' type='Spring.ServiceModel.ServiceExporterTests+Service, Spring.Services.Tests'/> <object id='service' type='Spring.ServiceModel.ServiceExporter, Spring.Services'> <property name='TargetName' value='theService'/> <property name='TypeAttributes'> <list> <object type='System.ServiceModel.ServiceKnownTypeAttribute, System.ServiceModel' abstract='true'> <constructor-arg name='type' value='System.Collections.Generic.List<int>' /> </object> </list> </property> </object> </objects>"; using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(xml))) { objectFactory = new XmlObjectFactory(new InputStreamResource(stream, string.Empty)); } Type proxyType = (Type)objectFactory.GetObject("service"); object[] attrs = proxyType.GetCustomAttributes(false); ServiceKnownTypeAttribute attrib = null; //loop thru all retreived attributes, attempting to find the one that is of the expected type for (int i = 0; i < attrs.Length; i++) { attrib = attrs[i] as ServiceKnownTypeAttribute; //break out of the loop once we find the one we want if (attrib != null) { break; } } Assert.NotNull(attrib, String.Format("No attributes of the expecte type {0} were found.", typeof(ServiceKnownTypeAttribute))); Assert.AreEqual(typeof(List <int>), attrib.Type, "Property 'Type' on Target Attribute not set!"); }