Eksporter (0) Skriv ut
Vis alt
EN
Dette innholdet er ikke tilgjengelig på ditt språk, men her er den engelske versjonen.
6 av 10 vurderte dette som nyttig - Vurder dette emnet

Step 7: Implement WCF Client to Call the Service Bus

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 of the 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 the 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 the 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

To implement a client application

  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, issuer name, and issuer secret that are read from the console.

    
    Console.Write("Your Service Namespace: ");
    string serviceNamespace = Console.ReadLine();
    Console.Write("Your Issuer Name: ");
    string issuerName = Console.ReadLine();
    Console.Write("Your Issuer Secret: ");
    string issuerSecret = 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 sharedSecretServiceBusCredential = new TransportClientEndpointBehavior();
    sharedSecretServiceBusCredential.TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerSecret);
    
  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(sharedSecretServiceBusCredential);
    
  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();
    

To run the client application

  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 issuer name. Enter the issuer name and press ENTER.

  5. After entering your issuer name, enter the issuer secret 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 Issuer Name: owner
    Your Issuer Secret: 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
  6. 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.

  7. 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.

  8. 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
  9. 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 Issuer Name: ");
            string issuerName = Console.ReadLine();
            Console.Write("Your Issuer Secret: ");
            string issuerSecret = Console.ReadLine();

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

            TransportClientEndpointBehavior sharedSecretServiceBusCredential = new TransportClientEndpointBehavior();
            sharedSecretServiceBusCredential.TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerSecret);

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

            channelFactory.Endpoint.Behaviors.Add(sharedSecretServiceBusCredential);

            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

Vurderte du dette som nyttig?
(1500 tegn igjen)
Takk for tilbakemeldingen

Fellesskapsinnhold

Legg til
Vis:
© 2014 Microsoft. Med enerett.