本文整理汇总了C#中ODataMessageReader.CreateODataParameterReader方法的典型用法代码示例。如果您正苦于以下问题:C# ODataMessageReader.CreateODataParameterReader方法的具体用法?C# ODataMessageReader.CreateODataParameterReader怎么用?C# ODataMessageReader.CreateODataParameterReader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ODataMessageReader
的用法示例。
在下文中一共展示了ODataMessageReader.CreateODataParameterReader方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Read
/// <inheritdoc />
public override object Read(ODataMessageReader messageReader, ODataDeserializerContext readContext)
{
if (messageReader == null)
{
throw Error.ArgumentNull("messageReader");
}
// Create the correct resource type;
ODataActionParameters payload = new ODataActionParameters();
IEdmFunctionImport action = GetFunctionImport(readContext);
ODataParameterReader reader = messageReader.CreateODataParameterReader(action);
while (reader.Read())
{
string parameterName = null;
IEdmFunctionParameter parameter = null;
switch (reader.State)
{
case ODataParameterReaderState.Value:
parameterName = reader.Name;
parameter = action.Parameters.SingleOrDefault(p => p.Name == parameterName);
// ODataLib protects against this but asserting just in case.
Contract.Assert(parameter != null, String.Format(CultureInfo.InvariantCulture, "Parameter '{0}' not found.", parameterName));
payload[parameterName] = Convert(reader.Value, parameter.Type, readContext);
break;
case ODataParameterReaderState.Collection:
parameterName = reader.Name;
parameter = action.Parameters.SingleOrDefault(p => p.Name == parameterName);
// ODataLib protects against this but asserting just in case.
Contract.Assert(parameter != null, String.Format(CultureInfo.InvariantCulture, "Parameter '{0}' not found.", parameterName));
IEdmCollectionTypeReference collectionType = parameter.Type as IEdmCollectionTypeReference;
Contract.Assert(collectionType != null);
payload[parameterName] = Convert(reader.CreateCollectionReader(), collectionType, readContext);
break;
default:
break;
}
}
return payload;
}
示例2: RunParameterReaderTest
private ParameterReaderResult RunParameterReaderTest(string payload)
{
var message = new InMemoryMessage();
message.Stream = new MemoryStream(Encoding.UTF8.GetBytes(payload));
message.SetHeader("Content-Type", "application/json");
var parameterReaderResult = new ParameterReaderResult();
using (var messageReader = new ODataMessageReader((IODataRequestMessage)message, null, this.model))
{
var parameterReader = messageReader.CreateODataParameterReader(this.action);
while (parameterReader.Read())
{
switch (parameterReader.State)
{
case ODataParameterReaderState.Value:
{
parameterReaderResult.Values.Add(new KeyValuePair<string, object>(parameterReader.Name, parameterReader.Value));
break;
}
case ODataParameterReaderState.Collection:
{
var collection = new ParameterReaderCollection();
parameterReaderResult.Collections.Add(new KeyValuePair<string, ParameterReaderCollection>(parameterReader.Name, collection));
var collectionReader = parameterReader.CreateCollectionReader();
while (collectionReader.Read())
{
switch (collectionReader.State)
{
case ODataCollectionReaderState.Value:
{
collection.Items.Add(collectionReader.Item);
break;
}
}
}
break;
}
case ODataParameterReaderState.Entry:
{
var entryReader = parameterReader.CreateEntryReader();
var entryList = new List<ODataEntry>();
parameterReaderResult.Entries.Add(new KeyValuePair<string, IList<ODataEntry>>(parameterReader.Name, entryList));
while (entryReader.Read())
{
switch (entryReader.State)
{
case ODataReaderState.EntryEnd:
entryList.Add((ODataEntry)entryReader.Item);
break;
}
}
break;
}
case ODataParameterReaderState.Feed:
{
var entryReader = parameterReader.CreateFeedReader();
var entryList = new List<ODataEntry>();
parameterReaderResult.Entries.Add(new KeyValuePair<string, IList<ODataEntry>>(parameterReader.Name, entryList));
var feedList = new List<ODataFeed>();
parameterReaderResult.Feeds.Add(new KeyValuePair<string, IList<ODataFeed>>(parameterReader.Name, feedList));
while (entryReader.Read())
{
switch (entryReader.State)
{
case ODataReaderState.EntryEnd:
entryList.Add((ODataEntry)entryReader.Item);
break;
case ODataReaderState.FeedEnd:
feedList.Add((ODataFeed)entryReader.Item);
break;
}
}
break;
}
}
}
}
return parameterReaderResult;
}
示例3: ProcessActionInvokePostBody
private Expression[] ProcessActionInvokePostBody(IODataRequestMessage message, IEdmOperation operation)
{
using (var messageReader = new ODataMessageReader(message, this.GetReaderSettings(), this.DataSource.Model))
{
List<Expression> parameterValues = new List<Expression>();
var parameterReader = messageReader.CreateODataParameterReader(operation);
while (parameterReader.Read())
{
switch (parameterReader.State)
{
case ODataParameterReaderState.Value:
{
object clrValue = ODataObjectModelConverter.ConvertPropertyValue(parameterReader.Value);
parameterValues.Add(Expression.Constant(clrValue));
break;
}
case ODataParameterReaderState.Collection:
{
ODataCollectionReader collectionReader = parameterReader.CreateCollectionReader();
object clrValue = ODataObjectModelConverter.ConvertPropertyValue(ODataObjectModelConverter.ReadCollectionParameterValue(collectionReader));
parameterValues.Add(Expression.Constant(clrValue, clrValue.GetType()));
break;
}
case ODataParameterReaderState.Entry:
{
var entryReader = parameterReader.CreateEntryReader();
object clrValue = ODataObjectModelConverter.ConvertPropertyValue(ODataObjectModelConverter.ReadEntryParameterValue(entryReader));
parameterValues.Add(Expression.Constant(clrValue, clrValue.GetType()));
break;
}
case ODataParameterReaderState.Feed:
{
IList collectionList = null;
var feedReader = parameterReader.CreateFeedReader();
while (feedReader.Read())
{
if (feedReader.State == ODataReaderState.EntryEnd)
{
object clrItem = ODataObjectModelConverter.ConvertPropertyValue(feedReader.Item);
if (collectionList == null)
{
Type itemType = clrItem.GetType();
Type listType = typeof(List<>).MakeGenericType(new[] { itemType });
collectionList = (IList)Utility.QuickCreateInstance(listType);
}
collectionList.Add(clrItem);
}
}
parameterValues.Add(Expression.Constant(collectionList, collectionList.GetType()));
break;
}
}
}
return parameterValues.ToArray();
}
}
示例4: Read
/// <inheritdoc />
public override object Read(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext)
{
if (messageReader == null)
{
throw Error.ArgumentNull("messageReader");
}
IEdmFunctionImport action = GetFunctionImport(readContext);
// Create the correct resource type;
Dictionary<string, object> payload;
if (type == typeof(ODataActionParameters))
{
payload = new ODataActionParameters();
}
else
{
payload = new ODataUntypedActionParameters(action);
}
ODataParameterReader reader = messageReader.CreateODataParameterReader(action);
while (reader.Read())
{
string parameterName = null;
IEdmFunctionParameter parameter = null;
switch (reader.State)
{
case ODataParameterReaderState.Value:
parameterName = reader.Name;
parameter = action.Parameters.SingleOrDefault(p => p.Name == parameterName);
// ODataLib protects against this but asserting just in case.
Contract.Assert(parameter != null, String.Format(CultureInfo.InvariantCulture, "Parameter '{0}' not found.", parameterName));
if (parameter.Type.IsPrimitive())
{
payload[parameterName] = reader.Value;
}
else
{
ODataEdmTypeDeserializer deserializer = DeserializerProvider.GetEdmTypeDeserializer(parameter.Type);
payload[parameterName] = deserializer.ReadInline(reader.Value, parameter.Type, readContext);
}
break;
case ODataParameterReaderState.Collection:
parameterName = reader.Name;
parameter = action.Parameters.SingleOrDefault(p => p.Name == parameterName);
// ODataLib protects against this but asserting just in case.
Contract.Assert(parameter != null, String.Format(CultureInfo.InvariantCulture, "Parameter '{0}' not found.", parameterName));
IEdmCollectionTypeReference collectionType = parameter.Type as IEdmCollectionTypeReference;
Contract.Assert(collectionType != null);
ODataCollectionValue value = ODataCollectionDeserializer.ReadCollection(reader.CreateCollectionReader());
ODataCollectionDeserializer collectionDeserializer = DeserializerProvider.GetEdmTypeDeserializer(collectionType) as ODataCollectionDeserializer;
payload[parameterName] = collectionDeserializer.ReadInline(value, collectionType, readContext);
break;
default:
break;
}
}
return payload;
}
示例5: Read
public override object Read(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext)
{
if (messageReader == null)
{
throw Error.ArgumentNull("messageReader");
}
IEdmAction action = GetAction(readContext);
Contract.Assert(action != null);
// Create the correct resource type;
Dictionary<string, object> payload;
if (type == typeof(ODataActionParameters))
{
payload = new ODataActionParameters();
}
else
{
payload = new ODataUntypedActionParameters(action);
}
ODataParameterReader reader = messageReader.CreateODataParameterReader(action);
while (reader.Read())
{
string parameterName = null;
IEdmOperationParameter parameter = null;
switch (reader.State)
{
case ODataParameterReaderState.Value:
parameterName = reader.Name;
parameter = action.Parameters.SingleOrDefault(p => p.Name == parameterName);
// ODataLib protects against this but asserting just in case.
Contract.Assert(parameter != null, String.Format(CultureInfo.InvariantCulture, "Parameter '{0}' not found.", parameterName));
if (parameter.Type.IsPrimitive())
{
payload[parameterName] = reader.Value;
}
else
{
ODataEdmTypeDeserializer deserializer = DeserializerProvider.GetEdmTypeDeserializer(parameter.Type);
payload[parameterName] = deserializer.ReadInline(reader.Value, parameter.Type, readContext);
}
break;
case ODataParameterReaderState.Collection:
parameterName = reader.Name;
parameter = action.Parameters.SingleOrDefault(p => p.Name == parameterName);
// ODataLib protects against this but asserting just in case.
Contract.Assert(parameter != null, String.Format(CultureInfo.InvariantCulture, "Parameter '{0}' not found.", parameterName));
IEdmCollectionTypeReference collectionType = parameter.Type as IEdmCollectionTypeReference;
Contract.Assert(collectionType != null);
ODataCollectionValue value = ODataCollectionDeserializer.ReadCollection(reader.CreateCollectionReader());
ODataCollectionDeserializer collectionDeserializer = (ODataCollectionDeserializer)DeserializerProvider.GetEdmTypeDeserializer(collectionType);
payload[parameterName] = collectionDeserializer.ReadInline(value, collectionType, readContext);
break;
case ODataParameterReaderState.Entry:
parameterName = reader.Name;
parameter = action.Parameters.SingleOrDefault(p => p.Name == parameterName);
Contract.Assert(parameter != null, String.Format(CultureInfo.InvariantCulture, "Parameter '{0}' not found.", parameterName));
IEdmEntityTypeReference entityTypeReference = parameter.Type as IEdmEntityTypeReference;
Contract.Assert(entityTypeReference != null);
ODataReader entryReader = reader.CreateEntryReader();
object item = ODataEntityDeserializer.ReadEntryOrFeed(entryReader);
ODataEntityDeserializer entityDeserializer = (ODataEntityDeserializer)DeserializerProvider.GetEdmTypeDeserializer(entityTypeReference);
payload[parameterName] = entityDeserializer.ReadInline(item, entityTypeReference, readContext);
break;
case ODataParameterReaderState.Feed:
parameterName = reader.Name;
parameter = action.Parameters.SingleOrDefault(p => p.Name == parameterName);
Contract.Assert(parameter != null, String.Format(CultureInfo.InvariantCulture, "Parameter '{0}' not found.", parameterName));
IEdmCollectionTypeReference feedType = parameter.Type as IEdmCollectionTypeReference;
Contract.Assert(feedType != null);
ODataReader feedReader = reader.CreateFeedReader();
object feed = ODataEntityDeserializer.ReadEntryOrFeed(feedReader);
ODataFeedDeserializer feedDeserializer = (ODataFeedDeserializer)DeserializerProvider.GetEdmTypeDeserializer(feedType);
object result = feedDeserializer.ReadInline(feed, feedType, readContext);
IEdmTypeReference elementTypeReference = feedType.ElementType();
Contract.Assert(elementTypeReference.IsEntity());
IEnumerable enumerable = result as IEnumerable;
if (enumerable != null)
{
if (readContext.IsUntyped)
{
EdmEntityObjectCollection entityCollection = new EdmEntityObjectCollection(feedType);
foreach (EdmEntityObject entityObject in enumerable)
{
entityCollection.Add(entityObject);
}
//.........这里部分代码省略.........
示例6: ReadParams
/// <summary>
/// Reads Parameter names and values provided by a client in a POST request to invoke a particular Action.
/// </summary>
/// <param name="messageReader">Reader used to read all OData payloads (entries, feeds, metadata documents, service documents, etc.).</param>
/// <param name="action">Represents an EDM operation.</param>
/// <param name="readContext">
/// Encapsulates the state and settings that get passed to System.Web.OData.Formatter.Deserialization.ODataDeserializer from the System.Web.OData.Formatter.ODataMediaTypeFormatter.
/// </param>
/// <returns>ActionPayload holds the Parameter names and values provided by a client in a POST request to invoke a particular Action.</returns>
private ODataActionParameters ReadParams(ODataMessageReader messageReader, IEdmOperation action, ODataDeserializerContext readContext)
{
// Create the correct resource type;
ODataActionParameters payload = new ODataActionParameters();
try
{
ODataParameterReader reader = messageReader.CreateODataParameterReader(action);
while (reader.Read())
{
string parameterName = null;
IEdmOperationParameter parameter = null;
switch (reader.State)
{
case ODataParameterReaderState.Value:
parameterName = reader.Name;
parameter = action.Parameters.SingleOrDefault(p => p.Name == parameterName);
// ODataLib protects against this but asserting just in case.
Contract.Assert(parameter != null, String.Format(CultureInfo.InvariantCulture, "Parameter '{0}' not found.", parameterName));
if (parameter.Type.IsPrimitive())
{
payload[parameterName] = reader.Value;
}
else
{
ODataEdmTypeDeserializer deserializer = DefaultODataDeserializerProvider.Instance.GetEdmTypeDeserializer(parameter.Type);
payload[parameterName] = deserializer.ReadInline(reader.Value, parameter.Type, readContext);
}
break;
case ODataParameterReaderState.Collection:
parameterName = reader.Name;
parameter = action.Parameters.SingleOrDefault(p => p.Name == parameterName);
// ODataLib protects against this but asserting just in case.
Contract.Assert(parameter != null, String.Format(CultureInfo.InvariantCulture, "Parameter '{0}' not found.", parameterName));
IEdmCollectionTypeReference collectionType = parameter.Type as IEdmCollectionTypeReference;
Contract.Assert(collectionType != null);
ODataCollectionValue value = ReadCollection(reader.CreateCollectionReader());
ODataCollectionDeserializer collectionDeserializer = DefaultODataDeserializerProvider.Instance.GetEdmTypeDeserializer(collectionType) as ODataCollectionDeserializer;
payload[parameterName] = collectionDeserializer.ReadInline(value, collectionType, readContext);
break;
default:
break;
}
}
}
catch (Exception exception)
{
DynamicLogger.Instance.WriteLoggerLogError("ReadParams", exception);
throw exception;
}
return payload;
}