我在我们的测试环境中遇到了一个问题,该环境有两个用户限制,因为Web API用户会根据需要反复登录以同步数据。所以我想我会问是否有办法让Web API在完成后退出。这可能吗?
答案 0 :(得分:3)
什么版本的acumatica?
Acumatica 4.x及以下没有注销方法。我所做的是为我的Acumatica方法创建一个单例类,并添加一个共同的" Validate"方法,以便重新使用现有连接。
如下所示:
private void Validate(bool forceLogin = false, int trycount = 0)
{
if (trycount == 2)
{
//throw new AcumaticaException("Could not login to Acumatica Instance");
}
if (_container == null || forceLogin)
{
_container = new System.Net.CookieContainer();
if (forceLogin != true)
{
forceLogin = true;
}
}
_guruscreen = new Screen
{
CookieContainer = _container,
AllowAutoRedirect = true,
EnableDecompression = true,
Timeout = 1000000,
Url = AcumaticaUrl
};
if (forceLogin)
{
_guruscreen.Login("webservice", "WebServicePwd");
}
try
{
_guruscreen.UntypedClear("CW900000");
}
catch
{
// if session expired, login again and resend request
Validate(true, trycount++);
}
}
然后在调用导出/提交之前的每个方法中,我调用Validate()来查看是否存在现有连接,看看它是否仍处于活动状态,如果没有尝试重新连接。
如果您在Acumatica 5.x中,则有退出方法。
下面的样本
private const string Url = "http://localhost/WebServices_5-0/Soap/DemoService.asmx";
private const string UntypedUrl = "http://localhost/WebServices_5-0/Soap/.asmx";
private const string Login = "xxxxx";
private const string Password = "XXX";
public void ExportStockItems()
{
Screen context = new Screen();
context.CookieContainer = new System.Net.CookieContainer();
context.Url = Url;
context.Login(Login, Password);
IN202500Content stockItemsSchema = context.IN202500GetSchema();
var commands = new Command[]
{
stockItemsSchema.StockItemSummary.ServiceCommands.EveryInventoryID,
stockItemsSchema.StockItemSummary.InventoryID,
stockItemsSchema.StockItemSummary.Description,
stockItemsSchema.GeneralSettingsItemDefaults.ItemClass,
stockItemsSchema.GeneralSettingsUnitOfMeasureBaseUnit.BaseUnit,
stockItemsSchema.PackagingDimensions.Volume,
stockItemsSchema.PackagingDimensions.Weight
};
var filters = new Filter[]
{
new Filter
{
Field = new Field
{
ObjectName = stockItemsSchema.StockItemSummary.InventoryID.ObjectName,
FieldName = "LastModifiedDateTime"
},
Condition = FilterCondition.Less,
Value = DateTime.Now.ToLongDateString(),
Operator = FilterOperator.And
},
new Filter
{
Field = new Field
{
ObjectName = stockItemsSchema.StockItemSummary.ItemStatus.ObjectName,
FieldName = stockItemsSchema.StockItemSummary.ItemStatus.FieldName
},
Condition = FilterCondition.Equals,
Value = "Active",
}
};
var items = context.IN202500Export(commands, filters, 0, false, false);
UntypedDemoServiceRef.Screen untypedContext = new UntypedDemoServiceRef.Screen();
context.Url = UntypedUrl;
untypedContext.CookieContainer = context.CookieContainer;
untypedContext.Logout();
}