Was deze pagina nuttig?
Uw feedback over deze inhoud is belangrijk. Laat ons weten wat u denkt.
Aanvullende feedback?
1500 tekens resterend
Step 7: Implement WCF Client to Call the Service Bus
EN
Deze inhoud is niet beschikbaar in uw taal, maar wel in het Engels.

Step 7: Implement WCF Client to Call the Service Bus

Updated: July 1, 2015

This is the last of seven tasks required to create a basic Service Bus service and a client that can call the service. For an overview of all seven tasks, see Service Bus Relayed Messaging Tutorial.

This topic describes how to implement a basic client application that accesses the service you created previously in this tutorial. Similar to the service, the client performs many of the same operations to access Service Bus:

  1. Sets the connectivity mode.

  2. Creates the URI that locates the host service.

  3. Defines the security credentials.

  4. Applies the credentials to the connection.

  5. Opens the connection.

  6. Performs the application-specific tasks.

  7. Closes the connection.

However, one of the main differences is that the client application uses a channel to connect to Service Bus, whereas the service uses a call to ServiceHost. The code used for these tasks is provided in the example following the procedure.

Time to completion: 15 minutes

  1. Set the connectivity mode to AutoDetect.

    Add the following code inside the Main() method of the client application.

    ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.AutoDetect;
    
  2. Define variables to hold the values for the service namespace, and SAS key that are read from the console.

    
    Console.Write("Your Service Namespace: ");
    string serviceNamespace = Console.ReadLine();
    Console.Write("Your SAS Key: ");
    string sasKey = Console.ReadLine();
    
    
    
    
    
  3. Create the URI that defines the location of the host in your Service Bus project.

    Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespace, "EchoService");
    
    
  4. Create the credential object for your service namespace endpoint.

    TransportClientEndpointBehavior sasCredential = new TransportClientEndpointBehavior();
    sasCredential.TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", sasKey);
    
  5. Create the channel factory that loads the configuration described in the App.config file.

    ChannelFactory<IEchoChannel> channelFactory = new ChannelFactory<IEchoChannel>("RelayEndpoint", new EndpointAddress(serviceUri));
    

    A channel factory is a WCF object that creates a channel through which the service and client applications communicate.

  6. Apply the Service Bus credentials.

    channelFactory.Endpoint.Behaviors.Add(sasCredential);
    
  7. Create and open the channel to the service.

    IEchoChannel channel = channelFactory.CreateChannel();
    channel.Open();
    
  8. Write the basic user interface and functionality for the echo.

    Console.WriteLine("Enter text to echo (or [Enter] to exit):");
    string input = Console.ReadLine();
    while (input != String.Empty)
    {
        try
        {
            Console.WriteLine("Server echoed: {0}", channel.Echo(input));
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: " + e.Message);
        }
        input = Console.ReadLine();
    }
    

    Note that the code uses the instance of the channel object as a proxy for the service.

  9. Close the channel, and close the factory.

    channel.Close();
    channelFactory.Close();
    

  1. Press F6 to build the solution.

    This builds both the client project and the service project that you created in a previous step of this tutorial and creating an executable file for each.

  2. Before running the client application, make sure that the service application is running.

    You should now have an executable file for the Echo service application named EchoService.exe, located under your service project folder at \bin\Debug\EchoService.exe (for the debug configuration) or \bin\Release\EchoService.exe (for the release configuration). Double-click this file to start the service application.

  3. A console window opens and prompts you for the service namespace. In this console window, enter the service namespace and press Enter.

  4. Next, you are prompted for your SAS key. Enter the SAS key and press ENTER.

    Here is an example output from the console window. Note that the values provided here are for example purposes only.

    Your Service Namespace: myNamespace
    Your SAS Key: 1deCBMEhx/RV3bgwIhCohqdtzj/ZG2WnyC1cLhHTpk4=

    The service application starts and prints the address it is listening on to the console window as seen in the following example.

    Service address: sb://mynamespace.servicebus.windows.net/EchoService/
    Press [Enter] to exit
  5. Run the client application.

    You should now have an executable for the Echo client application named EchoClient.exe that is located under the client project directory at .\bin\Debug\EchoClient.exe (for the debug configuration) or .\bin\Release\EchoClient.exe (for the release configuration). Double-click this file to start the client application.

  6. A console window opens and prompts you for the same information that you entered previously for the service application. Follow the previous steps to enter the same values for the client application for the service namespace, issuer name, and issuer secret.

  7. After entering these values, the client opens a channel to the service and prompts you to enter some text as seen in the following console output example.

    Enter text to echo (or [Enter] to exit): 

    Enter some text to send to the service application and press ENTER.

    This text is sent to the service through the Echo service operation and appears in the service console window as in the following example output.

    Echoing: My sample text

    The client application receives the return value of the Echo operation, which is the original text, and prints it to its console window. The following is an example output from the client console window.

    Server echoed: My sample text
  8. You can continue sending text messages from the client to the service in this manner. When you are finished, press ENTER in the client and service console windows to end both applications.

Example

The following example shows how to create a client application, how to call the operations of the service, and how to close the client after the operation call is finished.

using System;
using Microsoft.ServiceBus;
using System.ServiceModel;

namespace Microsoft.ServiceBus.Samples
{
    [ServiceContract(Name = "IEchoContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
    public interface IEchoContract
    {
        [OperationContract]
        String Echo(string text);
    }

    public interface IEchoChannel : IEchoContract, IClientChannel { }

    class Program
    {
        static void Main(string[] args)
        {
            ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.AutoDetect;

            
            Console.Write("Your Service Namespace: ");
            string serviceNamespace = Console.ReadLine();
            Console.Write("Your SAS Key: ");
            string sasKey = Console.ReadLine();



            Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespace, "EchoService");

            TransportClientEndpointBehavior sasCredential = new TransportClientEndpointBehavior();
            sasCredential.TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", sasKey);

            ChannelFactory<IEchoChannel> channelFactory = new ChannelFactory<IEchoChannel>("RelayEndpoint", new EndpointAddress(serviceUri));

            channelFactory.Endpoint.Behaviors.Add(sasCredential);

            IEchoChannel channel = channelFactory.CreateChannel();
            channel.Open();

            Console.WriteLine("Enter text to echo (or [Enter] to exit):");
            string input = Console.ReadLine();
            while (input != String.Empty)
            {
                try
                {
                    Console.WriteLine("Server echoed: {0}", channel.Echo(input));
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error: " + e.Message);
                }
                input = Console.ReadLine();
            }

            channel.Close();
            channelFactory.Close();

        }
    }
}

Ensure that the service is running before you start the client. For more information, see Step 4: Host and Run a Basic Web Service to Register with Service Bus.

Security

Weergeven:
© 2015 Microsoft