0

Dynamics CRM 2011 の Silverlight 5 アプリケーションで、CRM の組織サービスにアクセスして、エンティティ メタデータを照会します。エンティティ名を受け取り、そのすべてのフィールドのリストを返すサービスを作成しました。

このサービス メソッドを自動的にテストするにはどうすればよいですか? 主な問題は、CRM のコンテキストで実行されない Silverlight アプリから組織サービスへの参照を取得する方法です。

私の Service メソッドは次のようになります。

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 に答える