Simple.OData.Client真的很慢

时间:2018-06-26 12:41:07

标签: c# performance odata odata-v4

我正在使用Simple.OData.Client在我们的crm动态系统中进行查询和更新。 但是每次查询,插入或更新最多需要10秒钟。它就像邮递员的魅力。这意味着服务器不是问题。

这是我的代码:

基类

 public abstract class CrmBaseDao<T> where T : class
{
    protected ODataClient GetClient()
    {
        return new ODataClient(new ODataClientSettings(BuildServiceUrl())
        {
            Credentials = new NetworkCredential(Settings.Default.CrmUsername, Settings.Default.CrmPassword),
            IgnoreUnmappedProperties = true
        });
    }

    public async Task<IEnumerable<T>> GetAll()
    {
        var client = GetClient();
        return await client.For<T>().FindEntriesAsync();
    }

    private string BuildServiceUrl()
    {
        return Settings.Default.CrmBaseUrl + "/api/data/v8.2/";
    }
}

派生类:

    public void Insert(Account entity)
    {
        var task = GetClient()
            .For<Account>()
            .Set(ConvertToAnonymousType(entity))
            .InsertEntryAsync();

        task.Wait();

        entity.accountid = task.Result.accountid;
    }

    public void Update(Account entity)
    {
        var task = GetClient()
             .For<Account>()
             .Key(entity.accountid)
             .Set(ConvertToAnonymousType(entity))
             .UpdateEntryAsync();

        task.Wait();
    }

    private object ConvertToAnonymousType(Account entity)
    {
        return new
        {
            entity.address1_city,
            entity.address1_fax,
            entity.address1_line1,
            entity.address1_postalcode,
            entity.address1_stateorprovince,
            entity.he_accountnumber,
            entity.name,
            entity.telephone1,
            entity.telephone2
        };
    }

    public async Task<Account> GetByHeAccountNumber(string accountNumber)
    {
        return await GetClient().For<Account>()
            .Filter(x => x.he_accountnumber == accountNumber)
            .FindEntryAsync();
    }

通话:

 private void InsertIDocsToCrm()
    {
        foreach (var file in GetAllXmlFiles(Settings.Default.IDocPath))
        {
            var sapAccountEntity = GetSapAccountEntity(file);
            var crmAccountEntity = AccountConverter.Convert(sapAccountEntity);

            var existingAccount = crmAccountDao.GetByHeAccountNumber(crmAccountEntity.he_accountnumber);
            existingAccount.Wait();

            if (existingAccount.Result != null)
            {
                crmAccountEntity.accountid = existingAccount.Result.accountid;
                crmAccountDao.Update(crmAccountEntity);
            }
            else
                crmAccountDao.Insert(crmAccountEntity);
        }
    }

这整个功能需要很长时间(30秒以上) 有没有机会加快速度?

还需要很多内存吗?!

谢谢

1 个答案:

答案 0 :(得分:0)

我怀疑这可能与架构大小有关。我将关注您在GitHub上打开的问题。

更新。我运行了一些基准测试来模拟服务器响应,并且在Simple.OData.Client内部进行处理仅花费了几毫秒。我建议您在服务器端运行基准测试。您也可以尝试将元数据文件引用分配给ODataClientSettings的MetadataDocument属性。