Windows
Communication Foundation Services and WCF Data Services in Visual Studio
Visual Studio 2008
provides tools for working with Windows Communication Foundation (WCF) and WCF
Data Services, Microsoft technologies for creating distributed applications.
This topic provides an introduction to services from a Visual Studio
perspective.
Windows Communication
Foundation (WCF) is a unified framework for creating secure, reliable,
transacted, and interoperable distributed applications. In earlier versions of
Visual Studio, there were several technologies that could be used for
communicating between applications.
If you wanted to share
information in a way that enabled it to be accessed from any platform, you
would use a Web service (also known as an ASMX Web service). If you wanted to
just move data between a client and server that was running on the Windows
operating system, you would use .NET Remoting. If you wanted transacted
communications, you would use Enterprise Services (DCOM), or if you wanted a
queued model you would use Message Queuing (also known as MSMQ).
WCF brings together
the functionality of all those technologies under a unified programming model.
This simplifies the experience of developing distributed applications.
WCF Data Services are
services that interact directly with a database, allowing you to return data
using standard HTTP verbs such as GET, POST, PUT or DELETE. In general, WCF
Data Services are a good choice for applications that are used to create, update,
or delete records in a database. For more information, see ADO.NET Data Services Framework.
The WCF Programming
model is based on communication between two entities: a WCF service and a WCF
client. The programming model is encapsulated in the System.ServiceModel namespace in the .NET
Framework.
A WCF service is based
on an interface that defines a contract between the service and the client. It
is marked with a ServiceContractAttribute attribute, as
shown in the following code:
C#
[ServiceContract]
public interface IService1
C#
[OperationContract]
string GetData(string value);
You define functions
or methods that are exposed by a WCF service by marking them with a OperationContractAttribute attribute. In
addition, you can expose serialized data by marking a composite type with a DataContractAttribute attribute. This
enables data binding in a client.
After an interface and
its methods are defined, they are encapsulated in a class that implements the interface.
A single WCF service class can implement multiple service contracts.
A WCF service is
exposed for consumption through what is known as an endpoint. The
endpoint provides the only way to communicate with the service; you cannot
access the service through a direct reference as you would with other classes.
An endpoint consists
of an address, a binding, and a contract. The address defines where the service
is located; this could be a URL, an FTP address, or a network or local path. A
binding defines the way that you communicate with the service. WCF bindings
provide a versatile model for specifying a protocol such as HTTP or FTP, a
security mechanism such as Windows Authentication or user names and passwords,
and much more. A contract includes the operations that are exposed by the WCF
service class.
Multiple endpoints can
be exposed for a single WCF service. This enables different clients to
communicate with the same service in different ways. For example, a banking
service might provide one endpoint for employees and another for external
customers, each using a different address, binding, and/or contract.
A WCF client consists
of a proxy that enables an application to communicate with a
WCF service, and an endpoint that matches an endpoint defined for the service.
The proxy is generated on the client side in the app.config file and includes
information about the types and methods that are exposed by the service. For
services that expose multiple endpoints, the client can select the one that
best fits its needs, for example, to communicate over HTTP and use Windows
Authentication.
After a WCF client has
been created, you reference the service in your code just as you would any
other object. For example, to call the GetData method shown earlier, you would write
code that resembles the following:
C#
private void
button1_Click(System.Object sender, System.EventArgs e)
{
ServiceReference1.Service1Client client = new
ServiceReference1.Service1Client();
string returnString;
returnString =
client.GetData(textBox1.Text);
label1.Text =
returnString;
}
No comments:
Post a Comment