我使用的是使用.Net Framework3.5的C#编码的3.5服务。
除了主工作负载之外,此way服务的WebMethods还连接到Salesforce以检索和写入数据,因此我使用一个静态变量来保持Salesforce api登录对象在请求之间处于活动状态,这样我只登录一次(在Salesforce上),然后在后续调用中重用此变量,从而避免了每次请求时都需要登录。
此静态对象在Webservice的主ctor上求值,以检查login对象中的某些属性是否有效(例如,会话的有效性),如果不是,则再次调用login方法。
这在我的开发环境(Windows7和VS2012 IIS7.5?在开发服务器)和测试服务器(Win Server2003,IIS6),但它不能工作在生产箱,这也是一个Windows Server2003与IIS6,因为静态变量的值是空的每一个请求,登录到Salesforce的每个请求,给每个调用的长响应时间,也达到了Salesforce的一些限制,锁定帐户和阻止任何后续登录。
遗憾的是,这两台机器(测试和生产)都没有相同的配置,生产机器目前对我来说是无法到达的,所以目前我还不知道App Pool的回收时间和其他细节。
我认为这是一个配置问题,但不管怎样,这是我的代码,首先我只在Webservice主类中有一个静态变量,然后(当前版本)使用静态变量创建一个完整的静态类。
下面的所有代码都在同一个命名空间下
静态类(Salesforce登录逻辑):
public static class Srv
{
public static SFHelpers helper = new SFHelpers(); // own class, Holds Salesforce logic and data related to this requirement
public static SforceService SFserv = new SforceService(); // Salesforce class that handles login (and other Salesforce data manipulation methods)
public static DateTime NextLoginSF = DateTime.MinValue; // Determines when does the Salesforce session expires
public static void LoginSalesforce()
{ // Simplified salesforce login steps, removed try-catch and other conditions to facilitate comprehension
SFserv.Url = helper.URLSalesforce;
LoginResult loginResult = SFserv.login(SFuser, SFpass);
NextLoginSF = DateTime.Now.AddSeconds(loginResult.userInfo.sessionSecondsValid);
SFserv.Url = loginResult.serverUrl;
SFserv.SessionHeaderValue = new SessionHeader { sessionId = loginResult.sessionId };
}
}Main ctor和示例Webmethod:
[WebService(Namespace = "http://helloSO.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WsSFTest : System.Web.Services.WebService
{
public WsSFTest()
{
try
{
if (Srv.SFserv.SessionHeaderValue == null || DateTime.Now >= Srv.NextLoginSF) // any of this will trigger a login to renew Salesforce session
{
Srv.LoginSalesforce();
}
}
catch (SoapException se)
{
if (se.Code.Name == ExceptionCode.INVALID_SESSION_ID.ToString())
{ // Login again because Salesforce invalidated my session somehow
Srv.LoginSalesforce();
}
else
{
// Irrelevant
}
}
catch (Exception e)
{
// Irrelevant
}
}
[WebMethod]
public SampleResult SampleMethod(int param)
{
try
{
//irrelevant code gathers values here
var something = Srv.helper.Method(param, anotherParam);
return something;
}
catch (Exception e)
{
// Irrelevant
}
}
}将登录对象序列化到本地文件或持久性数据库是我最后的选择,因为快速响应时间是必须的。
我认为我要尝试的是会话变量,但是考虑到这种行为,类似的事情发生的可能性似乎很高。
有什么线索吗?提前感谢
发布于 2016-03-24 21:08:05
最后,我意识到了这一行的一些评估价值(特别是Srv.NextLoginSF)
if (Srv.SFserv.SessionHeaderValue == null || DateTime.Now >= Srv.NextLoginSF) // any of this will trigger a login to renew Salesforce session返回的值与预期的值不同,并且静态变量没有丢失它的值
https://stackoverflow.com/questions/36088858
复制相似问题