vendredi 19 août 2016

Posting any type of request object as parameter to the Web API

I have a web API which takes a class object as an input parameter and posts it to the WCF service operation contract. The WCF service is designed such that it takes any type of request and does internal mapping of what type of request has come and what code needs to be executed. The logic for that is given below.

    public class PdfPrinterService : IPdfPrinter
    {
    public PdfPrinterResponse Print(PdfPrinterRequestBase request)
    {
        if (request is Request1)
        {
           //Process user report request and send back the response
        }
        if (request is Request2)
        {
          //Process request 2 and send back the response
        }
       return PdfPrinterFacade.PrintPdf();
    }
 }

  //IPdfPrinter
  public interface IPdfPrinter
   {
    [OperationContract]
    PdfPrinterResponse Print(PdfPrinterRequestBase request);
   }

  //PdfPrinterRequestBase
  [DataContract]
  [KnownType(typeof(Request1))]
  [KnownType(typeof(Request2))]
  public class PdfPrinterRequestBase : IRequest
  {
     [DataMember]
     public RequestHeader ReqHdr { get; set; }
  }

 //Web API Request1
  public PdfPrinterResponse Print(Request1 _request1)
    {
        PdfPrinterService.PdfPrinterClient 
        _Client = new PdfPrinterService.PdfPrinterClient();
        return _Client.Print(_request1);
    }

   //Web API Request2
    public PdfPrinterResponse Print(Request2 _request2)
    {
        PdfPrinterService.PdfPrinterClient _Client = 
        new PdfPrinterService.PdfPrinterClient();
        return _Client.Print(_request2);
     }

The above approach is okay for me, But I'm planning to have only 1 API which takes any type of object and passes it to the WCF service and the mapping that i did (if request 1, process and send back response else if request 2 process and sendback etc) should work with 1 API call. Any help is greatly appreciated to achieve this.

Aucun commentaire:

Enregistrer un commentaire