How to exclude Json properties from WebApi response using a custom ContractResolver

I am currently working on a Backend API that acts as a middleman to a Front API and a DocumentDB database (the structure is shown in the above image).

 

I had the following class which is used to send data to both the Front API and to the DocumentDB. Some of the fields needed to be hidden when returning a response back to the Front API, since they weren’t useful for the user and for security concerns. First I tried using [JsonIgnore] and [IgnoreDataMember] (as seen below), which successfully excluded the required fields from the response, but using them was also excluding the same fields when sending the data to DocumentDB, so it was a no-go. Continue reading

The correct way to implement StructureMap in .NET Core WebApi when having multiple projects

StructureMap is an IoC/DI container for .Net which has been a long time around. I’ve been using it for these past few years, and after all this time, I found out the correct way to implement it.

This is how I used it before and I’m sure the majority of you are currently using the same methods. In the Registry, I would register each interface and it’s inherited class by calling a “config.for” for each service as follows:

    config.For(typeof(ITestService)).Add(typeof(TestService));
    config.For(typeof(IUserService)).Add(typeof(UserService));
    config.For(typeof(ITenantService)).Add(typeof(TenantService));
    config.For(typeof(IProjectService)).Add(typeof(ProjectService));

And whenever new interfaces and services are added, they need to be registered using a new “config.for” line.

That was the old way, of how I did things. This is the new way. Continue reading