Learning WCF Chapter1 Creating a New Service from Scratch

You’re about to be introduced to the WCF service.
This lab isn’t your typical “Hello World”—it’s “Hello Indigo”!
In this lab,you will learn how to build a new WCF service and in the process learn the minimum requirements of service development and consumption.
Here’s a short list of things you’ll accomplish:

• Create a new service contract and service implementation
• Programmatically configure a service host, its endpoints, and bindings
• Create a client application and open a client channel proxy to invoke the service

Now,before you start thinking “been there,done that,” this simple lab will be slightly different
because I’m going to give you some practical design tips that ensure configurability and appropriate decoupling of service,host,and client.
In addition,I’ll be diving deeper into basic concepts such as services,service contracts,endpoints, bindings, ServiceHost, and channels.

Lab: Creating Clients and Services Programmatically

In this first lab,you will create a new solution with three projects: a service,a host,and a client.
When you run the service host,you’ll expose a single service endpoint.
The client application will access service operations through that endpoint.
You’ll host the service in a console application and invoke the service using a manually constructed proxy.
This lab will teach you the basic requirements for creating,hosting,and consuming a service with WCF.

Creating a new service
The first thing you will do is create a new service contract with a single operation and implement this contract on a new service type.
1. In this lab,everything begins from scratch,so you’ll start by creating a new Visual Studio solution.
Open a new instance of Visual Studio 2008.
Select File ?New ? Project,and from the New Project dialog,create a new Blank Solution in the <YourLearningWCFPath>\Labs\Chapter1 directory.
Name the solution ServiceFromScratch. Verify that the .NET Framework version selected in the dropdown list is set to .NET Framework 3.0 or .NET Framework 3.5.
Click OK to create the empty solution.

2. Create the service project.
From Solution Explorer,right-click on the solution node and select Add ? New Project.
Select the Class Library template,name the project HelloIndigo,and make sure the location path matches the solution at <YourLearningWCFPath>\Labs\Chapter1\ServiceFromScratch.
Click OK to create the new project.


3. Now you will create your first service contract.
From Solution Explorer,rename the project’s only class file to Service.cs.
Open this file in the code window.
Add a new interface named IHelloIndigoService in Service.cs.
Add a single method to the interface, HelloIndigo, with the signature shown here:

public interface IHelloIndigoService
{
string HelloIndigo( );
}

4. Add a reference to the System.ServiceModel assembly. From Solution Explorer,right-click References and select System.ServiceModel from the list.
You’ll also need to add the following using statement to Service.cs:

using System.ServiceModel;

5. To turn this interface into a service contract,you’ll need to explicitly decorate the interface with the ServiceContractAttribute.
In addition,each method should be decorated with the OperationContractAttribute to include it in the service contract.
In this case,you’ll make IHelloIndigoService a service contract and expose HelloIndigo( ) as its only service operation by applying these attributes as shown here:

[ServiceContract(Namespace="http://www.thatindigogirl.com/samples/2006/06")]
public interface IHelloIndigoService
{
[OperationContract]
string HelloIndigo( );
}

Note:Providing a namespace for the ServiceContractAttribute reduces the possibility of naming collisions with other services. This will be discussed in greater detail in Chapter 2.

6. In the same file,create a service type to implement the service contract.
You can modify the existing class definition,renaming it to HelloIndigoService.
Then add the IHelloIndigoService interface to the derivation list and implement HelloIndigo( ) with the following code:

public class HelloIndigoService : IHelloIndigoService
{
public string HelloIndigo( )
{
return "Hello Indigo";
}
}

7. Compile the service project.

At this point,you’ve created a service contract with a single operation and implemented it on a service type.
The service is complete at this point,but to consume it from a client application, you will need to host it first.

Hosting a service
Next,add a new console application to the solution.
This will be the host application.
You’ll instantiate a ServiceHost instance for the service type and configure a single endpoint.
1. Go to the Solution Explorer and add a new Console Application project to the solution.
Name the new project Host.
2. Add a reference to the System.ServiceModel assembly,and add the following using statement to Program.cs:

using System.ServiceModel;

3. You will be writing code to host the HelloIndigoService type.
Before you can do this, you must add a reference to the HelloIndigo project.
4. Create a ServiceHost instance and endpoint for the service.
Open Program.cs in the code window and modify the Main( ) entry point,adding the code shown in Example 1-1.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Host
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(HelloIndigo.HelloIndigoService), new Uri("http://localhost:8000/HelloIndigo")))
            {
                host.AddServiceEndpoint(typeof(HelloIndigo.IHelloIndigoService), new BasicHttpBinding(), "HelloIndigoService");
                host.Open();
                Console.WriteLine("Press <ENTER> to terminate the service host");
                Console.ReadLine();
            }
        }
    }
}

Example 1-1. Code to programmatically initialize the ServiceHost

This code initializes a ServiceHost instance specifying the service type and a base address where relative service endpoints can be located.
It also adds a single relative endpoint for the service.
In this case,a base address is provided for HTTP protocol,and the relative endpoint uses one of the standard bindings, BasicHttpBinding, based on HTTP protocol.
5. Compile and run the host to verify that it works.
From Solution Explorer,rightclick on the Host project node and select “Set as Startup Project.”
Run the project (F5),and you should see console output similar to that shown in Figure 1-17.

