1

我目前正在使用 Visual Studio 2015 并且正在构建一个网站。我曾尝试使用具有 3 层架构的 OperationContracts 和 ServiceContracts,但是,我只能做基本的事情(使用普通字符串/int 创建、检索、更新、删除)。

我想问一下,对于Web Service WCF,是否可以从另一个数据库中检索PDF 文件?

这是我正在努力的场景:

  1. 公司 A 使用 Web 服务 (WCF) 从供应商 A 处检索发票数据(全部在不同的属性中,例如 InvoiceNum、PaymentAmt 等)。
  2. A公司使用外部API将所有字段填写到模板中并下载为PDF文件。
  3. 公司 A 使用 Web 服务 (WCF)发票 PDF 插入供应商 A 的数据库并存储为 PDF 文件。
  4. 公司 A 将 PDF 作为类型 BLOB 存储在他们自己的数据库 (SQL LocalDB) 中。

上述情况可能吗?如果可能,是否有任何指南/已知链接我可以参考/尝试来实现该场景?

4

1 回答 1

0

Pdf 可以被视为一个文件。您可以使用 byte[] 来传输文件。下面是我的简单示例。

我的合同。

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

我的服务。AspNetCompatibilityRequirementsMode.Allowed 用于启用 HttpContext,否则将为空。这里我直接将文件保存在服务器中,如果要保存在 sqlserver 中,只需使用 varbinary 字段保存 pdf 文件的 byte[] 即可。

 [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);

    }
}

我的 wcf 服务的 web.config。bindingconfiguration ECMSBindingConfig 用于启用上传大数据或服务不允许太大数据。serviceHostingEnvironment 的 aspNetCompatibilityEnabled 也应设置为 true,否则 HttpContext 将为空。

 <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"
   />

我的客户。用户网络表单作为示例。

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

后面的代码。这里我使用的是 channelFacotory ,它类似于 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。

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

</client>

我假设用户上传 pdf,如果你想上传其他文件,你可以添加文件扩展名作为服务的参数。其他操作应该类似。

于 2018-12-28T09:09:02.323 回答