WCF 异常:无法添加到已知类型列表

发布于 2024-08-29 09:26:46 字数 7465 浏览 9 评论 0原文

大家好...

我收到一个意外的 WCF 错误,抱怨“已知类型”的名称类似,但名称空间不同(请参阅下面的错误)。

...请记住我仍在尝试这一切。

另外...

我已经了解常规 Web 服务的本质迫使您扁平化对象层次结构,但我读过的所有内容都表明你不必使用 WCF 来做到这一点……这就是我尝试这样做的原因。

所以我的问题是......

  • 问:是否可以告诉 WCF 不要忽略 名称间距?
  • 问:我是否缺少标签?不要WCF 标签(如下)将名称间距纳入 帐户?
  • 问:我必须压平我的物体吗 等级制度?

基本错误是......

类型 'wcfFISH.StateManagement.Contractor.New' 无法添加到已知类型列表中 因为另一种类型 'wcfFISH.StateManagement.CostDivision.New' 具有相同的数据合约名称 'wcfFISH.StateManagement.Contractor:新' 已经存在。

错误看起来像...

An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
System.InvalidOperationException: An exception was thrown in a call to a WSDL export extension: System.ServiceModel.Description.DataContractSerializerOperationBehavior
 contract: http://tempuri.org/:IFishData ----> System.InvalidOperationException: Type 'wcfFISH.StateManagement.Contractor.New' cannot be added to list of known types since another type 'wcfFISH.StateManagement.CostDivision.New' with the same data contract name 'wcfFISH.StateManagement.Contractor:New' is already present.
   at System.Runtime.Serialization.DataContract.CheckAndAdd(Type type, Dictionary`2 typesChecked, Dictionary`2& nameToDataContractTable)
   at System.Runtime.Serialization.DataContract.ImportKnownTypeAttributes(Type type, Dictionary`2 typesChecked, Dictionary`2& knownDataContracts)
   at System.Runtime.Serialization.DataContract.ImportKnownTypeAttributes(Type type)
   at System.Runtime.Serialization.ClassDataContract.ClassDataContractCriticalHelper.get_KnownDataContracts()
   at System.Runtime.Serialization.ClassDataContract.get_KnownDataContracts()
   at System.Runtime.Serialization.DataContractSet.AddClassDataContract(ClassDataContract classDataContract)
   at System.Runtime.Serialization.DataContractSet.Add(XmlQualifiedName name, DataContract dataContract)
   at System.Runtime.Serialization.DataContractSet.AddClassDataContract(ClassDataContract classDataContract)
   at System.Runtime.Serialization.DataContractSet.Add(XmlQualifiedName name, DataContract dataContract)
   at System.Runtime.Serialization.DataContractSet.Add(Type type)
   at System.Runtime.Serialization.XsdDataContractExporter.Export(Type type)
   at System.ServiceModel.Description.MessageContractExporter.ExportType(Type type, String partName, String operationName, XmlSchemaType& xsdType)
   at System.ServiceModel.Description.DataContractSerializerMessageContractExporter.ExportBody(Int32 messageIndex, Object state)
   at System.ServiceModel.Description.MessageContractExporter.ExportMessage(Int32 messageIndex, Object state)
   at System.ServiceModel.Description.MessageContractExporter.ExportMessageContract()
   at System.ServiceModel.Description.DataContractSerializerOperationBehavior.System.ServiceModel.Description.IWsdlExportExtension.ExportContract(WsdlExporter exporter, WsdlContractConversionContext contractContext)
   at System.ServiceModel.Description.WsdlExporter.CallExtension(WsdlContractConversionContext contractContext, IWsdlExportExtension extension)
   --- End of inner ExceptionDetail stack trace ---
   at System.ServiceModel.Description.WsdlExporter.CallExtension(WsdlContractConversionContext contractContext, IWsdlExportExtension extension)
   at System.ServiceModel.Description.WsdlExporter.CallExportContract(WsdlContractConversionContext contractContext)
   at System.ServiceModel.Description.WsdlExporter.ExportContract(ContractDescription contract)
   at System.ServiceModel.Description.WsdlExporter.ExportEndpoint(ServiceEndpoint endpoint, XmlQualifiedName wsdlServiceQName)
   at System.ServiceModel.Description.WsdlExporter.ExportEndpoints(IEnumerable`1 endpoints, XmlQualifiedName wsdlServiceQName)
   at System.ServiceModel.Description.ServiceMetadataBehavior.MetadataExtensionInitializer.GenerateMetadata()
   at System.ServiceModel.Description.ServiceMetadataExtension.EnsureInitialized()
   at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.InitializationData.InitializeFrom(ServiceMetadataExtension extension)
   at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.GetInitData()
   at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.TryHandleDocumentationRequest(Message httpGetRequest, String[] queries, Message& replyMessage)
   at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.ProcessHttpRequest(Message httpGetRequest)
   at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.Get(Message message)
   at SyncInvokeGet(Object , Object[] , Object[] )
   at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

服务代码看起来像...

namespace wcfFISH.StateManagement
{
    [DataContract(Namespace = "wcfFISH.StateManagement")]
    [KnownType(typeof(wcfFISH.StateManagement.C1FE.Approved))]
    [KnownType(typeof(wcfFISH.StateManagement.C1FE.Denied))]
    [KnownType(typeof(wcfFISH.StateManagement.C1FE.New))]
    [KnownType(typeof(wcfFISH.StateManagement.C1FE.Pending))]
    [KnownType(typeof(wcfFISH.StateManagement.Contractor.Authenticated))]
    [KnownType(typeof(wcfFISH.StateManagement.Contractor.New))]
    [KnownType(typeof(wcfFISH.StateManagement.CostDivision.Authenticated))]
    [KnownType(typeof(wcfFISH.StateManagement.CostDivision.New))]
    [KnownType(typeof(wcfFISH.StateManagement.ReportingPeriod.Closed))]
    [KnownType(typeof(wcfFISH.StateManagement.ReportingPeriod.Open))]
    public abstract class ObjectState
    {
        #region CONSTRUCTORS

        protected ObjectState()
        { }

        #endregion

        #region PROPERTIES & FIELDS

        #endregion

        #region EVENTS
        #endregion

        #region METHODS    
        #endregion
    }
}

旁注...

我感到困惑的另一个原因是下面列出的原始声明从未引起问题...并且运行良好!这仅在我添加 CostDivision 时发生!

[KnownType(typeof(wcfFISH.StateManagement.C1FE.New))]
[KnownType(typeof(wcfFISH.StateManagement.Contractor.New))]

如果我不清楚...

我的理解是WCF应该理解以下类之间的区别:

[KnownType(typeof(wcfFISH.StateManagement.Contractor.Authenticated))]
[KnownType(typeof(wcfFISH.StateManagement.Contractor.New))]
[KnownType(typeof(wcfFISH.StateManagement.CostDivision.Authenticated))]
[KnownType(typeof(wcfFISH.StateManagement.CostDivision.New))]

如果它不理解...

我想我唯一的选择似乎是将所有事情的层次结构扁平化为:

wcfFISH.StateManagement.Authenticated
wcfFISH.StateManagement.New

……这是我唯一的选择吗? (希望不是!)

Good Day Everyone...

I’m getting an unexpected WCF error complaining of “Known Types” which are similarly named, but in different name-spaces (see error below).

...please keep in mind I'm still experimenting with all this.

Also…

I already understand the nature of regular web-services FORCES you to flatten your object hierarchy, but everything I’ve read says you don’t have to do that with WCF…which is why I’m attempting this.

So my questions are…

  • Q: Is it possible to tell WCF to NOT TO IGNORE
    the name-spacing?
  • Q: Am I missing a tag? Don’t the WCF
    tags (below) take name-spacing into
    account?
  • Q: Do I HAVE to flatten my object
    hierarchy?

The basic error is…

Type
'wcfFISH.StateManagement.Contractor.New'
cannot be added to list of known types
since another type
'wcfFISH.StateManagement.CostDivision.New'
with the same data contract name
'wcfFISH.StateManagement.Contractor:New'
is already present.

The error looks like…

An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
System.InvalidOperationException: An exception was thrown in a call to a WSDL export extension: System.ServiceModel.Description.DataContractSerializerOperationBehavior
 contract: http://tempuri.org/:IFishData ----> System.InvalidOperationException: Type 'wcfFISH.StateManagement.Contractor.New' cannot be added to list of known types since another type 'wcfFISH.StateManagement.CostDivision.New' with the same data contract name 'wcfFISH.StateManagement.Contractor:New' is already present.
   at System.Runtime.Serialization.DataContract.CheckAndAdd(Type type, Dictionary`2 typesChecked, Dictionary`2& nameToDataContractTable)
   at System.Runtime.Serialization.DataContract.ImportKnownTypeAttributes(Type type, Dictionary`2 typesChecked, Dictionary`2& knownDataContracts)
   at System.Runtime.Serialization.DataContract.ImportKnownTypeAttributes(Type type)
   at System.Runtime.Serialization.ClassDataContract.ClassDataContractCriticalHelper.get_KnownDataContracts()
   at System.Runtime.Serialization.ClassDataContract.get_KnownDataContracts()
   at System.Runtime.Serialization.DataContractSet.AddClassDataContract(ClassDataContract classDataContract)
   at System.Runtime.Serialization.DataContractSet.Add(XmlQualifiedName name, DataContract dataContract)
   at System.Runtime.Serialization.DataContractSet.AddClassDataContract(ClassDataContract classDataContract)
   at System.Runtime.Serialization.DataContractSet.Add(XmlQualifiedName name, DataContract dataContract)
   at System.Runtime.Serialization.DataContractSet.Add(Type type)
   at System.Runtime.Serialization.XsdDataContractExporter.Export(Type type)
   at System.ServiceModel.Description.MessageContractExporter.ExportType(Type type, String partName, String operationName, XmlSchemaType& xsdType)
   at System.ServiceModel.Description.DataContractSerializerMessageContractExporter.ExportBody(Int32 messageIndex, Object state)
   at System.ServiceModel.Description.MessageContractExporter.ExportMessage(Int32 messageIndex, Object state)
   at System.ServiceModel.Description.MessageContractExporter.ExportMessageContract()
   at System.ServiceModel.Description.DataContractSerializerOperationBehavior.System.ServiceModel.Description.IWsdlExportExtension.ExportContract(WsdlExporter exporter, WsdlContractConversionContext contractContext)
   at System.ServiceModel.Description.WsdlExporter.CallExtension(WsdlContractConversionContext contractContext, IWsdlExportExtension extension)
   --- End of inner ExceptionDetail stack trace ---
   at System.ServiceModel.Description.WsdlExporter.CallExtension(WsdlContractConversionContext contractContext, IWsdlExportExtension extension)
   at System.ServiceModel.Description.WsdlExporter.CallExportContract(WsdlContractConversionContext contractContext)
   at System.ServiceModel.Description.WsdlExporter.ExportContract(ContractDescription contract)
   at System.ServiceModel.Description.WsdlExporter.ExportEndpoint(ServiceEndpoint endpoint, XmlQualifiedName wsdlServiceQName)
   at System.ServiceModel.Description.WsdlExporter.ExportEndpoints(IEnumerable`1 endpoints, XmlQualifiedName wsdlServiceQName)
   at System.ServiceModel.Description.ServiceMetadataBehavior.MetadataExtensionInitializer.GenerateMetadata()
   at System.ServiceModel.Description.ServiceMetadataExtension.EnsureInitialized()
   at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.InitializationData.InitializeFrom(ServiceMetadataExtension extension)
   at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.GetInitData()
   at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.TryHandleDocumentationRequest(Message httpGetRequest, String[] queries, Message& replyMessage)
   at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.ProcessHttpRequest(Message httpGetRequest)
   at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.Get(Message message)
   at SyncInvokeGet(Object , Object[] , Object[] )
   at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

