之前写过一个博客,对非声明验证方式下连接组织服务的两种方式的性能进行了对比,但当时只是对比了实例化组织服务的时间,并没有对查询数据的时间进行对比,那有朋友也在我的博客中留言了反映了查询的时间问题,一直没时间做具体的测试,值此web
api出现的时机,一并测下
下方是我的测试demo,分别列出了获取组织服务需要的时间及查询所花时间,demo中是以查询一条用户记录的所有属性为例
三种分别是1、CrmConnection
2、普通的方式(姑且这么叫吧,因为这是最常用的)
3、web api
static void Main(string[] args)
{
string result = "";
#region CrmConnection效率
DateTime dt1 = DateTime.Now;
OrganizationService org = new OrganizationService(new CrmConnection("crm"));
DateTime dt8 = DateTime.Now;
result = result + "CrmConnection实例服务时间 " + (dt8 - dt1).Milliseconds.ToString() + "\r\n";
Entity test = org.Retrieve("systemuser", new Guid("B3EB9804-6CD8-E511-9413-D04319595BED"), new Microsoft.Xrm.Sdk.Query.ColumnSet(true));
DateTime dt2 = DateTime.Now;
result = result + "CrmConnection查询时间 " + (dt2 - dt8).Milliseconds.ToString() + "\r\n";
#endregion #region 普通方式获取OrganizationService
DateTime dt3 = DateTime.Now;
ClientCredentials clientCredentials = new ClientCredentials();
clientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential("administrator", "001", "skysoft");
IOrganizationService organizationServiceProxy = new OrganizationServiceProxy(new Uri("http://crmhost/XRMServices/2011/Organization.svc"), null, clientCredentials, null);
DateTime dt4 = DateTime.Now;
result = result + "普通方式获取组织服务时间 " + (dt4 - dt3).Milliseconds.ToString() + "\r\n";
Entity test1 = organizationServiceProxy.Retrieve("systemuser", new Guid("B3EB9804-6CD8-E511-9413-D04319595BED"), new Microsoft.Xrm.Sdk.Query.ColumnSet(true));
DateTime dt5 = DateTime.Now;
result = result + "普通方式查询数据时间 " + (dt5 - dt4).Milliseconds.ToString() + "\r\n";
result = result + "普通方式查询数据总时间 " + (dt5 - dt3).Milliseconds.ToString() + "\r\n"; #endregion #region web api方式
DateTime dt6 = DateTime.Now;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://crmhost/api/data/v8.0/systemusers(B3EB9804-6CD8-E511-9413-D04319595BED)");
req.Credentials = new NetworkCredential("administrator", "", "skysoft");
req.Method = "Get";
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
{
StreamReader read = new StreamReader(res.GetResponseStream());
string result1 = read.ReadToEnd();
}
DateTime dt7 = DateTime.Now;
result = result + "web api查询时间 " + (dt7 - dt6).Milliseconds.ToString() + "\r\n"; #endregion
Console.WriteLine(result);
Console.ReadLine();
}
下图是测试结果,可以看出第1种方式获取组织服务确实比第二种快很多,但在查询具体数据时就会落后很多,所以综合下来半斤八两吧,没有明显的差距,但看第三种也就是web api,完虐前两种,孰优孰劣一目了然。