wcf已知类型 known type
Posted 听哥哥的话
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了wcf已知类型 known type相关的知识,希望对你有一定的参考价值。
1.服务契约的定义 /* Copyright (c) 2014 HaiHui Software Co., Ltd. All rights reserved * * Create by [email protected] 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
以上是关于wcf已知类型 known type的主要内容,如果未能解决你的问题,请参考以下文章
apache_conf Block Known Bots将以下代码段添加到.htaccess以阻止已知机器人的访问。
当类型已知时,类型“T”不能用作泛型类型中的类型参数或方法错误
无法将 IP 地址“xxx”的 RSA 主机密钥添加到已知主机列表 (/home/webapp/.ssh/known_hosts)