Problem generating code from Swagger file for ValueTuple - c#

I have a swagger json file and the API is built with NET7. The API exposes NET-specific types such as ValueTuple. When I try to generate the code, the type is generate like:
"System.ValueTuple`2[[System.String, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]": {
"type": "object",
"additionalProperties": false
}
Is this correct? Is it possible to make it cleaner?

Related

Index out of range exception in FieldNameLookup.GetOrdinal when running SqlQuery EF

We are getting eventually an IndexOutOfRange exception when running code like this:
result.Data = dbOptima.Database.ExecuteStoredProcedure(
task,
StoredProcedureValues.PROC_GET_TASKS).ToList();
,where ExecuteStoredProcedure does the following:
public static IEnumerable<TResult> ExecuteStoredProcedure<TResult>(this Database database, IStoredProcedure<TResult> procedure, string procedureName)
{
var parameters = CreateSqlParametersFromProperties(procedure);
var format = CreateSPCommand<TResult>(parameters, procedureName);
return database.SqlQuery<TResult>(format, parameters.Cast<object>).ToArray());
}
We cannot reproduce the problem locally, but using Application Insights the exception is registered quite often. The following is a call stack extract:
System.IndexOutOfRangeException:
at System.Data.ProviderBase.FieldNameLookup.GetOrdinal (System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.SqlClient.SqlDataReader.GetOrdinal (System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.TryGetColumnOrdinalFromReader (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.CreateColumnMapFromReaderAndClrType (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext.InternalTranslate (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryInternal (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext+<>c__DisplayClass65`1.<ExecuteStoreQueryReliably>b__64 (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext+<>c__DisplayClass65`1.<ExecuteStoreQueryReliably>b__63 (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute (EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryReliably (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQuery (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Collections.Generic.List`1..ctor (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Linq.Enumerable.ToList (System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
We've tried to reproduce the problem locally by:
making the stored procedure fail.
making the stored procedure return 0 results.
causing a mapping problem by renaming some model property.
with no success.
Typically, this error happens due to a mapping error in the datareader when you use ADO.NET, but this is not the case since we use EF 6 and it does not happen always, so we cannot really find where the problem is.
The problem is related to binding the result of the stored procedure with the model declared to be bound with the output of the stored procedure.
If the stored procedure retrieves more columns than the properties that the model has, even though the model is satisfied (because all the properties can be matched with columns of the stored procedure output), internally an exception is raised cause the model doesn't have properties to store some of the store procedure output values.
So, this can be fixed in two ways: either
adding properties in the model to contain the columns of the stored
procedure, or
removing the unnecessary columns of the stored procedure output.
The second approach is normally better, since this issue is making evident that some data returned by the stored procedure is no longer required, so it would be cleaner and more performant to remove the unneeded code in the stored procedure.

Map Dictionary values to Entity object

I have an IEnumerable<Dictionary<string,string>> and I want to map it to an IEnumerable<Entity>. I was wondering what's the best way to do so?
E.g.
IEnumerable<Dictionary<string,string>> dictionary
where dictionary.Select(x=>x.Values.ToList()) which will give me the values I want to store/map to my entity object but the keys in the dictionary will tell it which entity to map to.
So I want to do something like if key == entityPropertyName, then insert value and I want to do that for the entire collection.
EDIT:
I tried this:
var serializer = new JavaScriptSerializer();
var csvToSave = serializer.Deserialize<entitiy>(serializer.Serialize(rows.ToDictionary(kvp=>kvp.Keys, kvp=>kvp.Values)));
But I get this issue:
Message :Type 'System.Collections.Generic.Dictionary`2[[System.Collections.Generic.Dictionary`2+KeyCollection[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Collections.Generic.Dictionary`2+ValueCollection[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' is not supported for serialization/deserialization of a dictionary, keys must be strings or objects.
Source :System.Web.Extensions

Add ref to SYSTEM.WEB.EXTENSIONS‬

I have the follow lines:
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
private global::AjaxControlToolkit.ToolkitScriptManager #__BuildControlToolkitScriptManager1() {
global::AjaxControlToolkit.ToolkitScriptManager #__ctrl;
and I got this error:
The type 'System.Web.UI.ScriptManager' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
I choose Add Reference>.Net>system.web.extension version 4.0 in visual web developer and still get same error .
What can I do?

How to keep grid DataSource after PostBack

I've written a custom grid view and I want to save grid DataSource in ViewState but I got this exception
Type '<>f__AnonymousType0`7[[System.Int32, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String,
mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089],[System.String, mscorlib,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089],[System.Boolean, mscorlib,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089],[System.String, mscorlib,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'
in Assembly 'ExtAspNet.Examples, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null' is not marked as serializable.
Now, I want to know how can I keep the grid DataSource?
Anything that you attempt to put in the viewstate must be decorated with the [Serializable] attribute, but because you have an anonymous type, you can't do it.
But besides that, don't do what you are trying to do, it will increase your page size considerably and unnecessarily. If anything, put your data source in Session and rebind it on postback but don't put it on viewstate.
Note, though, that putting a huge amount of data in session is not scalable or a good practice either, you have to base your decission depending on the size of your data and how much time it takes to get the data from the backend store. Have you measured how expensive it is to get the data, could you use Cache instead of Session, for example?
if you are binding your gridview datasource with a datatable you can do like this....
Declare the datatable as follows and everything will work as expected
private string _theDataTable="theDataTable";
private DataTable theDataTable
{
get
{
if(ViewState[_theDataTable]==null)
return new DataTable();
return (DataTable)ViewState[_theDataTable];
}
set
{
ViewState[_theDataTable] = value;
}
}
cheers!

Using multiple DataContracts for one service

I've got a ServiceContract that has an OperationContract with the following method signature: Manipulate(int fileid, param object[] operations).
I also have five DataContracts defined for the WCF service, and I'd like object[] operations to accept any number and combination of those. The problem is that they aren't visible unless I use them in the method signature. If I do that though, then only one type of DataContract can be used at a time, defeating the purpose of using an object[].
How do I make all five DataContracts visible on the client side without having to alter the method signature?
Use known types. For example in your config file you could define the different known types:
<configuration>
<system.runtime.serialization>
<dataContractSerializer>
<declaredTypes>
<add type="System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<knownType type="SomeNs.Foo, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXXXXX" />
<knownType type="SomeNs.Bar, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXXXXX" />
<knownType type="SomeNs.Baz, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXXXXX" />
</add>
</declaredTypes>
</dataContractSerializer>
</system.runtime.serialization>
</configuration>
Now clients will know about the Foo, Bar and Baz data contracts.
This being said, I would recommend you to use a common base type for your data contracts instead of object. Having a method signature that takes object as input is hard to understand from a consumer standpoint.

Categories

Resources