Bu sayfa faydalı mıydı?
Bu içerik hakkındaki geri bildiriminiz önemli. Ne düşündüğünüzü bilmemize izin verin.
İlave geri bildirim?
1500 karakter kaldı
MSDN Library
EN
Bu içerik dilinizde bulunmamaktadır ancak İngilizce sürümüne buradan bakabilirsiniz.

How to: Host a WCF Service that Uses the Service Bus Service

Updated: August 26, 2015

Hosting the service is the final step in creating a Service Bus application. Before reaching this point, you will have defined and implemented the service contract, defined and configured the service endpoint, and created the security credentials. For more information about what to do before hosting the application, see Building a Service for Service Bus. The next step is to put all the parts together and get them running. This process is accomplished through the service host, which takes the URL of your project, together with the contract, and creates a connection to Service Bus.

The first procedure describes how to create a service that uses Service Bus, with the configuration settings defined programmatically. The second procedure shows how to create a service when most of the configuration is specified in the App.config file. This procedure follows the NetOneWay sample in the Azure SDK. For a hybrid approach that uses both programmatic configuration and also an App.config file, see steps 1 through 4 of the Service Bus Relayed Messaging Tutorial.

For a complete discussion of hosting an application, see Hosting Services in the Windows Communication Foundation (WCF) documentation.

  1. Create an address for your service.

    The address for your Service Bus project is used in both service and client applications. For a service, the URI is used to determine where Service Bus exposes the service. For a client, the URI determines where the client looks for the service:

    string servicePath = "ServicePath";
    string serviceNamespace = "ServiceNamespace";
    Uri uri = ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespace, servicePath);
    
    
  2. Create a new instance of ServiceHost.

    host = new ServiceHost(typeof(EchoService), uri);
    

    In this example, the service host takes the supplied address, in addition to the type that the service contract implements. In this example, the class that implements the service contract is named EchoService.

  3. Create a description of the contract with a call to ContractDescription.

    ContractDescription contractDescription = ContractDescription.GetContract(typeof(IEchoContract), typeof(EchoService));
    

    ContractDescription links the contract with the specific implementation you want to use. In this example, the contract is defined in IEchoContract, and the implementation of the contract is EchoService.

  4. Define the address and binding for the endpoint:

    ServiceEndpoint serviceEndPoint = new ServiceEndpoint(contractDescription);
    serviceEndPoint.Address = new EndpointAddress(uri);
    serviceEndPoint.Binding = new NetTcpRelayBinding();
    
  5. Add any additional behaviors to the endpoint, such as security or publishing behaviors:

    serviceEndPoint.Behaviors.Add(sharedAccessSignatureServiceBusCredential);
    

    In this code sample, sharedAccessSignatureServiceBusCredential had previously been created to store the security credentials.

  6. Add the service endpoint to the service host instance. This step indicates which endpoint you want to instantiate.

    host.Description.Endpoints.Add(serviceEndPoint);
    
  7. Open the service by using a call to ServiceHost.Open.

    If successful, the service will be available for a client application to contact and communicate with through Service Bus without additional action required. However, you may want to perform additional tasks, such as notifying the user that the host has succeeded.

    host.Open();
    
    Console.WriteLine(String.Format("Listening at: {0}", endPoint));
    Console.WriteLine("Press [Enter] to exit");
    Console.ReadLine();
    
  8. When you are finished, close the host with ServiceHost.Close.

    host.Close();
    

  1. Create the name of the project to expose on the Service Bus:

    string serviceBusProjectName = "myProjectNameHere";
    

  2. Create the URI for your service:

    Uri address = ServiceBusEnvironment.CreateServiceUri("sb", serviceBusProjectName, "OnewayService");
    
  3. Create a new instance of the ServiceHost.

    ServiceHost host = new ServiceHost(typeof(LogService), uri);
    

    Here, the service host takes the supplied address, and also the type that the service contract implements. In this example, the class that implements the service contract is named LogService.

  4. If successful, the service will be available for a client application to contact and communicate with through the Service Bus without additional action required.

    host.Open();
    
    Console.WriteLine("Press [Enter] to exit");
    Console.ReadLine();
    
  5. When you are finished, close the host with ServiceHost.Close.

    host.Close();
    
  6. In your App.config file, add the credential and binding information for your project.

    An example App.config file that contains this information is located in the code sample at the end of this topic.

Example

The following example shows how to programmatically define and create a service application.

    class Program
    {
        static void Main(string[] args)
        {
            string servicePath = "ServicePath";
            string serviceNamespace = "ServiceNamespace";
            string keyName = "RootManageSharedAccessKey";
            string key = "YOUR_KEY";

            // Construct a Service Bus URI
            Uri uri = ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespace, servicePath);

            // Create a Behavior for the Credentials
            TransportClientEndpointBehavior sharedAccessSignatureServiceBusCredential = new TransportClientEndpointBehavior();
            
            sharedAccessSignatureSecretServiceBusCredential.TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(keyName, key);

            // Create the Service Host 
            host = new ServiceHost(typeof(EchoService), uri);
            ContractDescription contractDescription = ContractDescription.GetContract(typeof(IEchoContract), typeof(EchoService));
            ServiceEndpoint serviceEndPoint = new ServiceEndpoint(contractDescription);
            serviceEndPoint.Address = new EndpointAddress(uri);
            serviceEndPoint.Binding = new NetTcpRelayBinding();
            serviceEndPoint.Behaviors.Add(sharedSecretServiceBusCredential);
            host.Description.Endpoints.Add(serviceEndPoint);

            host.Open();

            Console.WriteLine(String.Format("Listening at: {0}", endPoint));
            Console.WriteLine("Press [Enter] to exit");
            Console.ReadLine();

            host.Close();
        }
    }




//Service that is configured mainly with an App.config file
   class Program
    {
        static void Main(string[] args)
        {
            string serviceNamespace = GetServiceNamespace();
            Uri address = ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespace, "OnewayService");

            ServiceHost host = new ServiceHost(typeof(OnewayService), address);
            host.Open();

            Console.WriteLine("Press [Enter] to exit");
            Console.ReadLine();

            host.Close();
        }
    }


//App.config file associated with the previous code sample

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="sharedAccessSignatureClientCredentials">
          
          <transportClientEndpointBehavior>
            <tokenProvider>
              <sharedAccessSignature keyName=”RootManageSharedAccessKey” key="**key**" />
            </tokenProvider>
          </transportClientEndpointBehavior>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <!-- Application Binding -->
      <netOnewayRelayBinding>
        <binding name="default" />
      </netOnewayRelayBinding>
    </bindings>
    <services>
      <service name="Microsoft.ServiceBus.Samples.OnewayService">
        <endpoint address="" behaviorConfiguration="sharedAccessSignatureClientCredentials"
          binding="netOnewayRelayBinding" bindingConfiguration="default"
          name="RelayEndpoint" contract="Microsoft.ServiceBus.Samples.IOnewayContract" />
      </service>
    </services>
   
  </system.serviceModel>
</configuration>

See Also

Show:
© 2015 Microsoft