0

在 Dynamics CRM 2011 的 Silverlight 5 应用程序中,我访问 CRM 的组织服务以查询实体元数据。我编写了一个服务,它接受一个实体名称并返回其所有字段的列表。

如何自动测试此服务方法?主要问题是如何从不在 CRM 上下文中运行的 Silverlight 应用程序获取对组织服务的引用。

我的服务方法如下所示:

public IOrganizationService OrganizationService
    {
        get
        {
            if (_organizationService == null)
               _organizationService = SilverlightUtility.GetSoapService();
            return _organizationService;
        }
        set { _organizationService = value; }
    }


public async Task<List<string>> GetAttributeNamesOfEntity(string entityName)
    {
        // build request
        OrganizationRequest request = new OrganizationRequest
            {
                RequestName = "RetrieveEntity",
                Parameters = new ParameterCollection
                    {
                        new XrmSoap.KeyValuePair<string, object>()
                            {
                                Key = "EntityFilters",
                                Value = EntityFilters.Attributes
                            },
                        new XrmSoap.KeyValuePair<string, object>()
                            {
                                Key = "RetrieveAsIfPublished",
                                Value = true
                            },
                        new XrmSoap.KeyValuePair<string, object>()
                            {
                                Key = "LogicalName",
                                Value = "avobase_tradeorder"
                            },
                        new XrmSoap.KeyValuePair<string, object>()
                            {
                                Key = "MetadataId",
                                Value = new Guid("00000000-0000-0000-0000-000000000000")
                            }
                    }
            };

        // fire request
        IAsyncResult result = OrganizationService.BeginExecute(request, null, OrganizationService);

        // wait for response
        TaskFactory<OrganizationResponse> tf = new TaskFactory<OrganizationResponse>();
        OrganizationResponse response = await tf.FromAsync(
            OrganizationService.BeginExecute(request, null, null), iar => OrganizationService.EndExecute(result));

        // parse response
        EntityMetadata entities = (EntityMetadata)response["EntityMetadata"];
        return entities.Attributes.Select(attr => attr.LogicalName).ToList();
    }

编辑:我可以使用 Resharper 和 AgUnit 创建和执行单元测试。因此,问题通常不在于如何编写单元测试。

4

1 回答 1

1

我已经调整了标准 Microsoft SDK 中的 GetSoapService 以接受回退值。这意味着在 Visual Studio 中调试和在 CRM 中运行时无需更改代码。无论如何它是

public static IOrganizationService GetSoapService(string FallbackValue = null)
    {
        Uri serviceUrl = new Uri(GetServerBaseUrl(FallbackValue)+ "/XRMServices/2011/Organization.svc/web");


        BasicHttpBinding binding = new BasicHttpBinding(Uri.UriSchemeHttps == serviceUrl.Scheme
            ? BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.TransportCredentialOnly);
        binding.MaxReceivedMessageSize = int.MaxValue;

        binding.MaxBufferSize = int.MaxValue;

        binding.SendTimeout = TimeSpan.FromMinutes(20);

        IOrganizationService ser =new OrganizationServiceClient(binding, new EndpointAddress(serviceUrl));

        return ser;


    }

public static string GetServerBaseUrl(string FallbackValue = null)
    {


        try
        {
            string serverUrl = (string)GetContext().Invoke("getClientUrl");
            //Remove the trailing forwards slash returned by CRM Online
            //So that it is always consistent with CRM On Premises
            if (serverUrl.EndsWith("/"))
            {
                serverUrl = serverUrl.Substring(0, serverUrl.Length - 1);
            }

            return serverUrl;
        }
        catch
        {
            //Try the old getServerUrl
            try
            {
                string serverUrl = (string)GetContext().Invoke("getServerUrl");
                //Remove the trailing forwards slash returned by CRM Online
                //So that it is always consistent with CRM On Premises
                if (serverUrl.EndsWith("/"))
                {
                    serverUrl = serverUrl.Substring(0, serverUrl.Length - 1);
                }

                return serverUrl;
            }
            catch
            {
                return FallbackValue;
            }
        }
于 2013-10-22T07:02:57.357 回答