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

Posted 707wk

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.NET Core 3.1 通过 Web Service 读写 Salesforce 数据相关的知识,希望对你有一定的参考价值。

1. 下载 WSDL 文件.

 

 

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

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

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

 public class SalesforceCoreHelper
 
     private static SoapClient SoapClientInstance;
 
     private static SessionHeader SoapClientInstanceHeader;
 
     public static void Init()
     
         Console.WriteLine("登录中...");
         SoapClient soapClient = new SoapClient();
         LoginResult loginResult = soapClient.login(null, "用户名", "密码");
 
         if (loginResult.passwordExpired)
         
             throw new Exception("Salesforce 密码过期");
         
 
         BasicHttpBinding basicHttpBinding = new BasicHttpBinding
         
             MaxBufferSize = int.MaxValue,
             ReaderQuotas = XmlDictionaryReaderQuotas.Max,
             MaxReceivedMessageSize = 2147483647L,
             AllowCookies = true
         ;
         basicHttpBinding.Security.Mode = BasicHttpSecurityMode.Transport;
         
         EndpointAddress remoteAddress = new EndpointAddress(loginResult.serverUrl);
 
         SoapClientInstance = new SoapClient(basicHttpBinding, remoteAddress);
         
         SoapClientInstanceHeader = new SessionHeader
         
             sessionId = loginResult.sessionId
         ;
         
     
 

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

以上是关于.NET Core 3.1 通过 Web Service 读写 Salesforce 数据的主要内容,如果未能解决你的问题,请参考以下文章

Asp.net core 3.1 保护 API 和 Web 应用程序

如何将 .net core Web API 项目添加到现有的 .NET core (3.1) Web Application 项目中?

我应该如何保护我的 Web 应用程序(ASP.Net Core 3.1 MVC)?

NET Core 3.1 MVC 授权/身份验证,带有在单独的 Net Core 3.1 Web Api 中从外部获取的令牌 (JWT)

Wcf 服务在 .NET Core 3.1 控制台应用程序中工作,但在 ASP.NET Core 3.1 Web API 中无法工作

Asp.net core 3.1 中的路由