最新要闻

广告

手机

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

家电

.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 BasicHttpBinding19         {20             MaxBufferSize = int.MaxValue,21             ReaderQuotas = XmlDictionaryReaderQuotas.Max,22             MaxReceivedMessageSize = 2147483647L,23             AllowCookies = true24         };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 SessionHeader32         {33             sessionId = loginResult.sessionId34         };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

TopottomTopBottom

关键词: 密码过期 同步操作 命名空间