[英]How to consume SalesForce Webservice from .net
我正在嘗試從.net使用SalesForce Web服務。 我真的是SalesForce的新手,對此並不了解。 如果有人可以逐步幫助我,那將非常有幫助。 以下是我已經完成的步驟,但不確定自己錯過了什么。 請幫忙。
SalesForce Web服務代碼:
global class SampleWebService
{
webservice static String sayHello(String Name)
{
return 'Hello ' + Name + ', welcome to Salesforce WebService.';
}
}
生成了WSDL文件。
創建.net項目,並添加該wsdl的Web參考。
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"); } }
}`
以下是我執行的使用企業WSDL連接Salesforce的步驟
在您的Salesforce環境中生成企業WSDL,如下所示
設置->開發-> API->生成企業WSDL。 將該網頁另存為XML。
在Visual Studio中,在“解決方案資源管理器”中右鍵單擊項目的“引用”,然后將保存的XML文件作為服務引用添加到項目。
同樣的方式也為您的apex類生成WSDL。 並將其作為服務引用附加到項目。 (如果您將任何apex類作為Web服務公開,否則請執行此步驟)
在此程序中,我嘗試調用用Apex編寫的Web服務方法,該方法基於唯一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希望對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.