The service code looks like…

namespace wcfFISH.StateManagement
{
    [DataContract(Namespace = "wcfFISH.StateManagement")]
    [KnownType(typeof(wcfFISH.StateManagement.C1FE.Approved))]
    [KnownType(typeof(wcfFISH.StateManagement.C1FE.Denied))]
    [KnownType(typeof(wcfFISH.StateManagement.C1FE.New))]
    [KnownType(typeof(wcfFISH.StateManagement.C1FE.Pending))]
    [KnownType(typeof(wcfFISH.StateManagement.Contractor.Authenticated))]
    [KnownType(typeof(wcfFISH.StateManagement.Contractor.New))]
    [KnownType(typeof(wcfFISH.StateManagement.CostDivision.Authenticated))]
    [KnownType(typeof(wcfFISH.StateManagement.CostDivision.New))]
    [KnownType(typeof(wcfFISH.StateManagement.ReportingPeriod.Closed))]
    [KnownType(typeof(wcfFISH.StateManagement.ReportingPeriod.Open))]
    public abstract class ObjectState
    {
        #region CONSTRUCTORS

        protected ObjectState()
        { }

        #endregion

        #region PROPERTIES & FIELDS

        #endregion

        #region EVENTS
        #endregion

        #region METHODS    
        #endregion
    }
}

