Genus Docs
Search Results for

    Show / Hide Table of Contents

    Consuming a Web Service from Microsoft Visual Studio - Code Example

    The code example below shows how a Web Service CustomerRelationsService and its operation GetAllCustomerContacts() can be called from Microsoft Visual Studio 2012 project using C#. Client proxy classes CustomerReleationsServiceClient, CustomerContacts, and CustomerContact were generated using Add Service Reference... as described here.

     1 ///
     2 /// The main program's entry point. This function is
     3 /// part of a Console Application project.
     4 ///
     5 ///The command-line arguments.
     6 publicstaticvoid Main(string[] args)
     7 {
     8     // Create an instance of the generated client proxy class.
     9     CustomerRelationsServiceClient client = newCustomerRelationsServiceClient();
     10 
     11     // Set credentials for the web service client. The use of 
     12     // "MyCredential.UserName" and "MyCredential.Password"
     13     // is to be replaced by your own user name and password.
     14 
     15     // Needed for "Basic" authentication.
     16     client.ClientCredentials.UserName.UserName = MyCredential.UserName;
     17     client.ClientCredentials.UserName.Password = MyCredential.Password;
     18 
     19     // Needed for "Windows" authentication.
     20     client.ClientCredentials.Windows.ClientCredential.UserName = MyCredential.UserName;
     21     client.ClientCredentials.Windows.ClientCredential.UserName = MyCredential.Password;
     22 
     23     // Optional part: Accept larger reponses than allowed by WCF defaults.
     24     if (client.Endpoint.Binding isBasicHttpBinding)
     25     {
     26         BasicHttpBinding endpointBinding = (BasicHttpBinding)client.Endpoint.Binding;
     27 
     28         // Adjust this size according to the expected size of the result.
     29         endpointBinding.MaxReceivedMessageSize *= 4;
     30         endpointBinding.MaxBufferSize = Math.Max(
     31             endpointBinding.MaxBufferSize, (int)endpointBinding.MaxReceivedMessageSize);
     32     }
     33 
     34     Console.WriteLine("Calling web service...\n");
     35 
     36     // Call the web service.
     37     CustomerContacts contacts = client.GetAllCustomerContacts();
     38 
     39     // Output the result ordered by the customer contact's last name.
     40     int count = 0;
     41     foreach (CustomerContact contact in contacts.OrderBy(contact => contact.LastName))
     42     {
     43         count++;
     44         Console.WriteLine(count + ". " +
     45             contact.LastName + ", " +
     46             contact.FirstName + ": " +
     47             (string.IsNullOrEmpty(contact.EMail) ?
     48                 "(No e-mail address)" : contact.EMail));
     49     }
     50 
     51     // Always close the client.
     52     client.Close();
     53 
     54     Console.WriteLine("\nHit a key to end the program...\n");
     55     Console.ReadKey(true);
     56 }
    • Improve this Doc
    In This Article
    Back to top © Copyright Genus AS, Norway. All rights reserved.