1

I am trying to set up a Web API to be used for capturing the Clocking In and Clocking Out times of Employees after scanning a QR code.

I have set up this SQL Server database to test out the logic for Web API and IONIC 3 project.

My SQL Server code are as follows:

-- Creating table 1 'FarmWorker'
CREATE TABLE [dbo].[FarmWorker] 
(
    [FarmWorkerNum] int primary key identity(1,1),
    [FarmWorkerFName] varchar (15) not null,
    [FarmWorkerLName] varchar (15) not null,
    [FarmWorkerIDNum] char (13) not null
);
GO

-- Creating table 2 'AttendenceSheet'
CREATE TABLE [dbo].[AttendenceSheet] 
(
    [AttendenceSheetID] int primary key identity(1,1),
    [ClockInTime] datetime not null,
    [ClockOutTime] datetime not null,
    [FarmWorkerNum] int not null 
         FOREIGN KEY REFERENCES [FarmWorker](FarmWorkerNum)
);
GO

My Web API has been set up using Visual Studio 2015 and ASP.Net Web API 2.

My controller code for the Web API are as follows:

namespace AttendanceService.Controllers
{
    public class FarmWorkerController : ApiController
    {
        public IEnumerable<FarmWorker> Get()
        {
            using (AttDBEntities entities = new AttDBEntities())
            {
                 return entities.FarmWorkers.ToList();
            }
        }

        public FarmWorker Get(int id)
        {
            using (AttDBEntities entities = new AttDBEntities())
            {
                return entities.FarmWorkers.FirstOrDefault(e =>Convert.ToInt32(e.FarmWorkerIDNum) == id);
            }
        }
    }
}

When I run my Web API in the browser and attempt to run the first Get (to return a list of all FarmWorkers), I get the following error:

An error has occurred

System.InvalidOperationException: the 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.

Type 'System.Data.Entity.DynamicProxies.FarmWorker_DA4B06B144222E86714953DE48C1952FFB 59C5CE216BAE8D8D506D8AEE9CEEBB' with data contract name 'FarmWorker_DA4B06B144222E86714953DE48C1952FFB59C5CE216BAE8D8D506D8AEE9CEEBB:htt p://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer.

Does anyone know how to resolve this error? Any help will greatly be appreciated.

Thank you in advance.

[EDIT / UPDATE 1] - 2 hour after posting

I have managed to resolve the error message by adding in this code into the WebApiConfig.cs file:

config.Formatters.Remove(config.Formatters.XmlFormatter);
config.Formatters.Add(config.Formatters.JsonFormatter);

config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;

QUESTION: Why do I receive an error when it is using XML format over JSON format? If possible, I would like to use the XML format, is there any way to make this possible ?

Thanks in advance again.

3
  • Much better now ! Commented Aug 11, 2018 at 16:20
  • 1
    It does look way better now! Thank alot for the edits @marc_s Commented Aug 11, 2018 at 16:24
  • I added in 2 lines of code that managed to resolve the error I was getting. config.Formatters.Remove(config.Formatters.XmlFormatter); config.Formatters.Add(config.Formatters.JsonFormatter); My question now is, why does it give me an error when in XML format, but now when it is in JSON Format? I would really like to use the XML Format over JSON format. Is there any way to make this possible? Commented Aug 11, 2018 at 17:45

2 Answers 2

1

My guess is that whatever you're using to call the endpoint (e.g. your browser, Fiddler, Postman), is specifying a Content-Type of application/json in the request. Try inspecting the request to see if that is the value of the header being sent.

If you really do wish to use XML, add the XML formatter again and make a request using a Content-Type of application/xml, which should return the content in XML format for you.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the response. I am using my browser as my endpoint to call the API. I have a QR Code scanning app set up using IONIC 3 and then call the API when clicking a button. I can successfully retrieve data and display it in my mobile app (IONIC 3) with the JSON data. I am not sure how to specify the Content-Type of the request. I have added in these lines of code into my WebApiConfig.cs EnableCorsAttribute cors = new EnableCorsAttribute("*", "*", "*"); config.EnableCors(cors);
@ArmandJordaan Download Postman, and follow through the tutorial in the documentation to send an API request specifying the Content-Type. It's useful to know how to use a tool like this when developing any API.
Thank you very much @John H
0

Solving the issue of 'ObjectContent1', I had to insert code into the WebApiConfig.cs

config.Formatters.Remove(config.Formatters.XmlFormatter);
config.Formatters.Add(config.Formatters.JsonFormatter);

config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.