【问题标题】:How to consume SalesForce Webservice from .net如何从 .net 使用 SalesForce Webservice
【发布时间】:2016-10-11 06:35:07
【问题描述】:

我正在尝试从 .net 使用 SalesForce 网络服务。我真的是 SalesForce 的新手,对此并不太了解。如果有人可以帮助我逐步完成操作,那将非常有帮助。以下是我已经完成的步骤,我不确定我错过了什么。请帮忙。

  1. SalesForce Web 服务代码:

    global class SampleWebService
    {
    webservice static String sayHello(String Name)
    {
    return 'Hello ' + Name + ', welcome to Salesforce WebService.';
    }
    }

  2. 生成了 WSDL 文件。

  3. 创建了 .net 项目并添加了该 wsdl 的 Web 引用。

  4. C#代码:

    使用系统;
    使用 System.Collections.Generic;
    使用 System.Linq;
    使用 System.Web;
    使用 System.Web.UI;
    使用 System.Web.UI.WebControls;

    命名空间 SalesForceWebService
    {
    公共部分类主页:System.Web.UI.Page
    {
    SalesForceService.SampleWebServiceService objService = new SalesForceService.SampleWebServiceService();

        protected void Page_Load(object sender, EventArgs e)  
        {  
            lblHello.Text = objService.sayHello("ABC");  
        }  
    }  
    

    }`

【问题讨论】:

    标签: .net salesforce


    【解决方案1】:

    以下是我使用 Enterprise WSDL 连接 Salesforce 所执行的步骤

    1. 在您的 Salesforce 环境中生成企业 WSDL,如下所示
      设置 --> 开发 --> API --> 生成企业 WSDL。 将该网页另存为 XML。

    2. 在 Visual Studio 中,右键单击解决方案资源管理器中项目的引用,并将保存的 XML 文件添加为项目的服务引用。

    3. 类似的方式也为您的 apex 类生成 WSDL。并将其作为服务引用附加到项目中。 (如果您将任何 apex 类公开为 Web 服务,否则请忽略此步骤)

    4. 开始编写代码。

    在这个程序中,我试图调用一个用 Apex 编写的 webservice 方法, 它根据唯一 ID 获取用户对象并更新字段。

        using ConsoleApplication1.Salesforce;
        using System.Web.Services.Protocols;
        namespace ConsoleApplication1
        {
        class Program
        {
            public static SforceService binding;
            public static ConsoleApplication1.ApexServiceName.ApexClassService classBinding;
            static void Main(string[] args)
            {
                binding = new SforceService();
                LoginResult lr = null;
                binding.Timeout = 120 * 60000;
                try
                {
                    lr = binding.login(username,password);
                    Console.WriteLine("login success");
                }
                catch (SoapException ex)
                {
                    Console.WriteLine(ex.Message);
                }
    
        classBinding = new ConsoleApplication1.ApexServiceName.YOUR_APEXCLASS_NAME();
        binding.Url = lr.serverUrl; //I1
        binding.SessionHeaderValue = new SessionHeader(); 
        binding.SessionHeaderValue.sessionId = lr.sessionId;  //I2
        classBinding.SessionHeaderValue = new ConsoleApplication1.ApexClassService.SessionHeader();
        classBinding.SessionHeaderValue.sessionId = lr.sessionId;
        ConsoleApplication1.ApexServiceName.User Appusr = classBinding.getUserById("1234567");  //I3
        Appusr.FirstName = "MurthyFYI"; //I4
        Boolean? result = classBinding.updateUser(Appusr); //I3
        binding.logout();
        }
        }
        }
    

    I1) 登录结果包含为您的组织提供服务的虚拟服务器实例的端点。将绑定的 URL 设置到此端点。

    I2) 从登录结果中获取会话 ID,并将其设置为将用于所有后续调用的会话标头。

    I3) 调用您已为其生成 WSDL 的 Apex 类的 Web 服务方法。

    I4) 更新用户对象上的字段。

    更多信息可以参考我的博文@http://murthyvvr.blogspot.in/2013/11/calling-salesforce-webservice-from-c.html 我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-04
      • 1970-01-01
      • 2016-02-15
      • 1970-01-01
      • 1970-01-01
      • 2014-04-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多