wcf已知类型 known type

1.服务契约的定义
/* Copyright (c) 2014 HaiHui Software Co., Ltd. All rights reserved
 * 
 * Create by  huanglc@holworth.com at 2014-10-14 10:09:00
 *
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Framework;
using System.Collections;
using Contract.Domain;

namespace Contract.IService
{

    ///<summary>
    ///协议内容 Interface
    ///</summary>
    [ServiceKnownType("GetKnownTypes", typeof(Framework.IService.KnownTypesProvider))]
    [ServiceContract]
    public interface IBasAgreementService : IEntityService<BasAgreement>
    {

    }

}
2.数据提供者 provider 解析的作用,你就知道哪些东西是属于服务的范畴,哪些属于数据的范畴
#if !NET_2_0
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;

namespace Framework.IService
{
    public static class KnownTypesProvider
    {
        static object syncObj = new object();
        public static Type[] GetKnownTypes(ICustomAttributeProvider attributeTarget)
        {
            //get contract types from service assembly:DataContractAttribute="Contract.IService.ITestService"
            Type serviceContractType = (Type)attributeTarget;
            Assembly callerAssembly = serviceContractType.Assembly;

            if (KnowAssemblys.Count == 0)//First Init
                LoadAppDomainTypes();
            GetTypeFromAssembly(callerAssembly);
            return KnowTypes.ToArray();
        }


        public static Type[] GetKnownTypes()
        {
            Assembly callerAssembly = Assembly.GetCallingAssembly();

            if (KnowAssemblys.Count == 0)//First Init
                LoadAppDomainTypes();
            GetTypeFromAssembly(callerAssembly);
            return KnowTypes.ToArray();
        }

        private static void LoadAppDomainTypes()
        {
            if (KnowAssemblys.Count == 0)//First Init
            {
                //CurrentDomain
                List<Assembly> typeAssemblys = new List<Assembly>();
                Assembly[] domainAssemblys = AppDomain.CurrentDomain.GetAssemblies();
                foreach (Assembly asm in domainAssemblys)
                    if (asm.FullName.IndexOf("Contract") > -1 || asm.FullName.IndexOf("Framework,") > -1 || asm.FullName.IndexOf("Domain")>-1)//IsContract?
                        typeAssemblys.Add(asm);

                //Type serializableType = typeof(SerializableAttribute);
                foreach (Assembly typeAssembly in typeAssemblys)
                {
                    GetTypeFromAssembly(typeAssembly);
                }
            }
        }

        static Type dataContractType = typeof(DataContractAttribute);
        private static void GetTypeFromAssembly(Assembly callerAssembly)
        {
            if (!KnowAssemblys.Contains(callerAssembly.FullName))//Not loaded
            {
                lock (syncObj)
                {
                    if (!KnowAssemblys.Contains(callerAssembly.FullName))//Not loaded(DOUBLE CHECK)
                    {
                        //filter by DataContractAttribute
                        Type[] exportedTypes = callerAssembly.GetExportedTypes();

                        foreach (Type type in exportedTypes)
                            if (Attribute.IsDefined(type, dataContractType, false))// || Attribute.IsDefined(type, serializableType, false))
                                //if (type.Namespace.IndexOf("Contract.")==0)
                                KnowTypes.Add(type);

                        KnowAssemblys.Add(callerAssembly.FullName);
                    }
                }
            }
        }

        private static List<Type> knowTypes;
        private static List<Type> KnowTypes
        {
            get
            {
                if (knowTypes == null)
                {
                    lock (syncObj)
                    {
                        if (knowTypes == null)
                        {
                            knowTypes = new List<Type>();

                            //bug fixed!
                            knowTypes.Add(typeof(string[]));
                            knowTypes.Add(typeof(object[]));
                            knowTypes.Add(typeof(System.Collections.Hashtable));
                            knowTypes.Add(typeof(System.Data.DataSet));
                        }
                    }
                }
                return knowTypes;
            }
        }

        private static List<String> knowAssemblys;
        private static List<String> KnowAssemblys
        {
            get
            {
                if (knowAssemblys == null)
                {
                    lock (syncObj)
                    {
                        if (knowAssemblys == null)
                            knowAssemblys = new List<String>();
                    }
                }
                return knowAssemblys;
            }
        }
    }
}
#endif
原文地址:https://www.cnblogs.com/kexb/p/6437618.html