OData v4 Web API 2 dynamic properties do not persist - asp.net-web-api

I have tried to implement the sample from
Open Types in OData v4 with ASP.NET Web API
I am using the ODataConventionModelBuilder and included an entity with property:
public IDictionary<string,object> properties { get; set; }
I can post a new entity including dynamic properties; they show up in the Post Controller method, but they won't persist in the database. Any ideas on how to fix this?

Related

Return IQueryable<SomeEntity> failed

Currently I am using aspnetboilerplate to enhance a project which include both mvc and web api, after port web api code to abp, I found the controller cannot return entity successfully, the code like below:
public IQueryable<SomeEnity> GetEntities()
{
return _repository.GetAll().Where(e => e.TagId == 1);
}
The exception from postman is popular:
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
After some searching, I get it work with introducing a SomeEntityDto (another class), because the entity number for the web api so many that I don't want introduce so many dto for each entity.
The question is:
Why the same code work with original project (EF and ASP.NET MVC) but failed with abp framework? Any solution?
Note: I also tried this, but not work.

Http Verb Attribute Not Working

I have decorated my service interface with Http Verb Attributes, but is not working.
Every method is treated as Post verb.
I'm using AspNetCore 1.1 and Abp packages 2.3.0
public interface ISettlementAppService : IApplicationService
{
Task<PagedResultDto<SettlementListDto>> GetPaged(GetSettlementInput input);
[HttpDelete]
Task Cancel(EntityDto<string> input);
}
For AspNet Core, add these attributes to the application service class, not to interface. Because they are handled by AspNet Core MVC (not by ABP) and it does not know about interfaces.
From the documentation (https://aspnetboilerplate.com/Pages/Documents/AspNet-Core#controllers):
Note: Previously, dynamic web api system was requiring to create service interfaces for application services. But this is not required for ASP.NET Core integration. Also, MVC attributes should be added to the service classes, even you have interfaces.
Do it like this;
public class SettlementAppService : ISettlementAppService
{
[HttpDelete]
Task Cancel(EntityDto<string> input){
//...
}
}

ASP.NET boilerplate: IInputDTO is not triggering Automatic Validation of DTO

I have created a DTO which implements IInputDTO.
public class CreateUserModel : IInputDto
When I receive a service call on my web api layer it doesn't seem to validate the DTO as it just goes through the flow of the service method
public async Task<HttpResponseMessage> Create(CreateUserModel createUserRequest)
I'm running ASP.NET Boilerplate 0.8.3
The support for ASP.NET boilerplate has answered me that this is currently not possible. Only classes which implement the IApplicationService are currently able to do this.
Link to issue on ASP.NET Boilerplate's issue page

Access the default JSON.NET serializer in ASP.NET Web API

I have a ASP.NET web API project and since it does not support having 2 body parameters, I use a JObject parameter and then extract the actual parameters from it. Like this.
Public bool mymethod(JObject data){
myclassA a = data["a"].toObject<myclassA>();
myclassA b = data["b"].toObject<myclassB>();
}
But the 2 class types implement ISerializable and I need the JSON.NET to ignore it. I have set up the default JSON.NET serializer to do that and it works fine when serialization is done automatically.
But I need to get a reference to the built in JSON.NET serializer so that I could use it like this in the above code.
myclassA b = data["b"].toObject<myclassB>(defaultSerializer);
Currently I create a new instance of the JSON.NET serializer and use it. But how can I get a reference to the default built in serializer in the asp.net WEB API ?
Also I cannot change anything in class types as this is sort of a legacy app that I'm converting to web api. Thanks.
Try this:
JsonSerializer serializer = JsonSerializer.Create(Configuration.Formatters.JsonFormatter.SerializerSettings);
That should give you the same serializer Web API uses.

How to enable Dependency Injection for ServiceRoute in MVC3 and WCF Web API

I am creating a MVC3 website that will expose a REST API using WCF Web API.
To register routes to the REST API I add code to the Global.asax similar to the code below.
routes.MapServiceRoute<RelationsService>("relations");
This works well enough but i need to use a DI approach to inject the dependencies that the Service depends on.
As you can see in the code above the MVC framework is creating the instance of the RelationsService but this should be done by the DI container.
Does anyone know how to configure MVC3 so that my own DI container is used for creating the instances of the Services?
You have to extend your current service registration call with an IHttpHostConfigurationBuilder that has been created with an IResourceFactory.
var configurationBuilder = HttpHostConfiguration.Create()
.SetResourceFactory(new ResourceFactory());
routes.MapServiceRoute<RelationsService>("relations", configurationBuilder);
Then if you for instance use StructureMap as preferred IoC/DI tool you can just ask for the service in the GetInstance method.
public class ResourceFactory : IResourceFactory
{
public object GetInstance(Type serviceType, InstanceContext instanceContext, HttpRequestMessage request)
{
return ObjectFactory.GetInstance(serviceType);
}
}

Resources