Partager via


Agent Mode Programming Sample

For the latest version of Commerce Server 2007 Help, see the Microsoft Web site.

Agent mode code calls the Web service behind the scenes. The actual code that does data manipulation is executed in the ASP.NET worker process under the identity of IIS thread pool. Each caller does not have to have access to the database, which highlights the advantage of this approach, which is being more secure and easier to configure. The disadvantage of this approach is significant overhead of passing data through the Web service.

The difference between Agent mode and Local mode is limited to how an instance of the context class for the corresponding Commerce Server system is created. After the context object is created, subsequent programming is the same, regardless of whether you are using Agent mode or Local mode.

To build and run this sample

  1. In Visual Studio 2005 or Visual Studio 2008, create a new C# console application.

  2. Paste the following code into your application and compile.

  3. Run the compiled console application. The call to the ReadLine method at the end of the sample will pause the application for you to observe the results.

Example

The following code creates instances of the MarketingServiceAgent and MarketingContext classes, creates an industry code using the industry code manager, creates a new customer, assigns the industry code to the customer, and then creates a new campaign associated with the customer ID.

This sample shows, in bold, the explicit construction of the MarketingServiceAgent object and then the version of the Create factory method that takes the MarketingServiceAgent object as its sole parameter.

Note

You can also use the version of the Create factory method that takes a string containing the URL of the Marketing Web service as its sole parameter, although you would not be able to set any of the properties of the MarketingServiceAgent object prior to calling the Create factory method.

using System;
using Microsoft.CommerceServer.Marketing;

namespace ConsoleApplication1
{
    // This code requires you to add the following references:
    // Microsoft.CommerceServer.CrossTierTypes
    // Microsoft.CommerceServer.Marketing.CrossTierTypes

    // Ensure that your user account has sufficient Marketing permissions to perform
    // these operations.
    
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // Set the URL to the Web service.
                string campaignServiceUrl = @"https://localhost/MarketingWebService/MarketingWebService.asmx";
                // Create an instance of the Marketing System agent.
                MarketingServiceAgent ma = new MarketingServiceAgent(campaignServiceUrl);
                MarketingContext marketingSystem = MarketingContext.Create(ma);

                // Create a new industry code because they are required for campaigns.
                IndustryCodeManager im = marketingSystem.IndustryCodes;
                IndustryCode ic = im.NewIndustryCode();
                ic.Name = "Travel";
               
                // Create a new customer to use for the campaign.
                Customer cu = marketingSystem.Customers.NewCustomer();

                // Set the minimum required properties.
                cu.Name = "Example Customer";
                cu.Industry = ic.Name;

                // Save the customer.
                cu.Save(true);
                              
                // Create the campaign.
                Campaign ca = marketingSystem.Campaigns.NewCampaign(cu.Id);
                
                // Set the minimum required campaign properties.
                ca.Name = "Example Campaign";
                DateTime startdate=DateTime.Now;
                DateTime enddate=startdate.AddDays(30);
                ca.StartDate = startdate;
                ca.EndDate = enddate;
                ca.EventTypeName = "Click";

                // Save the campaign.
                ca.Save(true);

                // Success!
                Console.WriteLine("Successfully created campaign name: {0}", ca.Name);
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: {0}\r\nMessage: {1}", ex.GetType(), ex.Message);
                if (ex.InnerException != null)
                {
                    Console.WriteLine("\r\nInner Exception: {0}\r\nMessage: {1}", ex.InnerException.GetType(), ex.InnerException.Message);
                }
                Console.ReadLine();
            }
        }
    }
}

See Also

Other Resources

Agent Mode Programming with the Management APIs

Local Mode Programming Sample

Web Service Programming Sample

Understanding the Different Types of Commerce Server APIs