Developing a WCF service (regarding its distributed service
oriented nature) consists of three general steps that you will see in a moment.
Each step may have some smaller steps for its own. Based on the approach you
follow and your needs for your application, it is possible to exclude one of
these small steps. However, main steps are constant.
·
Developing the Service
·
Developing the Client (or Clients)
·
Deployment
Let us see more details about these three steps.
Developing the Service
Here is where we provide all means to give necessary
services to clients. Usually we deploy one instance of a service to be used by
as many clients that need this service. Developing a good service depends on
our understanding about some principles that I talked about before. Choosing
the best configurations to set up our channels, endpoints and providing our
services is the main job that we need to do here.
A service can be hosted in Internet Information Services
(IIS) or can have its own logic for hosting. In the first case we do not need
to care about starting or stopping a service and IIS does this automatically
for us (so needs less code), but things are not as easy as this forever and
sometimes we need to write self-hosted services. Thankfully, this is possible
and needs some extra code.
In addition to having a good Service Oriented Architecture
for our application to build our service on its top, we need to choose the best
choice for messaging pattern, contracts, schemas, security, etc. and this is
not possible without practicing and writing different services.
There are three approaches in WCF development process:
·
Code First: In this approach you implement the service code logic
then generate metadata information for your service.
·
Contract First: In the second approach you write your metadata information
then begin writing service or client code because here their order does not
matter and both of them follow this metadata information.
·
Contractless: In this third approach (which is less common) you
get down to a lower level and work directly with messages and ignore contracts.
In the first and second approaches (Code First and Contract
First) you can speed up your process by using a handy code generator tool that
comes with .NET Framework 3.0 and is named SvcUtil. SvcUtil helps you to work
with metadata information easier. You know working with XSD and WSDL is not
easy and SvcUtil comes to help you in this way. SvcUtil does three jobs for
developers:
·
Generates WSDL and XSD metadata from the service assembly.
·
Generates proxy code for the client from service metadata from
MEX endpoint.
·
Generates proxy code for the client from service metadata from
WSDL and XSD.
Note that some services are self describing. It means that
you can provide MEX endpoints for a service and in this case you do not need to
generate any code using SvcUtil. Metadata information will be generated for
service automatically.
You can run "SvcUtil /?" command from command line
for more information about SvcUtil.
After choosing your approach to write service logic and
contract, you can go to the next step to host your service on IIS or write your
own code for self-hosting and configure your service based on your needs.
After configuration you can move to the client side and
start writing your client using the proxy code that you have gotten from
SvcUtil and your own configurations (created manually) or configurations that
are generated by SvcUtil. After this your application will be ready.
Do not worry! We will see an example to demonstrate these
theories.