Exportera (0) Skriv ut
Visa allt
EN
Det här innehållet finns inte tillgängligt på ditt språk men här finns den engelska versionen,

How to: Create a WCF SOAP Client Application for the Service Bus

Updated: March 12, 2015

The following topic describes how to create a traditional client application that accesses Microsoft Azure Service Bus. For more information about building a client application, see Building Clients in the Windows Communication Foundation (WCF) documentation. The following procedure is a simplified process for creating a client application that highlights the features unique to Service Bus. For a complete sample, see the Relayed Messaging Service Bus sample, or Step 5: Create a WCF Client for the Service Contract in the Service Bus Relayed Messaging Tutorial.

  1. Retrieve a copy of the contract for the service and include it in your code:

    using System;
    using System.ServiceModel;
    
    [ServiceContract(Name = "IEchoContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
    public interface IEchoContract
    {
        [OperationContract]
        string Echo(string text);
    }
    
    public interface IEchoChannel : IEchoContract, IClientChannel { }
    
    

    You can retrieve the contract from the service in a variety of ways, such as through metadata exposed by the service. For more information, see How to: Design a WCF Service Contract for use with Service Bus.

  2. Add references to the System.ServiceModel and Microsoft.ServiceBus namespaces to your project:

    using System.ServiceModel;
    using Microsoft.ServiceBus;
    
    
  3. Retrieve your service namespace and relevant credential information:

    static void Main(string[] args)
    {
        Console.Write("Your Service Namespace (ex. sb://<ServiceNamespace>.servicebus.windows.net/): ");
        string serviceNamespace = Console.ReadLine();
        Console.Write("Your SAS Policy Name: ");
        string sasPolicyName = Console.ReadLine();
        Console.Write("Your SAS Key: ");
        string sasKey = Console.ReadLine();
    }
    
    
    noteNote
    The previous code example assumes that the service endpoint requires SAS key credentials. Service endpoints may not require authentication; if one did not require authentication, setting the SAS policy name and key would also not be necessary.

    The type of security and authentication that is required to connect is defined by the service. You can retrieve this information in a variety of ways, such as through metadata that the service exposes. For more information, see How to: Design a WCF Service Contract for use with Service Bus. This topic assumes that the Service Bus endpoint requires client applications to authenticate and uses SAS credentials. (An endpoint may not require any authentication, although this example does.) You can also use other credential types, such as a simple Web token (SWT), or SAML. At this point, you can also set the client transport or message-level security. However, for many scenarios, the default settings are sufficient. For more information, see Securing and Authenticating a Service Bus Connection.

  4. Define the security credentials to use with the endpoint:

    TransportClientEndpointBehavior clientBehavior = new TransportClientEndpointBehavior();
    clientBehavior.TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(sasPolicyName, sasKey);
    
    
    
    noteNote
    The previous example assumes that the service endpoint requires SAS credentials. Service endpoints may not require authentication; if authentication is not required, skip this step and step 8, later in this section.

  5. Create a URI object pointing to the Service Bus service, as shown in the following code:

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

    In this code example, the CreateServiceUri method takes the schema (“sb” for Service Bus, used for TCP relay connections), the service namespace, and the name of the endpoint to which to connect. For more information, see Creating a Service Bus URI.

  6. Configure the client endpoint used to connect to the service.

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <client>
          <!-- Application Endpoint -->
          <endpoint name="RelayEndpoint"
                    contract="Microsoft.ServiceBus.Samples.IEchoContract"
                    binding="netTcpRelayBinding"/>
        </client>
    
      </system.serviceModel>
    </configuration>
    

    In this example, the client endpoint is defined using the name “RelayEndpoint”, which is used later to help create the channel factory. The endpoint configuration also declares the contract defined in the first step of this procedure, as well as the fact that the binding used to connect is a NetTcpRelayBinding. For this procedure, the information is declared in an App.config file. You can also define this information programmatically. For more information, see Configuring a WCF Service to Register with Service Bus.

  7. Instantiate a channel factory:

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

    This channel factory uses the type of channel defined in the client contract at the beginning of this procedure.

  8. Apply the credentials and any other behaviors to the endpoint:

    channelFactory.Endpoint.Behaviors.Add(clientBehavior);
    
    noteNote
    The preceding code example assumes that the service endpoint requires SAS credentials. Service endpoints may or may not require authentication; if an endpoint does not require authentication, adding the credential behavior to the client endpoint would not be required.

  9. Create a new channel to the service and open it:

    IEchoChannel channel = channelFactory.CreateChannel();
    channel.Open();
    
  10. Perform whatever tasks are necessary for your scenario:

    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();
    }
    

    This step consists of accessing the service application through the exposed endpoint. For more information, see Building a Service for Service Bus.

  11. When you are finished, close the channel, as shown in the following code:

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

See Also

Visa:
© 2015 Microsoft