.NET Core 3.1 通过 Web Service 读写 Salesforce 数据

1. 下载 WSDL 文件.

 

 

 2. 项目右键 -> 添加 -> 服务引用 -> WCF Web Service, 选择刚才下载的 WSDL 文件, 修改命名空间为 Salesforce.Core, 最后勾选 生成同步操作, 等待配置完成.

3. 找到生成的 Reference.cs 文件, 自动生成的代码有问题, 替换里面的 [][] 为 [].

4. 直接使用官网的示例代码读取数据, 会报 长度超过65536 错误, 需要添加 BasicHttpBinding 设置.

 1 public class SalesforceCoreHelper
 2 {
 3     private static SoapClient SoapClientInstance;
 4 
 5     private static SessionHeader SoapClientInstanceHeader;
 6 
 7     public static void Init()
 8     {
 9         Console.WriteLine("登录中...");
10         SoapClient soapClient = new SoapClient();
11         LoginResult loginResult = soapClient.login(null, "用户名", "密码");
12 
13         if (loginResult.passwordExpired)
14         {
15             throw new Exception("Salesforce 密码过期");
16         }
17 
18         BasicHttpBinding basicHttpBinding = new BasicHttpBinding
19         {
20             MaxBufferSize = int.MaxValue,
21             ReaderQuotas = XmlDictionaryReaderQuotas.Max,
22             MaxReceivedMessageSize = 2147483647L,
23             AllowCookies = true
24         };
25         basicHttpBinding.Security.Mode = BasicHttpSecurityMode.Transport;
26         
27         EndpointAddress remoteAddress = new EndpointAddress(loginResult.serverUrl);
28 
29         SoapClientInstance = new SoapClient(basicHttpBinding, remoteAddress);
30         
31         SoapClientInstanceHeader = new SessionHeader
32         {
33             sessionId = loginResult.sessionId
34         };
35         
36     }
37 }

5. 由于 .NET Core 3.1 与 .NET Framework 版本的方法参数不同, 所以使用扩展方法封装一下 SoapClient 的增删查改方法.

namespace SalesforceLib.Core
{
    public static class SalesforceCoreExtension
    {

        public static QueryResult Query(this SoapClient SoapClientInstance, SessionHeader header, string queryString)
        {

            SoapClientInstance.query(header, null, null, null, queryString, out QueryResult tmpQueryResult);

            return tmpQueryResult;
        }

        public static QueryResult QueryMore(this SoapClient SoapClientInstance, SessionHeader header, string queryLocator)
        {

            SoapClientInstance.queryMore(header, null, queryLocator, out QueryResult tmpQueryResult);

            return tmpQueryResult;
        }

        public static SaveResult[] Update(this SoapClient SoapClientInstance, SessionHeader header, sObject[] sObjects)
        {

            SoapClientInstance.update(header, null, null, null, null, null, null, null, null, null, null, null, null, sObjects, out LimitInfo[] tmpLimitInfoList, out SaveResult[] tmpSaveResultList);

            return tmpSaveResultList;
        }

        public static SaveResult[] Create(this SoapClient SoapClientInstance, SessionHeader header, sObject[] sObjects)
        {

            SoapClientInstance.create(header, null, null, null, null, null, null, null, null, null, null, null, sObjects, out LimitInfo[] tmpLimitInfoList, out SaveResult[] tmpSaveResultList);

            return tmpSaveResultList;
        }

        public static DeleteResult[] Delete(this SoapClient SoapClientInstance, SessionHeader header, string[] ids)
        {

            SoapClientInstance.delete(header, null, null, null, null, null, null, null, null, null, null, ids, out LimitInfo[] tmpLimitInfoList, out DeleteResult[] tmpSaveResultList);

            return tmpSaveResultList;
        }

    }
}

6. 按照官网 .NET Framework 版本的增删查改代码使用就行了

 

参考文章:

Step 4: Walk Through the Sample Code | SOAP API Developer Guide | Salesforce Developers

SoapClient.Login takes more than one minute - Salesforce Developer Community

posted @ 2023-02-02 23:44  707wk  阅读(133)  评论(0编辑  收藏  举报