After some experiences I camed out to the following code. It takes care of finding the missing types used in an operation contract.
If you want to use this code, the only thing to do here, is to extend the assemblies list of all your assemblies that contain data contract classes, whitch are part of the operation contracts in use.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Runtime.Serialization; using System.Web; namespace Wellcode.Wello { /// <summary> /// data contract type collector /// </summary> public static class KnownTypeProvider { /// <summary> /// Collect types of all data contracts in all assemblies of /// interest. this are /// - WellFrame Assembly, where basic types are definied /// - Assembly, that contains generated entity types, used in the web service. /// </summary> /// <param name="provider"></param> /// <returns></returns> public static IEnumerable<Type> GetKnownTypes(ICustomAttributeProvider provider) { if (types != null) return types; types = new List<Type>(); var assemblies = new List<Assembly>(); assemblies.Add(typeof(Wellcode.Frame.WebEntity).Assembly); assemblies.Add(typeof(Wellcode.Wello.Document).Assembly); foreach (var assembly in assemblies) { foreach (var type in assembly.ExportedTypes) { var attr = type.GetCustomAttribute<DataContractAttribute>(false); if (attr != null && !type.IsGenericType) { types.Add(type); } } } return types; } /// <summary> /// static list of found types /// </summary> private static List<Type> types = null; } } |
the following example shows the using of the provider.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
namespace Wellcode.Wello { public partial interface IWelloService { /// <summary> /// get an entity 'AccingAccount' using its primary keys /// Table AccingAccount /// </summary> /// <returns>Entity 'AccingAccount'</returns> [OperationContract] [ServiceKnownType("GetKnownTypes", typeof(KnownTypeProvider))] AccingAccount GetAccingAccount(int accountId); } } |