Eksporter (0) Skriv ut
Vis alt
EN
Dette innholdet er ikke tilgjengelig på ditt språk, men her er den engelske versjonen.

Step 2: Define a WCF Service Contract to use with Service Bus

Updated: June 18, 2014

This is the second of seven tasks required to create a basic Windows Communication Foundation (WCF) service and a client that can call the service that uses Microsoft Azure Service Bus. For an overview of all seven of the tasks, see the Service Bus Relayed Messaging Tutorial. The previous step is Step 1: Sign up for an Account; the subsequent step is Step 3: Implement the WCF Contract to use Service Bus.

When creating a basic WCF service (whether for the Service Bus or not), you must define the service contract, which specifies what operations (the Web service terminology for methods or functions) the service supports. Contracts are created by defining a C++, C#, or Visual Basic interface. This tutorial demonstrates how to create such a contract using C#. Each method in the interface corresponds to a specific service operation. Each interface must have the ServiceContractAttribute attribute applied to it, and each operation must have the OperationContractAttribute applied to it. If a method in an interface that has the ServiceContractAttribute attribute does not have the OperationContractAttribute attribute, that method is not exposed. The code for these tasks is provided in the example following the procedure. For more information about how to define a contract, see Designing a WCF Contract for the Service Bus. For a larger discussion of contracts and services, see Designing and Implementing Services in the WCF documentation.

Expected time to completion: 10 minutes.

To create a Service Bus contract with an interface

  1. Open Visual Studio 2012 as an administrator by right-clicking the program in the Start menu and selecting Run as administrator.

  2. Create a new console application project. Click the File menu and select New, then click Project. In the New Project dialog, click Visual C# (if Visual C# does not appear, look under Other Languages), click the Console Application template, and name it EchoService. Use the default Location. Click OK to create the project.

  3. Note that the following two steps (4 and 5) are not necessary if you are running Visual Studio 2008 or later.

  4. In Solution Explorer, right-click the name of your project (in this example, EchoService), and click Properties.

  5. Click the Application tab on the left, then select .NET Framework 4 from the Target framework: dropdown. Click Yes when prompted to reload the project.

  6. For a C# project, Visual Studio creates a file that is named Program.cs. This class will contain an empty method called Main(). This method is required for a console application project to build correctly. Therefore, you can safely leave it in the project.

  7. Add a reference to System.ServiceModel.dll to the project:

    1. In the Solution Explorer, right-click the References folder under the project folder and then click Add Reference….

    2. Select the .NET tab in the Add Reference dialog and scroll down until you see System.ServiceModel, select it, and then click OK.

    noteNote
    When using a command-line compiler (for example, Csc.exe), you must also provide the path to the assemblies. By default, for example on a computer that is running Windows 7, the path is: Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation.

  8. In the Solution Explorer, double-click the Program.cs file to open it in the editor.

  9. Add a using statement for the System.ServiceModel namespace.

    using System.ServiceModel;
    

    System.ServiceModel is the namespace that lets you programmatically access the basic features of WCF. Service Bus uses many of the objects and attributes of WCF to define service contracts. You will most likely use this namespace in most of your Service Bus applications.

  10. Change the namespace name from its default name of EchoService to Microsoft.ServiceBus.Samples.

    ImportantImportant
    This tutorial uses the C# namespace Microsoft.ServiceBus.Samples, which is the namespace of the contract managed type that is used in the configuration file in Step 6: Configure the WCF Client. You can specify any namespace you want when you build this sample; however, the tutorial will not work unless you then modify the namespaces of the contract and service accordingly, in the application configuration file. The namespace specified in the App.config file must be the same as the namespace specified in your C# files.

  11. Directly after the Microsoft.ServiceBus.Samples namespace declaration, but within the namespace, define a new interface named IEchoContract and apply the ServiceContractAttribute attribute to the interface with a namespace value of http://samples.microsoft.com/ServiceModel/Relay/. The namespace value differs from the namespace that you use throughout the scope of your code. Instead, the namespace value is used as a unique identifier for this contract. Specifying the namespace explicitly prevents the default namespace value from being added to the contract name.

    [ServiceContract(Name = "IEchoContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
    public interface IEchoContract
    {
    }
    
    
    noteNote
    Typically, the service contract namespace contains a naming scheme that includes version information. Including version information in the service contract namespace enables services to isolate major changes by defining a new service contract with a new namespace and exposing it on a new endpoint. In in this manner, clients can continue to use the old service contract without having to be updated. Version information can consist of a date or a build number. For more information, see Service Versioning. For the purposes of this tutorial, the naming scheme of the service contract namespace does not contain version information.

  12. Within the IEchoContract interface, declare a method for the single operation the IEchoContract contract exposes in the interface and apply the OperationContractAttribute attribute to the method that you want to expose as part of the public Service Bus contract.

    [OperationContract]
    string Echo(string text);
    
  13. Outside the contract, declare a channel that inherits from both IEchoChannel and also to the IClientChannel interface, as shown here:

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

    A channel is the WCF object through which the host and client pass information to each other. Later, you will write code against the channel to echo information between the two applications.

  14. From the Build menu, click Build Solution or press F6 to confirm the accuracy of your work.

Example

The following code example shows a basic interface that defines a Service Bus contract.

using System;
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)
        {
        }
    }
}

Now that the interface is created, you can implement the interface, as described in Step 3: Implement the WCF Contract to use Service Bus.

Fellesskapsinnhold

Legg til
Vis:
© 2014 Microsoft