当前位置: 首页 > 编程语言 > C#

WCFConfig中System.Object的已知类型分享

时间:2023-04-10 14:21:45 C#

WCFConfig中System.Object的已知类型从对象。我可以通过属性指定已知类型。但在这种情况下,我需要通过配置使其工作。这是一个例子。以下工作正常:[ServiceContract][ServiceKnownType(typeof(MyData))]publicinterfaceIContract{[OperationContract]voidSend(objectdata);}[DataContract]publicclassMyData{[DataMember]publicstringMessage{get;放;但是,如果我删除ServiceKnownType属性并将以下内容放入配置中:我会收到ConfigurationErrorsException消息“属性‘type’的值无效。错误是:System.Object类型不能用作在配置中声明类型。”无论如何通过配置使这项工作?答案证明是不可能单独在配置文件中做我想做的事。上面的配置对应于DataContracts上使用的[KnownType]属性。[ServiceKnownType]似乎无法在配置中实现。另一种方法是将[ServiceKnownType(methodName,type)]属性与自定义配置部分一起使用。新配置如下所示:Contract:[ServiceContract][ServiceKnownType("GetServiceKnownTypes",typeof(KnownTypeHelper))]publicinterfaceIContract{[OperationContract]voidSend(objectdata);}[DataContract]publicclassMyData{[DataMember]publicstringMessage{get;放;}}包含回调的帮助程序类,该回调返回已知类型列表publicstaticclassKnownTypeHelper{publicstaticIEnumerableGetServiceKnownTypes(ICustomAttributeProviderprovider){ServiceKnownTypesSectionserviceKnownTypes=(ServiceKnownTypesSection)ConfigurationManager.GetSection("serviceKnownTypes");DeclaredServiceElementservice=serviceKnownTypes.Services[((Type)(provider)).AssemblyQualifiedName];foreach(ServiceKnownTypeElementknownTypeinservice.KnownTypes){result.Add(knownType.Type);}返回结果;有关创建自定义配置部分的信息,请访问:http://msdn.microsoft.com/en-us/library/2tw134k3.aspxhttp://msdn.microsoft.com/en-us/library/system.configuration.configurationcollectionattribute.aspx我不确定它是否是设计使然,但是如果您不声明具有已知类型的服务合同(即您可以选择将已知类型添加到服务合同中),下面的KnownTypeHelper不会抛出错误).使用System.Collections.Generic;使用系统配置;使用System.Reflection;//////用于从配置文件中查找Wcf服务的已知类型的帮助程序。///publicstaticclassKnownTypeHelper{/////从配置文件中获取服务的已知类型。/////////提供者。/////////配置文件中服务的已知类型。////publicstaticIEnumerableGetServiceKnownTypes(ICustomAttributeProviderprovider){varresult=newList();varserviceKnownTypes=(ServiceKnownTypesSection)ConfigurationManager.GetSection("serviceKnownTypes");if(serviceKnownTypes!=null){varservice=serviceKnownTypes.Services[((Type)provider).AssemblyQualifiedName];if(service!=null){foreach(ServiceKnownTypeElementknownTypeinservice.KnownTypes){result.Add(knownType.Type);}}}返回结果;为了省去别人创建配置类的麻烦,注意:这里没有验证程序集限定的类型名。如果有人想添加适当的属性来执行此操作,请这样做。以上就是C#学习教程的全部内容:WCF已知的System.Object在Config中的类型。///配置已知服务类型的部分。///publicclassServiceKnownTypesSection:ConfigurationSection{//////获取服务。///[ConfigurationProperty("declaredServices",IsDefaultCollection=false)][ConfigurationCollection(typeof(DeclaredServiceElement),AddItemName="serviceContract",CollectionType=ConfigurationElementCollectionType.AddRemoveClearMap)]publicDeclaredServiceElementCollection服务{get{return(DeclaredServiceElementCollection)base["declaredServices"];}}}//////声明的服务元素的集合。///publicclassDeclaredServiceElementCollection:ConfigurationElementCollection{//////获取为其声明了已知类型的服务。/////////服务的关键。///publicnewDeclaredServiceElementthis[stringkey]{get{return(DeclaredServiceElement)BaseGet(键);}设置{varelement=BaseGet(key);varindex=this.BaseIndexOf(元素);if(BaseGet(index)!=null){BaseRemoveAt(index);}BaseAdd(索引,值);}}//////在派生类中重写时,创建一个新的./////////一个新的.///protectedoverrideConfigurationElementCreateNewElement(){returnnewDeclaredServiceElement();}//////在派生类中重写时获取指定配置元素的元素键。/////////充当指定./////////要为其返回密钥的。///protectedoverrideobjectGetElementKey(ConfigurationElementelement){return((DeclaredServiceElement)element).Type;}}//////为其声明已知类型的服务。///publicclassDeclaredServiceElement:ConfigurationElement{//////获取或设置类型。///[ConfigurationProperty("type",IsRequired=true,IsKey=true)]publicstringType{get{return(string)this["type"];}设置{这个["类型"]=值;}}//////获取已知类型。///[ConfigurationProperty("knownTypes",IsDefaultCollection=false)][ConfigurationCollection(typeof(DeclaredServiceElement),AddItemName="knownType",CollectionType=ConfigurationElementCollectionType.AddRemoveClearMap)]publicServiceKnownTypeElementCollectionKnownTypes{get{返回(ServiceKnownTypeElementCollection)base["knownTypes"];}}}//////已知类型元素的集合。///publicclassServiceKnownTypeElementCollection:ConfigurationElementCollection{//////获取具有指定键的已知类型。/////////已知类型的键。///publicnewServiceKnownTypeElementthis[stringkey]{get{return(ServiceKnownTypeElement)BaseGet(key);}}设置{varelement=BaseGet(key);varindex=this.BaseIndexOf(元素);if(BaseGet(index)!=null){BaseRemoveAt(index);}BaseAdd(索引,值);}}//////当n在派生类中重写,创建一个新的./////////一个新的.///protectedoverrideConfigurationElementCreateNewElement(){returnnewServiceKnownTypeElement();}//////在派生类中重写时获取指定配置元素的元素键。/////////充当指定./////////要为其返回密钥的。///protectedoverrideobjectGetElementKey(ConfigurationElementelement){return((ServiceKnownTypeElement)element).Type;}}//////用于与服务关联的已知类型的配置元素。///publicclassServiceKnownTypeElement:ConfigurationElement{//////获取或设置TypeString。///[ConfigurationProperty("type",IsRequired=true,IsKey=true)]publicstringTypeString{get{return(string)this["type"];}设置{这个["类型"]=值;}}//////获取或设置类型。///publicTypeType{get{returnType.GetType(this.TypeString);}设置{t他的[“类型”]=value.AssemblyQualifiedName;}}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处: