1

I am currently using Visual Studio 2015 and is building a website. I have tried using OperationContracts and ServiceContracts with a 3-tier architecture, however, I could only do the basic things (Create, Retrieve, Update, Delete with normal strings/int).

I want to ask, for the Web Service WCF, is it possible to retrieve a PDF File from another database?

Here’s the scenario I’m working towards:

  1. Company A uses Web service (WCF) to retrieve Invoice data(All in different attributes e.g. InvoiceNum, PaymentAmt etc) from Supplier A.
  2. Company A uses external API to fill in all the fields into a template and download as a PDF file.
  3. Company A uses Web service (WCF) to insert the Invoice PDF into the Supplier A’s database and store as a PDF file.
  4. Company A stores the PDF as a type BLOB in their own database (SQL LocalDB).

Is the above scenario possible? If it’s possible, are there any guidelines/known links I can refer/try out to achieve the scenario?

John Arc
  • 173
  • 1
  • 17
  • https://www.codeproject.com/Tips/862823/Transfer-File-Using-WCF – Robert Harvey Dec 27 '18 at 15:19
  • @RobertHarvey Hello, thanks for the link! However, I am building a website and not on console. Are there any other links for ASP.NET websites? – John Arc Dec 27 '18 at 15:26
  • Mmm... You'd better dig in and learn how this works. You're the one who is going to have to maintain it, and there's lots of code to chew on at that link. – Robert Harvey Dec 27 '18 at 15:29
  • @RobertHarvey Yeah, I've never used console before so the codes look kind of alien to me haha. Nonetheless, thanks for the help! I'll look into the codes in the link. – John Arc Dec 27 '18 at 15:39

1 Answers1

0

Pdf could been seen as a file. You could use byte[] to transfer file. Below is my simple sample.

My contract.

[ServiceContract()]
public interface  IFileUpload
{
    [OperationContract]
    void Upload(byte[] bys);
}

My service. AspNetCompatibilityRequirementsMode.Allowed is used to enable HttpContext or it will be null. Here I directly save the file in the server, if you want to save it in sqlserver , just use varbinary field to save the byte[] of pdf file.

 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class FileUploadService : IFileUpload
{
    public void Upload(byte[] bys)
    {
        string filename = Guid.NewGuid().ToString()+".pdf";
        File.WriteAllBytes(HttpContext.Current.Request.MapPath("/upload/") + filename, bys);

    }
}

My web.config of wcf service. The bindingconfiguration ECMSBindingConfig is used to enable uploading large data or the service doesn't allow too large data. serviceHostingEnvironment's aspNetCompatibilityEnabled should also be set to true or HttpContext will be null.

 <service name="Service.CalculatorService" >
    <endpoint  binding="basicHttpBinding"  bindingConfiguration="ECMSBindingConfig" contract="ServiceInterface.ICalculatorService"></endpoint>
  </service>
  <bindings>
  <basicHttpBinding>
    <binding name="ECMSBindingConfig"  allowCookies="false"  maxBufferPoolSize="2147483647" maxBufferSize="2147483647"
      maxReceivedMessageSize="2147483647" bypassProxyOnLocal="true"  >
      <readerQuotas maxArrayLength="2147483647" maxNameTableCharCount="2147483647"
          maxStringContentLength="2147483647" maxDepth="2147483647"
          maxBytesPerRead="2147483647" />
      <security mode="None" />
    </binding>
  </basicHttpBinding>
</bindings>
 <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
   />

My client. User webform as a sample.

<form id="form1" runat="server">
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:Button ID="Button1" runat="server" Text="upload" OnClick="Button1_Click" />
</form>

Code behind. Here I use channelFacotory ,it is similar to client generated by visual studio

 protected void Button1_Click(object sender, EventArgs e)
    {
        HttpPostedFile file = FileUpload1.PostedFile;
        using (ChannelFactory<IFileUpload> uploadPdf = new ChannelFactory<IFileUpload>("upload"))
        {   
            IFileUpload fileUpload = uploadPdf.CreateChannel();
            byte[] bys = new byte[file.InputStream.Length];
            file.InputStream.Read(bys, 0, bys.Length);
            fileUpload.Upload(bys);
        }
    }

Web.config of client.

 <client>
  <endpoint name ="upload" address="http://localhost:62193/uploadPdf.svc" binding="basicHttpBinding" contract="ServiceInterface.LargeData.IFileUpload" />

</client>

I assume the user uploads pdf, if you want to upload other files , you could add file extension as the service's parameter. Other operation should be similar.

Ackelry Xu
  • 756
  • 3
  • 6