0

In ASP.NET Core 6 Web API I have give this Soap Service, http://thirdparty:2022/HrmService/HrmService?WSDL to consume .

I used WCF connected service to generate it, I call it HrmServiceReference. Then I write this code:

DTO:

public class EmployeeDto
{
    public string EmployeeCode { get; set; }
    public string DepartmentCode { get; set; } = null;
}

EmployeeService - interface:

Task<EmployeeResponse> GetEmployeeDetailAsync(EmployeeDto model);

Implementation:

public class EmployeeService : IEmployeeService
{
    private readonly HrmServiceServiceClient _client;

    public EmployeeDetailService(HrmServiceServiceClient client)
    {
        _client = client;
    }

    public async Task<EmployeeResponse> GetEmployeeDetailAsync(EmployeeDto model)
    {
        var request = new QUERYEMPLOYEE_REQ();
        request.HRM_HEADER = new HRM_HEADERType();
        request.HRM_HEADER.USERNAME = "HRMUSN";
        request.HRM_HEADER.PASSWORD = "HRMPSW";

        request.HRM_BODY = new QUERYEMPLOYEE_HRM_BODY();
        request.HRM_BODY.OPR.EMP = model.EmployeeCode;
        request.HRM_BODY.OPR.DEP = model.DepartmentCode;

        var response = await _client.QueryEmployeeAsync(request);

        return response;
    }
}

Controller:

[Route("api/[controller]")]
[ApiController]
public class EmployeeController : ControllerBase
{
    private readonly IEmployeeDetailService _employeeDetailService;

    public EmployeeController(IEmployeeDetailService)
    {
        _employeeDetailService = employeeDetailService;
    }

    [HttpGet("EmpDetail")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [ProducesResponseType(StatusCodes.Status500InternalServerError)]
    public async Task<IActionResult> GetMyEmployeeDetailAsync([FromQuery] EmployeeDto model)
    {
        var result = await _employeeDetailService.GetEmployeeDetailAsync(model);
        return Ok(result);
    }
}

In Swagger, I entered the input parameter (EmployeeCode and DepartmentCode) and submitted.

But I got this error:

System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.

Both request.HRM_BODY.OPR.EMP = model.EmployeeCode and request.HRM_BODY.OPR.DEP = model.DepartmentCode are highlighted.

What could be the problem and how do I resolve it?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Bami
  • 383
  • 1
  • 4
  • 10
  • 1
    You need to instantiate a class on `request.HRM_BODY.OPR` as well. – rene Apr 17 '23 at 12:17
  • @rene - How do I do that? – Bami Apr 17 '23 at 12:18
  • You already did that three times, for the request, the header, and the body. – rene Apr 17 '23 at 12:20
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Yong Shun Apr 17 '23 at 12:21

0 Answers0