自定义wcf数据提供程序并调试关系错误

Sig*_*igh 7 wcf wcf-data-services

我正在实现一个自定义数据提供程序,我已经知道它返回数据并且可以进行过滤,但是在使关系工作时遇到了一些麻烦.

查询元数据时,关系看起来是正确的,并且在查询表时会出现相关的属性链接,但在尝试访问ResourceReference属性时,我会收到以下异常:

    Object reference not set to an instance of an object.
        System.NullReferenceException
        stacktrace at System.Data.Services.Providers.DataServiceProviderWrapper.GetResourceAssociationSet(ResourceSetWrapper resourceSet, ResourceType resourceType, ResourceProperty resourceProperty)
   at System.Data.Services.Providers.DataServiceProviderWrapper.GetContainer(ResourceSetWrapper sourceContainer, ResourceType sourceResourceType, ResourceProperty navigationProperty)
   at System.Data.Services.Providers.DataServiceProviderWrapper.GetResourceProperties(ResourceSetWrapper resourceSet, ResourceType resourceType)
   at System.Data.Services.Serializers.SyndicationSerializer.WriteObjectProperties(IExpandedResult expanded, Object customObject, ResourceType resourceType, Uri absoluteUri, String relativeUri, SyndicationItem item, DictionaryContent content, EpmSourcePathSegment currentSourceRoot)
   at System.Data.Services.Serializers.SyndicationSerializer.WriteEntryElement(IExpandedResult expanded, Object element, ResourceType expectedType, Uri absoluteUri, String relativeUri, SyndicationItem target)
   at System.Data.Services.Serializers.SyndicationSerializer.WriteTopLevelElement(IExpandedResult expanded, Object element)
   at System.Data.Services.Serializers.Serializer.WriteRequest(IEnumerator queryResults, Boolean hasMoved)
   at System.Data.Services.ResponseBodyWriter.Write(Stream stream)
Run Code Online (Sandbox Code Playgroud)

以下是我如何创建关系的示例:

var sourceReference = new ResourceProperty(
                relatedType.ResourceTypeName,
                ResourcePropertyKind.ResourceReference,
                relatedType.ResourceType);
            sourceReference.CanReflectOnInstanceTypeProperty = false;
            compoundType.ResourceType.AddProperty(sourceReference);

var destinationReference = new ResourceProperty(
                compoundType.ResourceSetName,
                ResourcePropertyKind.ResourceSetReference,
                compoundType.ResourceType);
            destinationReference.CanReflectOnInstanceTypeProperty = false;
            source.ResourceType.AddProperty(destinationReference);

var sourceAssociation = new ResourceAssociationSet(
                        "source",
                        new ResourceAssociationSetEnd(compoundType.ResourceSet, compoundType.ResourceType, sourceReference),
                        new ResourceAssociationSetEnd(relatedType.ResourceSet, relatedType.ResourceType, null));

var destinationAssociation = new ResourceAssociationSet(
                             "destination",
                             new ResourceAssociationSetEnd(relatedType.ResourceSet, relatedType.ResourceType, destinationReference),
                             new ResourceAssociationSetEnd(compoundType.ResourceSet, compoundType.ResourceType, null));
Run Code Online (Sandbox Code Playgroud)

通过查看OData网站上的示例代码,我认为我已经完成了所有操作,并且无法确定我的错误.有任何想法吗?或者有关调试自定义WCF数据服务的提示?

更新: 这是在null异常之前发生的事情.

有一个资源集Collars与项目的关系,所以我做这个查询:blah.svc/Collars(1)/项目

我使用参数ResourceSet = Collars,ResourceType = Collar,Property = Project调用我的IDataServiceMetadataProvider中的GetResourceAssociationSet覆盖,并返回上面指定的关联集.然后使用ResourceSet = Projects,ResourceType = Collar,Property = Project再次调用GetResourceAssociationSet,并返回相同的关联集.

然后在System.Data.Services.Providers.GetResourceAssociationSetEnd中传入的变量是resourceSet = Projects,resourceType = Collar,resourceProperty = Project,此函数返回null.

这使得System.Data.Services.Providers.DataServiceProviderWrapper.GetResourceAssociationSet中的thisEnd等于null.然后使用相同的变量调用GetRelatedResourceAssociationSetEnd,并返回null.

然后随着电话崩溃

ResourceSetWrapper relatedSet = this.ValidateResourceSet(relatedEnd.ResourceSet); 
Run Code Online (Sandbox Code Playgroud)

因为relatedEnd为null.

Sig*_*igh -1

对我来说,解决方案是我在 IDataServiceQueryProvider 中实现 GetResourceType 时犯了一个错误。

当查询作为 ResourceSetReference 的资源属性时,我返回父资源的 ResourceType,而不是相关资源的类型。