0

I've a WCF service with one method which will be called from multiple web API controllers like in the below code.

    public string Print(PdfPrinterRequest _request)
    {
        PdfPrinterService.PdfPrinterClient _Client = new PdfPrinterService.PdfPrinterClient();
        PdfPrinterResponse _response = new PdfPrinterResponse();
        return _Client.Print(_request.Document, out _pdfResponse);
    }

PdfPrinterRequest(Document class) is the entity which I'm passing to get the response message from WCF service.Currently the document class holds few properties(REquest Header). I would like to call the same Print method from other API and pass type 'Customer' to WCF service. How can i achieve this? Can anyone please suggest me the correct implementation?

Below is my WCF service code,

   public class PdfPrinterService : IPdfPrinter
   {
      public PdfPrinterResponse Print(PdfPrinterRequest request)
      {
        return PdfPrinterFacade.PrintPdf(request);
      }
   }

   public static PdfPrinterResponse PrintPdf(PdfPrinterRequest request)
    {
        PdfPrinterResponse response = new PdfPrinterResponse();
        //Process the request and send back the response message
    }

    [MessageContract]
    public class PdfPrinterRequest
    {
    private Document _document;

    [MessageBodyMember]
    public Document Document
    {
        get { return _document; }
        set { _document = value; }
    }
   }

How to pass a dynamic class object as a parameter in place of PdfPrinterRequest which is no bound to only one type(Document)? Please suggest.

Thanks,

Silk Road
  • 13
  • 11
  • Try to use method overloads if that is possible. – ViRuSTriNiTy Jul 15 '16 at 08:04
  • Do you have a common method in all objects like `GetRepsonse()`, or at least can you change them to have such a common method ? – Zein Makki Jul 15 '16 at 08:05
  • @ViRuSTriNiTy , I can have overloads, But dont know how many methods i end up with, Rather i would like to have one method to accept any type of class object. Thanks – Silk Road Jul 15 '16 at 08:13
  • @user3185569 : I'm maintaing Request and Response objects where Request will hold all the input parameters and Response is common for all Requests with a MEssage and Info as return types. – Silk Road Jul 15 '16 at 08:15
  • @PuliganP In this case you need to have a common base class `Foo` for all request classes with certain functionality you need to process the request. `PdfPrinterRequest` would then be extending `Foo`. – ViRuSTriNiTy Jul 15 '16 at 08:21
  • @ViRuSTriNiTy, Can you please post an example.. Tahnks – Silk Road Jul 15 '16 at 08:35
  • @PuliganP Sadly using a base class will not work according to this question: http://stackoverflow.com/q/1321278/3936440, please have a look at the first answer refering to the `MessageContract` issue. – ViRuSTriNiTy Jul 15 '16 at 08:46
  • @ViRuSTriNiTy, So, I need to have overlod methods with different Requests? Right – Silk Road Jul 15 '16 at 08:55
  • Any example please to make it dynamic – Silk Road Jul 15 '16 at 10:51

1 Answers1

0

Warning: The NetDataContractSerializer poses a significant security risk and should be avoided.


If this service does not need to be interoperable, you can switch to NetDataContractSerializer, which uses full .NET type information, and is able to serialize many more types (but not any type - that's impossible). Grab the UseNetDataContractSerializerAttribute from this answer and apply like so:

[UseNetDataContractSerializer]
public class PdfPrinterService : IPdfPrinter
{
   public PdfPrinterResponse Print(PdfPrinterRequest request)
   {
     return PdfPrinterFacade.PrintPdf(request);
   }
}

[MessageContract]
public class PdfPrinterRequest
{
    [MessageBodyMember]
    public object Document { get; set; }
}
Eli Arbel
  • 22,391
  • 3
  • 45
  • 71
  • In the above Print method, I'm now passing fixed type 'PdfPrinterRequest', Rather i would like to pass any type of Request. Please suggest how to do this? – Silk Road Jul 16 '16 at 05:55
  • Ok, I've changed MessageContracts to DataContract and I removed Document class property from the PdfPrinterRequest class and put request related properties. Can you quote the full example here? – Silk Road Jul 16 '16 at 08:39
  • I just need to pass a dynamic Type to Print method as input parameter and execute some business logic based on the passed type and return data. – Silk Road Jul 16 '16 at 08:41