I have an image object that I need to save on the server. I convert it into an array of bytes, and then serialize it into JSON with my optional data. But the server can not successfully convert it and it returns an error message to me:
{
"Message": "Cannot convert object of type 'System.String' to type 'System.Byte[]'",
"StackTrace": " at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) at System.Web.Script.Serialization.ObjectConverter.AssignToPropertyOrField(Object propertyValue, Object o, String memberName, JavaScriptSerializer serializer, Boolean throwOnError) at System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary`2 dictionary, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) at System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary`2 rawParams) at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams) at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)",
"ExceptionType": "System.InvalidOperationException"
}
My Json Data which i send:
{
"articleData": {
"ArticleNumber": "002",
"ArticlePhoto": "/9j/4QBwRXhpZgAATU0AKgAAAAgABIdpAAQAAAABAAAAPgESAAQAAAABAAAAAAEAAAQAAAABAAAC0AEBAAQAAAABAAAFAAAAAAAAA6ACAAMAAAABAWgAAKADAAMAAAABAoAAAJIIAAQAAAABAAAAAAAAAAD/4AAQSkZJRgABAQAAAQABAAD/2wBDABQODxIPDRQSEBIXFRQYHjIhHhwcHj0sLiQySUBMS0dARkVQWnNiUFVtVkVGZIhlbXd7gYKBTmCNl4x9lnN+gXz/2wBDARUXFx4aHjshITt8U0ZTfHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHz/wAARCAKAAWgDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDjKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAoqWigCKipaKAIqKlooAioqWigCKipaKAIqKlooAioqWigCKipaKAIqKlooAioqWigCKipaKAIqKlooAioqWigCKipaKAIqKlooAioqWigCKipaKAIqKlooAioqWigCKipaKAIqKlooAioqWigCKipaKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKAND7HF/tfnS/Yov9r86npcVndjIBZQ/wC1+dKLGI/3vzqcVIBxRdgVfsEOP4vzo+wQ/wC1+dWiOKXFF2BT+wQ/7X50v2CH/a/OrYFLii7AqjToD/e/OnjTbfH8X51YWnr0ouwKy6Xbnrv/AO+q6KDwjpkkCORNllBPz/8A1qyVrs7T/jz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7Z70bPegBtFO2H1o2e9ADaKdso8s+tADaBTtnvRs96AEzQAT0p4QDrzThQA0J604DFFFABRS0ooAbinAU5R7VKoHoKQEQFKBTnxu44oAoAULTgOKaF96eBSGKKUCkApRQAtOFIAacBSGApw600Cnge9ADlpw60iilI4zQAtLQKAKAFooApcUAQlj5mKsIelV3xuzUsZ6VDGi0nSs+4/4/Y/8Aeq+n3az7j/j8T60LcaLlNpufc0cVZAtFBUGimBz+/wBqN/tTKKsQ/f7Ub/amUUAP3+1G/wBqZRQA/f7Ub/amUUAP3+1G/wBqZRQA/wAz2o8z2plFAD9/tR5h9KZRQA/zPajzPamUUAP3+1Hme1MooAk8z2FHm+1R0UWAlE5HYU77Sf7o/OoKKVgJvPJOdopRckfwioKKLAWPtR/uj86PtR/uj86r0UWAs/az/dH50C8YfwD86rUUWQFr7af7g/Ol+2t/cH51UoosgLYvm/uD86UX7D/lmPzqnRRZDuy8NRYf8sx+dL/aTYx5Y/OqFFFkFy9/aTf88x+dKNTb/nmPzqhRRZCND+1G/wCeS/nR/aj/APPMfnWfRRZAXTqDE/6sfnSrqTL/AMsx+dUaKOVDuaS6w6jHlL+dQyX7SSh9gBHvVOilyoLsvDUn/uD86P7Tf/nmPzqjRTshF8ao4/5Zj86KoUUWQBRRRTAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKAP//Z"
},
"sessionId": "d98b2c41-373a-40af-ac3b-874198e22a08"
}
My Request provider:
public class JsonProvider : IJsonProvider
{
private const string JsonContentType = "application/json";
public async Task<TResult> MakeApiCall<TResult,TData>(string url, HttpMethod method, TData data)
where TResult : class
{
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(method, url))
{
if (method != HttpMethod.Get)
{
var json = await Task.Run(() => JsonConvert.SerializeObject(data))
.ConfigureAwait(false);
request.Content = new StringContent(json, Encoding.UTF8, JsonContentType);
}
HttpResponseMessage response = await httpClient.SendAsync(request)
.ConfigureAwait(false);
var stringSerialized = await response.Content.ReadAsStringAsync()
.ConfigureAwait(false);
return await Task.Run(() => JsonConvert.DeserializeObject<TResult>(serialize))
.ConfigureAwait(false);
}
}
}
}
Request Objects:
public class SessionRequest
{
[JsonProperty("sessionId")] public string SessionId { get; set; }
}
public class UploadArticlePhotoRequest : SessionRequest
{
[JsonProperty("articleData")] public PhotoRequest ArticleData { get; set; }
}
public class PhotoRequest
{
[JsonProperty("ArticleNumber")] public string ArticleNumber { get; set; }
[JsonProperty("ArticlePhoto")] public byte[] ArticlePhoto { get; set; }
}
Can the problem in the format of conversion or encoding? Is it a client application or server problem?
Can the problem in the format of conversion or encoding? Is
Read the error message. It is clear. "Cannot convert object of type 'System.String' to type 'System.Byte[]'".
Is it a client application or server problem?
Well, obviously - gibven it is a .NET level error - the error happens on the server. Now, this may be bad data sent, but generally that would be seen as a server level error.
You basically made up a TData and it is not compatible with the data sent.
Happens - lack of reading details. That ALSO leads to horrific code like this:
return await Task.Run(() => JsonConvert.DeserializeObject(serialize))
.ConfigureAwait(false);
Just skip the Task.run and deserialize sync. See, there is no async method for that - with good reason - and you do not gain aynthign with Task.RUn, all you do is make sure 2 threads wait, not one. Antipattern from a lack of reading details and thinking things through. There is no way JsonConvert should be async to start (it is CPU bound, not IO bound) and you force the use of a separate task for it, while the original one waits.
Related
I am working on an Azure Function app and want to have a bunch of methods that return a simple, strongly typed response which serializes to json in a simple way without using OkObjectResult due to it having too many extra properties that I don't need.
Example of desired output, where "Value" can become a complex type serialized to json:
{
FooBar : Value
}
This is pretty easily achieved by using 'dynamic' keyword.
[Function("CreateFooBar")]
public async Task<dynamic> CreateFooBar([HttpTrigger(AuthorizationLevel.Anonymous, "post")]
HttpRequestData req,
FunctionContext executionContext)
{
return new
{
FooBar = "Value"
}
}
However, I don't want to have dynamic everywhere in my code.
How can I make the below code actually work so it serializes using a key/value style mechanism? The JsonSerialization and ISerialization doesn't seem to provide an interface for this?
public class OkResultWithValue
{
public OkResultWithValue(string key,object value)
{
_key= key;
}
private string _key;
public object Value { get; set ; }
}
[Function("CreateFooBar")]
public async Task<OkResultWithValue> CreateFooBar([HttpTrigger(AuthorizationLevel.Anonymous, "post")]
HttpRequestData req,
FunctionContext executionContext)
{
object value = DoSomeThing();
return new OkResultWithValue("FooBar",value)
}
Now I can have all my Azure Functions return a strongly typed OkResultWithValue like this:
You can achieve the same with object instead:
[Function("CreateFooBar")]
public async Task<object> CreateFooBar([HttpTrigger(AuthorizationLevel.Anonymous, "post")]
HttpRequestData req,
FunctionContext executionContext)
{
return new
{
FooBar = "Value"
}
}
I have been going crazy this last days.
I'm trying to use string Guids as _id's in my MongoDb, with the latest version of the driver. I stumbled with this tutorial, which shed me some light...
Storing GUIDs as strings in MongoDB with C#
But I am getting this error: FormatException: Cannot deserialize a 'Guid' from BsonType 'ObjectId'.
MongoDB.Bson.Serialization.Serializers.GuidSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
MongoDB.Bson.Serialization.Serializers.SerializerBase<TValue>.MongoDB.Bson.Serialization.IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize(IBsonSerializer serializer, BsonDeserializationContext context)
MongoDB.Bson.Serialization.BsonClassMapSerializer<TClass>.DeserializeMemberValue(BsonDeserializationContext context, BsonMemberMap memberMap)
The stack trace:
at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.DeserializeMemberValue(BsonDeserializationContext context, BsonMemberMap memberMap)
at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.DeserializeClass(BsonDeserializationContext context)
at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue](IBsonSerializer`1 serializer, BsonDeserializationContext context)
at MongoDB.Driver.Core.Operations.CursorBatchDeserializationHelper.DeserializeBatch[TDocument](RawBsonArray batch, IBsonSerializer`1 documentSerializer, MessageEncoderSettings messageEncoderSettings)
at MongoDB.Driver.Core.Operations.FindCommandOperation`1.CreateFirstCursorBatch(BsonDocument cursorDocument)
at MongoDB.Driver.Core.Operations.FindCommandOperation`1.CreateCursor(IChannelSourceHandle channelSource, BsonDocument commandResult)
at MongoDB.Driver.Core.Operations.FindCommandOperation`1.Execute(RetryableReadContext context, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Operations.FindOperation`1.Execute(RetryableReadContext context, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Operations.FindOperation`1.Execute(IReadBinding binding, CancellationToken cancellationToken)
at MongoDB.Driver.OperationExecutor.ExecuteReadOperation[TResult](IReadBinding binding, IReadOperation`1 operation, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl`1.ExecuteReadOperation[TResult](IClientSessionHandle session, IReadOperation`1 operation, ReadPreference readPreference, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl`1.ExecuteReadOperation[TResult](IClientSessionHandle session, IReadOperation`1 operation, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl`1.FindSync[TProjection](IClientSessionHandle session, FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl`1.<>c__DisplayClass45_0`1.<FindSync>b__0(IClientSessionHandle session)
at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSession[TResult](Func`2 func, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl`1.FindSync[TProjection](FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
at MongoDB.Driver.FindFluent`2.ToCursor(CancellationToken cancellationToken)
at MongoDB.Driver.Core.Operations.AsyncCursorSourceEnumerableAdapter`1.GetEnumerator()
at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
at System.Linq.SystemCore_EnumerableDebugView`1.get_Items()
I've scoured the Internet in search of an answer.
I DO NOT WANT TO USE POCO's DECORATION, OR RETURN BsonDocuments. Already went this route:
BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;
BsonSerializer.RegisterSerializer<Guid>(new GuidSerializer(GuidRepresentation.Standard));
and didn't work. Also did the convention thing...
public class GuidAsStringRepresentationConvention : ConventionBase, IMemberMapConvention
{
public void Apply(BsonMemberMap memberMap)
{
if (memberMap.MemberType == typeof(Guid))
memberMap.SetSerializer(new GuidSerializer(BsonType.String));
else if (memberMap.MemberType == typeof(Guid?))
memberMap.SetSerializer(new NullableSerializer<Guid>(new GuidSerializer(BsonType.String)));
}
}
Didn't work either...
This is my code:
public class TestClass
{
public Guid Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
}
This gets called BEFORE the Db connection:
if (!BsonClassMap.IsClassMapRegistered(typeof(TestClass)))
{
BsonClassMap.RegisterClassMap<TestClass>(cm =>
{
cm.MapIdMember(m => m.Id).SetOrder(0);
cm.MapMember(m => m.Name).SetOrder(1);
cm.MapMember(m => m.IsActive).SetOrder(2);
});
}
var conn = configuration.GetConnectionString("MongoDb");
var name = MongoUrl.Create(conn).DatabaseName;
var client = new MongoClient(conn);
_db = client.GetDatabase(name);
after that, I try to get the records, after I initialize the collection in the constructor...
public async Task<IEnumerable<TestClass>> ReadAllAsync(CancellationToken cancellationToken = default)
{
var filter = Builders<TestClass>.Filter.Empty;
var items = await _collection.Find<TestClass>(filter).ToListAsync(cancellationToken: cancellationToken);
return items;
}
And of course, I initialize the serializer on "ConfigureServices" in the Startup.cs
BsonSerializer.RegisterSerializer(new GuidSerializer(BsonType.String));
Last, the BSON document with the string GUID (or UUID):
{
"_id" : "c2ea54fc-3942-4ad5-8315-9e96cc5de034",
"name" : "I'm Going Crazy",
"isActive" : true
}
If anyone can shed some light, I would appreciate it a lot. Thanks!!!!
[UPDATE]
I did this:
var col = _db.GetCollection<BsonDocument>("TestCollection");
var list = col.Find(FilterDefinition<BsonDocument>.Empty).ToList();
...and I can see the string GUIDs, everything loads perfectly. It's just NOT PARSING correctly. AND I DON'T KNOW WHY. And I refuse to parse this document manually!!!! That's what the driver is supposed to be doing!
[UPDATE 2]
Decided to decorate the property with:
[BsonId]
[BsonRepresentation(MongoDB.Bson.BsonType.String)]
public Guid Id { get; set; }
Didn't work either...
If you want to keep using Guids natively (rather than treating them as strings), change the serializer you are registering.
Remove:
BsonSerializer.RegisterSerializer(new GuidSerializer(BsonType.String));
And replace it with:
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;
Yes, the second line (at time of writing), is an obsolete method, but for now it's the only way to do it. It's an open bug with the c# driver.
There does seem to be an issue here that needs to be fixed.
In the long run we encourage everyone to use the new V3 GuidRepresentationMode, and in fact when using V3 mode this bug does not appear to happen. I added the following line of code as the first line of main:
BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;
We still need to figure out how to get your code to work in V2 mode, but if you are willing to use V3 mode you could have an immediate workaround.
Apparently, the GUIDs I was using were not valid GUIDs. I generated them using an online generator.
I generated new ones on another website, and voilá. Everyting worked.
In the following class for Asp.Net http middleware, there is a method with type parameter (ExportResult<T>(...)) and a dictionary (Exports) to store the method with different type parameters.
public class ExportMiddleware : IMiddleware
{
public Dictionary<string, Func<IEnumerable<dynamic>, string, DataSourceLoadOptionsBase, HttpContext, Task>> Exports =>
new Dictionary<string, Func<IEnumerable<dynamic>, string, DataSourceLoadOptionsBase, HttpContext, Task>>();
public ExportMiddleware()
{
Exports.Add("download1", (IEnumerable<dynamic> source, string format, DataSourceLoadOptionsBase dataOptions, HttpContext context) =>
ExportResult<MyEntity>(source, format, dataOptions, context)); // Error
// Cannot convert IEnumerable<dynamic> to IEnumerable<MyEntity>
}
private async Task ExportResult<T>(IEnumerable<T> source, string format, DataSourceLoadOptionsBase dataOptions, HttpContext context)
{
// ....
// report.DataSource = loadedData.data.Cast<T>();
// ....
}
// Consume the dictionary
public Task InvokeAsync(HttpContext context, RequestDelegate next)
{
// ....
var path = context.Request.Path.ToString();
if (Exports.TryGetValue(path, out var func))
return func(source, format, options, context);
return next(context);
}
How to resolve the error of Cannot convert IEnumerable<dynamic> to IEnumerable<MyEntity> on source?
Is there a way not using dynamic? (there is no class type parameter)
If your code knows that source will be an IEnumerable<MyEntity>, then cast it as such:
Exports.Add("download1", (IEnumerable<dynamic> source, string format, DataSourceLoadOptionsBase dataOptions, HttpContext context) =>
ExportResult((IEnumerable<MyEntity>)source, format, dataOptions, context));
If you want to use the calling pattern in your example, you can move the cast into your Export helper:
private async Task ExportResult<T>(IEnumerable<dynamic> dynamicSource, string format, DataSourceLoadOptionsBase dataOptions, HttpContext context)
{
var source = (IEnumerable<T>)dynamicSource;
If the source is actually a different type of IEnumerable<>, but you happen to know that all the items in it will be of type T, use the LINQ Cast method instead:
private async Task ExportResult<T>(IEnumerable<dynamic> dynamicSource, string format, DataSourceLoadOptionsBase dataOptions, HttpContext context)
{
var source = dynamicSource.Cast<T>();
In any of these cases, you might want to re-think whether dynamic is really what you want. Since IEnumerable<> is covariant, IEnumerable<object> would work just as well in all the code you've shared.
All,
I am trying to modify the payload of incoming object via the web API. Currently I'm using a custom formatter which inherits from JsonMediaTypeFormatter and overrides the relevant methods.
Looks like this:
public override async Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger,
CancellationToken cancellationToken)
{
object obj = await base.ReadFromStreamAsync(type, readStream, content, formatterLogger, cancellationToken);
TrySetEventNo(obj, GetEventNo());
return obj;
}
private void TrySetEventNo(object content, long eventNo)
{
if (content is EventModelBase)
{
EventModelBase eventBase = (EventModelBase)content;
eventBase.EventNo = eventNo;
}
}
I'm using this to track every object that comes through the API.
Before all of this happens, I have a MessageHandler which is creating an event number and adding it to Request.Properties.
Trying to get the event number in the formatter which was created previously in the MessageHandler is proving difficult. Access HttpContext.Current.Items["MS_HttpRequestMessage"].Properties seems to be a different request as it does not contain the event number.
I've two questions:
Am I doing this the correctly or is there a better way?
If I am taking the correct approach, how to I get the correct Request to extract the event number?
Thanks
I've found a solution, instead of attempting to do this inside a formatter I'm now using an ActionFilterAttribute.
overriding OnActionExecuting(HttpActionContext actionContext) and enumerating action actionContext.ActionArguments.
The complete solution looks like so:
public class SetModelEventNumberActionFilter : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
foreach (KeyValuePair<string, object> actionArgument in actionContext.ActionArguments)
{
TrySetEventNo(actionArgument.Value, GetEventNo(actionContext));
}
base.OnActionExecuting(actionContext);
}
private void TrySetEventNo(object content, long eventNo)
{
if (content is EventPivotRequestMessageBase)
{
EventPivotRequestMessageBase eventBase = (EventPivotRequestMessageBase)content;
eventBase.EventNo = eventNo;
}
}
private long GetEventNo(HttpActionContext actionContext)
{
long eventNo = (long)actionContext.Request.Properties[Constant.EVENT_ID];
return eventNo;
}
}
I'm trying to access the complete original text from within a method marked as a LuisIntent within a LuisDialog.
The documentation shows these methods as taking two arguments:
IDialogContext context, LuisResult result
Neither of which publicly exposes the original text of the message. The context object does contain the message but in a private property (context.data.message.text) which is not accessible.
Is there a way to access this in the context, or can it be passed into the dialog constructor?
With the new version of Bot Framework (1.0.2) the LuisResult object now has a Query parameter that contains the original query sent to LUIS.
You can override the MessageReceived(...) function of the LuisDialog keep the fields of the message that you need as member variables and access those fields in your intent handlers. Below I modified the SimpleAlarmDialog to show how you can access 'message.Text' in one of the intent handlers:
[LuisModel("c413b2ef-382c-45bd-8ff0-f76d60e2a821", "6d0966209c6e4f6b835ce34492f3e6d9")]
[Serializable]
public class SimpleAlarmDialog : LuisDialog<object>
{
private readonly Dictionary<string, Alarm> alarmByWhat = new Dictionary<string, Alarm>();
[Serializable]
public class PartialMessage
{
public string Text { set; get; }
}
private PartialMessage message;
protected override async Task MessageReceived(IDialogContext context, IAwaitable<Message> item)
{
var msg = await item;
this.message = new PartialMessage { Text = msg.Text };
await base.MessageReceived(context, item);
}
[LuisIntent("builtin.intent.alarm.delete_alarm")]
public async Task DeleteAlarm(IDialogContext context, LuisResult result)
{
await context.PostAsync($"echo: {message.Text}");
Alarm alarm;
if (TryFindAlarm(result, out alarm))
{
this.alarmByWhat.Remove(alarm.What);
await context.PostAsync($"alarm {alarm} deleted");
}
else
{
await context.PostAsync("did not find alarm");
}
context.Wait(MessageReceived);
}
}
If you break into the method, you can see in the quick watch that the context object has a non-public property through to context.data.mesage.Text (note the misspelling of "mesage"). Since the property is non-public, you could cheat by using reflection to get at it (see GetInstanceField in How to get the value of private field in C#?)
Microsoft.Bot.Builder.Dialogs.Internals.JObjectBotData data = GetInstanceField(typeof (Microsoft.Bot.Builder.Dialogs.Internals.DialogContext), context, "data") as Microsoft.Bot.Builder.Dialogs.Internals.JObjectBotData;
Microsoft.Bot.Connector.Message originalMessage = GetInstanceField(typeof(Microsoft.Bot.Builder.Dialogs.Internals.JObjectBotData), data, "mesage") as Microsoft.Bot.Connector.Message;
string originalMessageText = originalMessage.Text;
You could do it like this by using the Query property of the LuisResult,
[LuisIntent(intentName: "someIntentName")]
private async Task Eligibility(IDialogContext context, LuisResult result)
{
await context.PostAsync($"The original text is: {result.Query}");
context.Wait(MessageReceivedAsync);
}