65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 COM 组件消费 SOAP 服务

starIconstarIconstarIconstarIconstarIcon

5.00/5 (4投票s)

2016年12月9日

CPOL

1分钟阅读

viewsIcon

15611

本文档解释了如何通过使用用 C# 编写的 COM DLL,从纯 VisualBasicScript 消费 WebService (SOAP)。

当然,有一种方法可以通过编写 XML 文件并将其作为 HTTP POST 发送到服务器来“手动”发送请求到 SOAP 服务,例如这样

然而,这是一种困难的方法,你需要处理像

<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"" 
xmlns:fruit=""http://fabrikam.net""><soap:Header/><soap:Body>
<fruit:findBananasbyId><Id>" & bananaId & /Id></fruit:findBananasById></soap:Body></soap:Envelope>"

这样复杂的结构,而且相信我,这仅仅是个开始。:)

另一方面,在 Visual Studio 中创建一个消费 SOAP 服务的客户端应用程序非常简单。例如,请查看此链接

现在你可以创建一个 .NET 库 (DLL),但是如何在 VBS 中使用它呢?

答案是 - **DLL 需要是一个 COM 对象!**

假设我们有一个这样的 COM-DLL,我们可以轻松地使用它

Dim oComSoap
Set oComSoap = CreateObject("myComInterface")
oComSoap.SendRequest

当然有两点/陷阱

  1. .NET WCF 应用程序有一个app.config 文件,其内容如下
    < system.serviceModel >
            < bindings >
              < wsHttpBinding >
                   < binding name = "WSHttpBinding_PurchaseInvoiceService" />
              </ wsHttpBinding >
            </ bindings >
            < client >
              < endpoint address = 
              "http://ax5-w8r2-01.contoso.com/MicrosoftDynamicsAXAif50/purchaseinvoiceservice.svc"
                      binding = "wsHttpBinding" 
                      bindingConfiguration = "WSHttpBinding_PurchaseInvoiceService"
                      contract = "PurchaseInvoiceService.PurchaseInvoiceService" 
                      name = "WSHttpBinding_PurchaseInvoiceService" >
                  < identity >  
                    < userPrincipalName value = "Administrator@contoso.com" />
                  </ identity >
              </ endpoint >
            </ client >
    </ system.serviceModel >

    你的独立 DLL 不会使用app.config,应该通过创建一个相应的类来替换它

    PurchaseInvoiceServiceClient _oClient;
    
    WSHttpBinding oBinding = new WSHttpBinding();
    oBinding.Name = "WSHttpBinding_PurchaseInvoiceService";     
    string strEndPoint = 
    "http://ax5-w8r2-01.contoso.com/MicrosoftDynamicsAXAif50/purchaseinvoiceservice.svc";
    EndpointAddress oEndPoint = new EndpointAddress(strEndPoint);
    _oClient = new PurchaseInvoiceServiceClient(oBinding, oEndPoint);
  2. .NET WCF 应用程序将入口点/主函数标记为 [STAThread],这意味着应用程序的 COM 线程模型是单线程公寓 (STA)。

    你需要对 DLL 的每个 SOAP 接口执行此操作

    [STAThread]
    public bool SendRequest()

关注点

© . All rights reserved.