Created
January 28, 2021 04:40
-
-
Save garethj-msft/4cfce0f84b91c857a4dc4e27f7856789 to your computer and use it in GitHub Desktop.
OData Custom Serializer for Edm.Stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using System.Reflection; | |
using Microsoft.AspNet.OData; | |
using Microsoft.AspNet.OData.Formatter; | |
using Microsoft.AspNetCore.Http.Extensions; | |
using Microsoft.OData; | |
namespace SMSOrgView | |
{ | |
using Microsoft.AspNet.OData.Formatter.Serialization; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.Extensions.Logging; | |
using Microsoft.OData.Edm; | |
using System.Collections.Generic; | |
using System; | |
using SMSOrgView.Models; | |
public class OrgODataSerializerProvider : DefaultODataSerializerProvider | |
{ | |
private readonly OrgODataResourceSerializer resourceSerializer; | |
public OrgODataSerializerProvider(IServiceProvider rootContainer) | |
: base(rootContainer) | |
{ | |
this.resourceSerializer = new OrgODataResourceSerializer(this); | |
} | |
public override ODataEdmTypeSerializer GetEdmTypeSerializer(IEdmTypeReference edmType) | |
{ | |
if (edmType.IsEntity()) | |
{ | |
return this.resourceSerializer; | |
} | |
return base.GetEdmTypeSerializer(edmType); | |
} | |
} | |
public class OrgODataResourceSerializer : ODataResourceSerializer | |
{ | |
public OrgODataResourceSerializer(ODataSerializerProvider serializerProvider) | |
: base(serializerProvider) | |
{ | |
} | |
public override ODataProperty CreateStructuralProperty(IEdmStructuralProperty structuralProperty, ResourceContext resourceContext) | |
{ | |
if (structuralProperty.Type.Definition.IsStream()) | |
{ | |
var prop = new ODataProperty | |
{ | |
Name = structuralProperty.Name, | |
}; | |
var value = new ODataStreamReferenceValue(); | |
if (resourceContext.SerializerContext.MetadataLevel == ODataMetadataLevel.FullMetadata) | |
{ | |
value.ContentType = this.ContentType; | |
} | |
prop.Value = value; | |
return prop; | |
} | |
else | |
{ | |
return base.CreateStructuralProperty(structuralProperty, resourceContext); | |
} | |
} | |
public virtual string ContentType | |
{ | |
get { return "application/octet-stream"; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment