Compartilhar via


Creating the policy helper application

Open the file that contains the method named Main and complete the following steps:

  1. Add namespace references.

    To simplify the use of several classes and methods, add using statements for the following namespaces:

    • System.Configuration

    • Microsoft.Dynamics.Common

  2. Add data members.

    Add private data members to your class. To use service context information, add a Context object. Add a GUID for each policy and behavior. Also add a GUID for each parameter required by a behavior. Add a dictionary object that stores the collection of behaviors for the policies.

    The following code example shows the objects used to add policies for the sample Leads service. Notice how a GUID is used to uniquely identify the create, delete, and update policies. The create policy includes one behavior so a GUID is used to uniquely identify the behavior. Also, the create behavior includes one behavior option parameter that stores the configuration information the service administrator enters. A GUID is used to uniquely identify the behavior option parameter.

    private Context context;
    

private Guid deleteLeadPolicyId; private Guid createLeadPolicyId; private Guid updateLeadPolicyId; private Guid createQualifiedLeadBehaviorId; private int QualifiedLeadParameterId; private Dictionary<Guid, Behavior> behaviors; private static string tenant;

  1. Initialize the data members.

    To populate the private data members, add a private method to the class. Use a parameter to specify the company that requires the new policy. The company ID will be obtained from the command line parameters of the "policy helper" application. If your web service will be used in a multitenant environment, use an additional parameter to specify the tenant that is being accessed. This value will also be passed in as an optional command line argument.

    private void InitializeComponent(int cmpIdNumber)
    

{

}

Use the InitializeComponent method to instantiate a context object. // Instantiate a context object context = new Context();

// Specify the company to use // Set the ID using the company ID supplied by the command line parameter CompanyKey companyKey = new CompanyKey(); companyKey.Id = cmpIdNumber;

// Set up the context object context.OrganizationKey = (OrganizationKey)companyKey; context.CultureName = "en-US"; context.TenantName = tenant;

Create the keys that uniquely identify each policy. The following code sample shows the policies for the sample Leads service. The GUIDs were generated using the Create GUID utility accessed from the Visual Studio Tools menu. Notice how the braces were removed from each GUID.

**Warning:** The GUID value you assign to the key uniquely identifies each policy or behavior. When you work with policies and behaviors, you must use the GUID value to specify an individual policy or behavior.

<pre class="checklistscript" IsFakePre="true" xmlns="http://www.w3.org/1999/xhtml">createLeadPolicyId = new Guid("D3E303CC-CCB5-4945-9DD9-4DA409264518");

updateLeadPolicyId = new Guid("443E9AFD-50E0-44e2-A2F1-8FFFC2DCE596"); deleteLeadPolicyId = new Guid("3B7922CE-A7C7-41fc-879E-C749A246B5D1");

Create keys that uniquely identify each behavior. The following code sample creates a behavior key for the create policy of the sample Leads service.

<pre class="checklistscript" IsFakePre="true" xmlns="http://www.w3.org/1999/xhtml">createQualifiedLeadBehaviorId =
new Guid("10099A58-64BF-4fa3-B35F-ED43D1B6FF9C");
Specify a value that identifies each behavior option parameter. Parameter are typically numbered sequentially starting with "1". The following code example specifies the ID of the behavior option parameter of the create policy for the sample Leads service.

<pre class="checklistscript" IsFakePre="true" xmlns="http://www.w3.org/1999/xhtml">QualifiedLeadParameterId = 1;
Create a dictionary that will be used to store behaviors. The following code example uses a dictionary collection to store behaviors. The dictionary allows you to use the ID of a behavior to retrieve that behavior.

<pre class="checklistscript" IsFakePre="true" xmlns="http://www.w3.org/1999/xhtml">behaviors = new Dictionary&lt;Guid, Behavior&gt;();
  1. Implement command line parameters.

    In the Main method, add support for command line parameters. Use "/load" to add policy metadata. Use "/remove" to delete your policy metadata. Use an integer to specify the ID of the company where the policies need to be installed. Use an optional string parameter to specify the tenant if the web service is being used in a multitenant environment.

    The following code example shows one technique for obtaining command line information. Notice how the number of parameters are verified and how the first parameter is checked to be sure it contains "/load" or "/remove".

    static void Main(string[] args)
    

{ // Get the tenant name, if supplied. if (args.Length == 3) { tenant = args[2].ToString(); } else { tenant = ""; }

if ((args[0].ToLowerInvariant() == "/load") ||
        (args[0].ToLowerInvariant() == "/remove"))
{

}
else
{
    Console.WriteLine("InstallLeadPolicyMetadata.exe requires
        the first parameter to be: /load or /remove");
Console.WriteLine("Valid parameters are: /load or /remove");
        Console.WriteLine();
}
else
{
    Console.WriteLine("InstallLeadPolicyMetadata.exe has an
        incorrect number of parameters");
    Console.WriteLine("Valid parameters are: /load or /remove,
        a company ID, and an optional tenant name");
    Console.WriteLine();
}

Console.WriteLine("Press any key to quit.");
Console.ReadKey();
  1. Create an instance of the class.

    Add a statement to the Main method that instantiates the class.

    Program managePolicies = new Program();
    
  1. Get the company ID and initialize the class.

    Add statements to the Main method that retrieve the company ID from the command line parameters and initialize the class.

    The following code example shows how to retrieve the company ID from the command line parameters. Notice how the company ID is converted to an integer and used as an input parameter for the InitializeComponent method.

    int companyId = Int32.Parse(args[1]);
    
    

managePolicies.InitializeComponent(companyId);

  1. Specify the app.config information.

    The app.config file must contain caching configuration, application setting, and Tenant Discovery Service endpoint information. It can also contain diagnostic information that can help with troubleshooting the application. Add a reference to the WSInstallAppSettings.config file. This file reference provides access to configuration settings used by your helper application. You must also add a reference to the WSCachingConfiguration.config file. These two files are typically found in the following folder:

    c:\Program Files\Microsoft Dynamics\GPWebServices\ServiceConfigs

    The app.config file also contains entries that are used to locate the Tenant Discover Service if you are installing in a multitenant environment. It also contains entries for diagnostic logging that can be activated if you need to debug the application.

    The following example shows the configuration settings for the policy loader application. Note how the app.config references caching configuration information through the WSCachingConfiguration.config and how it also references the WSInstallAppSettings.config file. You can also see the Tenant Discovery Service endpoints that are referenced, as well as the diagnostic logging information.

    <?xml version="1.0"?>
    

<configuration> <configSections> <section name="cachingConfiguration" type="Microsoft.Dynamics.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Dynamics.EnterpriseLibrary.Caching, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </configSections> <cachingConfiguration configSource="ServiceConfigs\WSCachingConfiguration.config"/> <appSettings file="ServiceConfigs\WSInstallAppSettings.config"/> <system.serviceModel> <bindings configSource="ServiceConfigs\WSBindings.config"/> <client> <endpoint name="DiscoveryServiceHttp" binding="wsHttpBinding" bindingConfiguration="DiscoveryServiceBindingHttp" contract="Microsoft.Dynamics.MultitenantServices. ServicesInterface.ITenantDiscoveryService" address=""/> <endpoint name="DiscoveryServiceHttps" binding="wsHttpBinding" bindingConfiguration="DiscoveryServiceBindingHttps" contract="Microsoft.Dynamics.MultitenantServices. ServicesInterface.ITenantDiscoveryService" address=""/> </client> </system.serviceModel>

&lt;system.diagnostics&gt;
    &lt;switches&gt;
        &lt;add name="ApplicationTraceSwitch" value="0"/&gt;
    &lt;/switches&gt;
    &lt;trace autoflush="true" indentsize="4"&gt;
        &lt;listeners&gt;
            &lt;add name="dynamicsListener"/&gt;
        &lt;/listeners&gt;
    &lt;/trace&gt;
    &lt;sharedListeners&gt;
        &lt;add name="dynamicsListener"
            type="System.Diagnostics.TextWriterTraceListener"
            initializeData="C:\Program Files\Microsoft Dynamics\GPWebServices\Logs\Tracing\LeadPolicyMetadata.log"/&gt;
    &lt;/sharedListeners&gt;
&lt;/system.diagnostics&gt;

</configuration>