zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    用 ArcMap 发布 ArcGIS Server FeatureServer Feature Access 服务 PostgreSQL 版本
    ArcMap 发布 ArcGIS Server OGC(WMSServer,MapServer)服务
    ArcScene 创建三维模型数据
    ArcMap 导入自定义样式Symbols
    ArcMap 导入 CGCS2000 线段数据
    ArcMap 导入 CGCS2000 点坐标数据
    ArcGis Server manager 忘记用户名和密码
    The view or its master was not found or no view engine supports the searched locations
    python小记(3)操作文件
    pytest(2) pytest与unittest的区别
  • 原文地址:https://www.cnblogs.com/kexb/p/6437618.html
Copyright © 2011-2022 走看看