Figure 1-17. Console output for the host application
6. Stop debugging and return to Visual Studio.
You now have a host application for the service.
When it is running clients will be able to communicate with the service.
The next step is to create a client application.

Creating a proxy to invoke a service
Now you will create a new console application to test the service.
To do this,the client requires metadata from the service and information about its endpoint.
This information will be used to initialize a client proxy that can invoke service operations.

1. Go to Solution Explorer and add a new Console Application to the solution.
Name the new project Client.
2. As you might expect,this project also requires a reference to System.ServiceModel.
Add this reference and add the following using statement to Program.cs:

using System.ServiceModel;

3. Copy the service contract to the client.
First,add a new class to the Client project,naming the file ServiceProxy.cs.
Open this new file in the code window and add the IHelloIndigoService contract metadata as shown in Example 1-2.

Example 1-2. Service contract metadata for the client

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Client
{
    [ServiceContract(Namespace = "http://www.thatindigogirl.com/samples/2006/06")]
    public interface IHelloIndigoService
    {
        [OperationContract]
        string HelloIndigo();
    }
}

This service contract supplies the necessary metadata to the client,describing namespaces and service operation signatures.
4. Now you can add code to invoke the service endpoint.
Open Program.cs and modify the Main( ) entry point by adding the code as shown in Example 1-3.

Example 1-3. Code to invoke a service through its generated proxy

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            EndpointAddress ep = new EndpointAddress("http://localhost:8000/HelloIndigo/HelloIndigoService");
            IHelloIndigoService proxy = ChannelFactory<IHelloIndigoService>.CreateChannel(new BasicHttpBinding(), ep);
            string s = proxy.HelloIndigo();
            Console.WriteLine(s);
            Console.WriteLine("Press <ENTER> to terminate Client.");
            Console.ReadLine();
        }
    }
}

This code uses the ChannelFactory to create a new channel to invoke the service.
This strongly typed channel reference acts as a proxy.
The code also initializes an EndpointAddress with the correct address and binding expected by the service endpoint.
5. Test the client and service.
Compile the solution and run the Host project first,followed by the Client project.
The Client console output should look similar to that shown in Figure 1-18.

In the next few sections,I will explain in more detail the steps you completed and
the features you explored in the lab.

时间: 2024-11-05 20:43:27

Learning WCF Chapter1 Creating a New Service from Scratch的相关文章

Learning WCF Chapter1 Generating a Service and Client Proxy

In the previous lab,you created a service and client from scratch without leveraging the tools available to WCF developers. Although this helps you to understand the raw requirements for sending messages between clients and services,in reality,develo

Learning WCF Chapter1 Exposing Multiple Service Endpoints

So far in this chapter,I have shown you different ways to create services,how to expose a service endpoint and metadata exchange endpoint,how to generate client proxies,how to work with metadata,and how to configure service behaviors. In this section

Learning WCF Chapter1 Summary

SummaryThis chapter covered a lot of ground,beginning with a look at the purpose of WCF,the problems it solves and its alignment with SOA,through discussion of the fundamentals developers should know before they begin working with WCF. I also touched

转载——Java与WCF交互(二):WCF客户端调用Java Web Service

在上篇< Java与WCF交互(一):Java客户端调用WCF服务>中,我介绍了自己如何使用axis2生成java客户端的悲惨经历.有同学问起使用什么协议,经初步验证,发现只有wsHttpBinding可行,而NetTcpBinding不可行,具体原因待查.昨晚回去重新测试WCF客户端调用Java Web Service,并将过程公布如下: 其实本不需要做web service,只是原来公开的经典的Web service像(http://soapinterop.java.sun.com/rou

【转】WCF光芒下的Web Service

WCF光芒下的Web Service 学习.NET的开发人员,在WCF的光芒照耀下,Web Service 似乎快要被人遗忘了.因为身边做技术的人一开口就是WCF多么的牛逼!废话不多,本人很久不写博客,今天总结一下最近几日遇到的几个关于WebService 跨语言的调用问题. 1 WebService 简介 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术.是:通

Creating a RFID Reader From Scratch

Creating a RFID Reader From Scratch Part 1 For a couple of years now, each fall, I've been attending the ritual of collecting sheep from their  summer pastures in the mountains to get them back to their farm and owners for the winter. The way this wo

Learning WCF Chapter2 Service Contracts

A service contract describes the operations supported by a service,the message exchange pattern they use,and the format of each message. The service contract is also the main driver for generating a service description. A valid WCF service implements

Learning WCF Chapter2 Service Description

While messaging protocols are responsible for message serialization formats,there must be a way to capture the protocols required to communicate between clients and services. In Chapter 1,you worked with WSDL documents,which describe the requirements

Learning WCF Chapter2 Data Contracts

A data contract describes how CLR types map to XSD schema definitions. Data contracts are the preferred way to enable serialization of complex types included in operation signatures as parameters or return values. You create a data contract by applyi