EN
Dette innholdet er ikke tilgjengelig på ditt språk, men her er den engelske versjonen.
0 av 3 vurderte dette som nyttig - Vurder dette emnet

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

Hosting the service is the final step in creating a Windows Azure 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 you must do before hosting the application, see Building a Service for the Service Bus. The next step is to put all these separate 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 the Service Bus.

The first procedure describes how to create a service that uses the 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 Windows 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.

To host a Service Bus service programmatically

  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 the 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(sharedSecretServiceBusCredential);
    

    In this code sample, sharedSecretServiceBusCredential 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 the 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();
    

To host a Service Bus service that uses an App.config file

  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 issuerName = "IssuerName";
            string issuerSecret = "IssuerSecret";

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

            // Create a Behavior for the Credentials
            TransportClientEndpointBehavior sharedSecretServiceBusCredential = new TransportClientEndpointBehavior();
            sharedSecretServiceBusCredential.CredentialType = TransportClientCredentialType.SharedSecret;
            sharedSecretServiceBusCredential.Credentials.SharedSecret.IssuerName = issuerName;
            sharedSecretServiceBusCredential.Credentials.SharedSecret.IssuerSecret = issuerSecret;

            // 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="sharedSecretClientCredentials">
          <transportClientEndpointBehavior credentialType="SharedSecret">
            <clientCredentials>
              <sharedSecret issuerName="ISSUER_NAME" issuerSecret="ISSUER_SECRET" />
            </clientCredentials>
          </transportClientEndpointBehavior>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <!-- Application Binding -->
      <netOnewayRelayBinding>
        <binding name="default" />
      </netOnewayRelayBinding>
    </bindings>
    <services>
      <service name="Microsoft.ServiceBus.Samples.OnewayService">
        <endpoint address="" behaviorConfiguration="sharedSecretClientCredentials"
          binding="netOnewayRelayBinding" bindingConfiguration="default"
          name="RelayEndpoint" contract="Microsoft.ServiceBus.Samples.IOnewayContract" />
      </service>
    </services>
   
  </system.serviceModel>
</configuration>

See Also

Vurderte du dette som nyttig?
(1500 tegn igjen)

Fellesskapsinnhold

Legg til
© 2013 Microsoft. Med enerett.
facebook page visit twitter rss feed newsletter