I need to convert my web application to phone application. I was succeed to deserializer the request respond into the generic list on my web app, but I haven't figure out how to do it on the phone app. On the web I can use DataContractSerializer and XmlDictionaryRead.CreateTextReader and System.Xml.XmlDictionaryReaderQuotas(), but there is no such method on Windows phone. I modified the code and get the error like that
Error in line 1 position 2. Expecting element 'ArrayOfQueueItem' from namespace 'http://schemas.datacontract.org/2004/07/CMSPhoneApp.DataObjects'..
//Encountered 'Element' with name 'QueueItem', namespace ''.
There is the code to deserialzier
try{
using (XmlReader r = XmlReader.Create(new StringReader(content)))
{
var ser = new DataContractSerializer(typeof(T));
var reader = XmlDictionaryReader.CreateDictionaryReader(r);
ser = new DataContractSerializer(typeof(T));
var deserializedItem = (T)ser.ReadObject(reader, true);
reader.Close();
return deserializedItem;
}
}
I read the respond stream into the string as below:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfQueueItem xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/DataObjects">
<QueueItem>
<callNumber>349551</callNumber>
<errorMsg
i:nil="true" />
<link>/Call/349551</link>
<page>Call</page>
<siteCity>Burnaby</siteCity>
<status>Open</status>
<summary>Mobile Application Research</summary>
</QueueItem>
<QueueItem>
<callNumber>376209</callNumber>
<errorMsg
i:nil="true" />
<link>/Call/376209</link>
<page>Call</page>
<siteCity>Burnaby</siteCity>
<status>Open</status>
<summary>July 2012 ASD Calls of the Month.</summary>
</QueueItem>
</ArrayOfQueueItem>
The following is the "QueueItem" class:
namespace CMSPhoneApp.DataObjects
{
//This is Model
public class QueueItem
{
public string callNumber { get; set; }
public string summary { get; set; }
public string status { get; set; } //queInvoiceAdmin no status
public string link { get; set; }
public string errorMsg { get; set; }
public string page { get; set; }
public string siteCity { get; set; }
}
}
Even I trid to cut the respond stream into as follows, I stil got the same error:
<QueueItem>
<callNumber>349551</callNumber>
<errorMsg
i:nil="true" />
<link>/Call/349551</link>
<page>Call</page>
<siteCity>Burnaby</siteCity>
<status>Open</status>
<summary>Mobile Application Research</summary>
</QueueItem>
<QueueItem>
<callNumber>376209</callNumber>
<errorMsg
i:nil="true" />
<link>/Call/376209</link>
<page>Call</page>
<siteCity>Burnaby</siteCity>
<status>Open</status>
<summary>July 2012 ASD Calls of the Month.</summary>
</QueueItem>
Would someone guide me or show me the example or link to solve this problem. Thanks in advance.
It may not be a good solution, but it fix my issue. I changed the 'schemas.datacontract.org/2004/07/DataObjects' to 'schemas.datacontract.org/2004/07/CMSPhoneApp.DataObjects'. Therefore it can find the class location. Also I need to create ArrayOfQueueItem Class for deserialization.
Related
I updated my solution from .Net 3.1 to .Net 6. Also updated the Npgsql nuget package from 5.0.10 to 6.0.4 as part of this upgrade.
Since then, I am receiving an error "Can't cast database type character to Guid" when I try to retreive data from the database.
My mapping in the context file is
entity.Property(e => e.UserId).HasColumnName("user_id").HasColumnType("CHAR(36)");
In C# class, this property is a GUID.
Is there some mapping update with the newer version of npgsql?
EF Core has a built-in value converter which implicitly converts .NET Guid properties to text columns (see docs. Note that PostgreSQL has a full UUID type - that's a better way to store GUIDs in the database, rather than as text.
This works in EF Core 6.0.4 - if you're encountering trouble, please produce a minimal, runnable code sample and add it to your question above.
Working 6.0 code:
await using var ctx = new BlogContext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();
ctx.Blogs.Add(new Blog { Guid = Guid.NewGuid()});
await ctx.SaveChangesAsync();
_ = await ctx.Blogs.ToListAsync();
public class BlogContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseNpgsql(#"Host=localhost;Username=test;Password=test")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
}
public class Blog
{
public int Id { get; set; }
public string Name { get; set; }
[Column(TypeName = "CHAR(36)")]
public Guid Guid { get; set; }
}
I am using Ant design Blazor and trying to create a child component and apply validation as usual from the parent component. Below is my code.
Child component
<AntDesign.FormItem Label="#Label">
<AntDesign.InputNumber Step="Step" Min="Min" #bind-Value="#Amount" OnChange="OnAmountChanged"
TValue="double?" />
</AntDesign.FormItem>
#code {
[Parameter]
public string Label { get; set; }
[Parameter]
public double Step { get; set; }
[Parameter]
public double Min { get; set; } = double.MinValue;
[Parameter]
public double Max { get; set; } = double.MaxValue;
[Parameter]
public string Placeholder { get; set; }
[Parameter]
public double? Amount { get; set; }
[Parameter]
public EventCallback<double?> AmountChanged { get; set; }
private async Task OnAmountChanged(double? value)
{
await AmountChanged.InvokeAsync(value);
}
}
Parent component
<AntDesign.Content>
<AntDesign.Form LabelColSpan="8"
WrapperColSpan="16"
Model="#exchangeRate"
OnFinish="#OnFinish"
OnFinishFailed="#OnFinishFailed">
<Validator>
<FluentValidator TValidator="ExchangeRateValidator" />
</Validator>
<ChildContent>
<AntDesign.FormItem Label="Rate">
<AntDesign.InputNumber Step="0.1" Min="0" #bind-Value="context.Rate" Formatter="RateFormat" />
</AntDesign.FormItem>
<FormItem Label="Date">
<DatePicker TValue="DateTime?" Format="yyyy/MM/dd" #bind-Value="context.Date">
</DatePicker>
</FormItem>
<InputNumberComponent Label="Rate" #bind-Amount="context.Amount"></InputNumberComponent>
<Button Type="primary" HtmlType="submit">Submit</Button>
</ChildContent>
</AntDesign.Form>
</AntDesign.Content>
As can be seen, I am binding the value of Amount to the component directly using #bind-Amount. But, when I click on the Submit button, it triggers validation and displays the validation messages on the first two fields but not on the child component.
Any pointers?
A colleague of mine was able to help with the answer. We had to look into the Ant Design Blazor source for the FormItem to figure out what is happening and get the answer. To get the validation messages, Ant is depending on the validation messages in the EditContext but retrieving them using the Any input control's FieldIdentifier.
The FieldIdentifier is created by Ant using the ValueExpression of the control and not the value. When we set the value, from the child control, the Value taken as the ValueExpression and in turn, when looking for validation messages, it searches for error messages against Value and not the actual model field.
Setting the ValueExpression to a binding expression when binding to the child property fixed the issue.
Im using web API to return data in azure table storage. Im returning a class that I 'm inheriting TableEntity in a class and adding properties but want to keep to the .Net convention of capitalized property names but also keep to the JavaScript/json convention of lowercase properties names.
I've tried adding the Json.net property attributes to the class but it appears to be ignored. E.g.:
[JsonProperty("id")]
public string ID {get;set;}
If the instance has a value set on ID, null is represent in the serialized result.
According to your description, I tested this issue on my side and found it works well on my side and Azure. Here is my detailed steps, you could refer to it.
Create a controller named UserInfoController in the Web API application with the Get function like this:
// GET: api/UserInfo
[HttpGet]
public async Task<string> Get()
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(<your-Storage-ConnectionString>);
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable cloudTable = tableClient.GetTableReference("UserInfo");
TableQuery<User> query = new TableQuery<User>()
.Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Grade Four"));
var results =await cloudTable.ExecuteQuerySegmentedAsync(query, null);
//Serialize the object to string by using the latest stable version of Newtonsoft.Json
string jsonString = JsonConvert.SerializeObject(results);
return jsonString;
}
Entity
public class User : TableEntity
{
public User(string partitionKey, string rowKey)
{
this.PartitionKey = partitionKey;
this.RowKey = rowKey;
}
public User() { }
[JsonProperty("id")]
public long ID { get; set; }
[JsonProperty("username")]
public string UserName { get; set; }
[JsonProperty("phone")]
public string Phone { get; set; }
[JsonProperty("age")]
public int Age { get; set; }
}
Result
Deploy the Web API application to Azure, then you could find the following result by calling the function via Fiddler.
In summary, please try to check the version of Json.NET you are using. If you aren't using the latest (9.0.1), then please try to upgrade to the latest version and run your application again to find whether it could work as expected.
FYI - while this doesn't answer the direct answer of how to get TableEntity to respect JSON.net attributes... I was able to solve the use case by overriding the ReadEntity and WriteEntity method in the inherited class:
e.g.
public class User : TableEntity{
//Upper case name
public string Name {get; set};
public override void ReadEntity(IDictionary<string, AzureTableStorage.EntityProperty> properties, OperationContext operationContext){
base.ReadEntity(properties, operationContext);
//lower case
this.Name = properties["name"];
}
I'm using the new MVC6 framework with Visual Studio 2015, and suddenly all my Data Annotations stopped working. All of them, without me changing the code.
public sealed class RegisterUser
{
[Required(ErrorMessage = "required")]
[RegularExpression(#"^((.|\n)*)$", ErrorMessage = "regex")]
[StringLength(32, MinimumLength = 3, ErrorMessage = "length")]
public string Name { get; set; }
...
}
And
[Route(Address + "/membership")]
public class MembershipController : Controller
{
// POST [address]/membership/register
[AllowAnonymous]
[HttpPost("Register")]
public IActionResult Register([FromBody]RegisterUser model)
{
// Validate the input model.
if (model == null)
return ...
if (!ModelState.IsValid)
return ...
// Always get HERE
}
}
Why, on earth, do I pass the "ModelState.IsValid" test (it always evaluates to true) ?
For example, I'm passing Name="x", and it still evaluates to true. As if the annotations aren't there.
Does it relate to using MvcCore ?
Frustrating as it is, I forgot that changing to 'core' project strips out many of the common features. And so, in Startup.cs, add
services.AddMvc()
or
services.AddMvcCore().AddDataAnnotations()
Depending on your usage.
Was experiencing the same issue and commented this following line of code
services.AddControllersWithViews(options => {
// options.ModelValidatorProviders.Clear();
}).AddRazorRuntimeCompilation();
I don't remember why I added this line of code...
{ get; set; } Required
I know the OP has NOT made this error but I have just wasted a number of hours...
Need to use public string Name { get; set; } and not public string Name;
I'm using ASP Core 3.1.
I have a WCFService that returns sundry types of Data in this kind of a way:
[DataContract( Name="Amazing", NameSpace="http://schemas.myorganisation.com/DataContract1")]
Public class AmazingDto
{
[DataMember( Order=0, IsRequired=true )]
public string Name { get; set; }
[DataMember( Order=0, IsRequired=true )]
public bool IsAmazing { get; set; }
}
And then
[DataContract ( Name="GetAmazingListResponse", NameSpace="http://schemas.myorganisation.com/DataContract1")]
Public class GetAmazingListResponseDto
{
[DataMember(Order=0, IsRequired-true, EmitDefaultValue=False)]
public ICollection<AmazingDto> AmazingList{ get; set; }
}
Also
[DataContract(Name = "Response", Namespace = "http://schemas.myorganisation.com/DataContract1")]
public class ResponseDto<TData> : BaseResponseDto
{
public ResponseDto();
[DataMember(Order = 0, IsRequired = true)]
public StatusDto Status { get; set; }
[DataMember(Order = 1, IsRequired = false, EmitDefaultValue = false)]
public TData Data { get; set; }
}
And then
public ResponseDto<GetAmazingListResponseDto> GetAmazingList()
{
var response = new ResponseDto<GetAmazingListDto>
{
Status = new StatusDto { StatusResult = StatusResultEnum.Success },
Data = new GetAmazingListResponseDto
{
AmazingList = new List<AmazingDto>
{
new AmazingDto { Name="Ponies", IsAmazing=true },
new AmazingDto { Name="Glenatron", IsAmazing=false }
}
}
};
return response;
}
Now when I make a call to that service using a tool like SoapUI I get exactly the response I might expect.
<GetAmazingListResult xmlns:a="http://schemas.myorganisation.com/DataContract1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Status>
<a:StatusResult>Success</a:StatusResult>
</a:Status>
<a:Data xmlns:b="http://schemas.myorganisation.com/DataContract1">
<b:AmazingList>
<b:Amazing>
<b:Name>Ponies</b:Name>
<b:IsAmazing>true</b:IsAmazing>
</b:Amazing>
<b:Amazing>
<b:Name>Glenatron</b:Name>
<b:IsAmazing>false</b:IsAmazing>
</b:Amazing>
</b:AmazingList>
</a:Data>
</GetAmazingListResult>
However, when I use Visual Studio 2010 to create a Service Reference and make a call like this:
var client= new FindoutAmazingnessServiceClient();
var response = client.GetAmazingList();
What I find is that response only has two properties, Status and ExtensionData. I have seen this described in other SE questions but the answer was always that something was missing a DataContract or DataMember on the data contracts. I definitely have those, so something else must be happening so that VS.Net can't see my classes. I have tried referencing the DTO library that contains these files and trying to configure the reference to match types with that library, but it makes no difference.
Where am I going wrong?
Just Add [DataMember] to your data members. It solved my problem.
The solution I have found is, as I am working against my own WCF service, to import the interfaces into my client and then use ChannelFactory to set up the connection. This works very well in my scenario, but doesn't solve the problem I am raising with this question.