Side note…

Another reason I am confused is the original declarations listed below NEVER caused an issue...and it was working fine! This only happened when I ADDED CostDivision!

[KnownType(typeof(wcfFISH.StateManagement.C1FE.New))]
[KnownType(typeof(wcfFISH.StateManagement.Contractor.New))]

In case I’m not being clear…

My understanding is WCF should understand the difference between the following classes:

[KnownType(typeof(wcfFISH.StateManagement.Contractor.Authenticated))]
[KnownType(typeof(wcfFISH.StateManagement.Contractor.New))]
[KnownType(typeof(wcfFISH.StateManagement.CostDivision.Authenticated))]
[KnownType(typeof(wcfFISH.StateManagement.CostDivision.New))]

If it does NOT understand…

I guess my only option seems to be to flatten the hierarchy FOR EVERYTHING to:

wcfFISH.StateManagement.Authenticated
wcfFISH.StateManagement.New

…is this my only option? (hope not!)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牵你手 2024-09-05 09:26:46

我的通灵能力告诉我,您的 wcfFISH.StateManagement.CostDivision.New 声明是从 wcfFISH.StateManagement.Contractor.New 复制/粘贴的,当您更改类名时,您没有更改其上的 DataContract 属性以匹配。

My psychic powers tell me that your declaration of wcfFISH.StateManagement.CostDivision.New was copy/pasted from wcfFISH.StateManagement.Contractor.New and while you changed the class name you didn't change the DataContract attribute on it to match.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文