You are on page 1of 32

Visual Studio® 2008:

Windows®
Communication
Foundation
Module 3: Endpoints and Behaviors
• Exposing WCF Services over Different Endpoints

• Adding Behaviors to Services and Endpoints

• Interoperating with Non-WCF Web Services


Lesson: Exposing WCF Services over Different
Endpoints
• Relating Services to Endpoints

• Bindings and Channels

• HTTP-Based Bindings

• Connection-Oriented Bindings

• Queue-Based Bindings

• Changing an Endpoint to Use a Different Binding

• Using Both Bindings

• Exposing Multiple Contracts

• Configuring Bindings

• Demonstration: Using a Different Binding


Relating Services to Endpoints

Contract B Contract A

TCP Endpoint HTTP Endpoint


WCF Service

Intranet Client Internet Client


Bindings and Channels

Your choice of binding will be influenced by: WCF


Dispatcher Service
• Style of communication
• Interoperability requirements
• Security requirements
• Performance requirements
• Transactional requirements

Encoding

Client Transport
Channel

A basicHttpBinding channel stack


HTTP-Based Bindings

• Typically used for Internet, extranet, and integration

• Sacrifice some performance for interoperability

• BasicHttpBinding

• WsHttpBinding

• WsDualHttpBinding

• WsFederationHttpBinding
Connection-Oriented Bindings

• Typically used for local networking and intranets

• Focus on performance and optimization

• NetTcpBinding

• NetNamedPipeBinding
Queue-Based Bindings

• Typically used for local networking and intranets

• Focus on reliability and throughput

• NetMsmqBinding

• MsmqIntegrationBinding
Changing an Endpoint to Use a Different Binding

<service name="ConnectedWCF.BankService">
<endpoint address="BankService"
binding="basicHttpBinding"
contract="ConnectedWCF.IBank"/>
<host>
<baseAddresses>
<baseAddress baseAddress="http://localhost:8080/Simple"/>
</baseAddresses>
</host>
</service>

<service name="ConnectedWCF.BankService">
<endpoint address="BankService"
binding="netTcpBinding"
contract="ConnectedWCF.IBank" /> Change binding
<host> attribute and address
<baseAddresses>
<baseAddress baseAddress="net.tcp://localhost:9001/Simple"/>
</baseAddresses>
</host>
</service>
Using Both Bindings

WCF runtime routes to the same service implementation

<service name="ConnectedWCF.BankService">
<endpoint address="BankService"
binding="basicHttpBinding"
contract="ConnectedWCF.IBank"/>
<endpoint address="BankService"
binding="netTcpBinding"
contract="ConnectedWCF.IBank"/>
<host>
<baseAddresses>
<baseAddress
baseAddress="http://localhost:8080/Simple"/>
<baseAddress
baseAddress="net.tcp://localhost:9001/Simple"/>
</baseAddresses>
</host>
</service>
Exposing Multiple Contracts
<services>
<service name="ConnectedWCF.BankService">
<endpoint address="BankService"
binding="basicHttpBinding"
contract="ConnectedWCF.IBank"/>
<host>
<baseAddresses>
<baseAddress
baseAddress="http://localhost:8080/Simple"/>
</baseAddresses>
</host>
</service>
<service name="ConnectedWCF.OrderingService">
<endpoint address="OrderingService"
binding="basicHttpBinding"
contract="ConnectedWCF.IOrdering"/>
<host>
<baseAddresses>
<baseAddress
baseAddress="http://localhost:8080/Simple"/>
</baseAddresses>
</host>
</service>
</services>
Configuring Bindings

<system.serviceModel>
<services>
<service name="BankService" >
<endpoint address="http://localhost:8080/BankService"
contract="IBankService"
binding="basicHttpBinding"
bindingConfiguration="myBasicHttpBindingConfig"/>
</service>
</services>
</system.serviceModel>

<bindings>
<basicHttpBinding>
<binding name="myBasicHttpBindingConfig">
<security mode="Transport"/>
</binding>
</basicHttpBinding>
</bindings>
Demonstration: Using a Different Binding
In this demonstration, you will see how to change the
binding used by a WCF client and service
Lesson: Adding Behaviors to Services and Endpoints
• WCF Behaviors

• Service Behaviors

• Operation Behaviors

• Endpoint Behaviors

• Demonstration: Adding Metadata Exchange to a WCF


Service by Using a Behavior
WCF Behaviors

WCF Configuration

WCF Runtime

Behavior
Configuration
Service Behaviors
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]
public class BankService : IBank
{
...
}

<services>
<service name="ConnectedWCF.BankService"
behaviorConfiguration="metadataAndDebugEnabled">
...
</service>
</services>
<behaviors>
<serviceBehaviors
<behavior name="metadataAndDebugEnabled">
<serviceDebug includeExceptionDetailInFaults="true" /
<serviceMetadata httpGetEnabled="true" httpGetUrl="" />
</behavior>
</serviceBehaviors>
</behaviors>
Operation Behaviors

Operation behaviors control aspects such as:


• Transactions
• Security
• Instance management
Define operation behaviors with the OperationBehavior attribute

[OperationBehavior(TransactionAutoComplete = true,
TransactionScopeRequired = true)]
[TransactionFlow(TransactionFlowOption.Mandatory)]
public string Transfer(string sourceAccount, decimal amount)
{
...
}
Endpoint Behaviors

<system.serviceModel>
<services>
<service name="BankService" >
<endpoint address="http://localhost:8080/BankService"
contract="IBankService"
binding="basicHttpBinding"
bindingConfiguration="myBasicHttpBindingConfig"
behaviorConfiguration="bankServiceCredentialBehavior"/>
</service>
</services>
</system.serviceModel>

<endpointBehaviors>
<behavior
name="bankServiceCredentialBehavior">
<!– Configuration elements go here. -->
</behavior>
</endpointBehaviors>
Demonstration: Adding Metadata Exchange to a
WCF Service by Using a Behavior
In this demonstration, you will see how to use a service
behavior to add metadata exchange capability to a WCF
service
Notes Page Over-flow Slide. Do Not Print Slide.
See Notes pane.
Lesson: Interoperating with Non-WCF Web Services
• Overview of WCF Web Service Interoperability

• Interoperability with Older Web Services

• Demonstration: Consuming an ASP.NET Web Service

• Web Service Standards Supported by WCF

• Example: Security Interoperability

• Creating a Custom Binding


Overview of WCF Web Service Interoperability

Legacy Web services prior to the WS-* standardization

Newer services based on the WS-* protocol suite

POX services that use raw XML


Interoperability with Older Web Services

• Expose an HTTP/Get metadata endpoint


• Use basicHttpBinding
• Use default ASP.NET 2.0 Web service settings
• Do not use custom serialization
Demonstration: Consuming an ASP.NET Web Service
In this demonstration, you will see how to consume an
ASP.NET Web Service with a WCF client
Notes Page Over-flow Slide. Do Not Print Slide.
See Notes pane.
Web Service Standards Supported by WCF

• WSDL 1.1 and XSD 1.0 generated from contracts as metadata


• HTTP 1.1, and HTTP bindings for SOAP 1.1 and SOAP 1.2
• WS Addressing profiles
• WS-SecurityPolicy 1.0
• Various WSS, WS-Trust, and WS-SecureConversation
• WS-Coordination, and WS-AtomicTransaction
• WS-MetadataExchange, WS-Transfer, and WS-Policy
Example: Security Interoperability

WSS SOAP Message Security


• Basic binding: User name and password, or X.509 certificate
<BasicHttpBinding>
<binding name=“BankServiceBinding">
<security mode="TransportWithMessageCredential">
<transport credentialType="Basic"/>
</security>
</binding>
</BasicHttpBinding>

• WS binding: Username, certificate, and Kerberos protocol


(security mode set to “Message” for all of these)
<BasicHttpBinding>
<binding name=“BankServiceBinding">
<security mode="Message">
<message credentialType="Certificate"/>
</security>
</binding>
</BasicHttpBinding>
Creating a Custom Binding

ReliableSessionBindingElement rel =
new ReliableSessionBindingElement();

HttpTransportBindingElement trans =
new HttpTransportBindingElement();
trans.AuthenticationScheme = AuthenticationSchemes.Anonymous;
trans.HostNameComparisonMode =

HostNameComparisonMode.StrongWildcard;

BindingElement[] elements = new BindingElement[2];


elements[0] = rel;
elements[1] = trans;

CustomBinding binding = new CustomBinding(elements);


Notes Page Over-flow Slide. Do Not Print Slide.
See Notes pane.
Lab: Changing Service Endpoints and Behaviors
• Exercise 1: Exposing Services by Using Different Bindings

• Exercise 2: Adding Metadata Exchange to a Service

• Exercise 3: Creating WCF Clients and Services That


Interoperate with Non-WCF Web Services

Logon information

Virtual machine 6461-LON-DEV-03

User name Student

Password Pa$$w0rd

Estimated time: 80 minutes


Lab Review
• What do you think are the security implications for
exposing a metadata endpoint over the Web?
• Why would you want to use TCP instead of HTTP in a local
environment?
• What type of binding other than the basicHttpBinding
binding could you use to interoperate with the Patient
Letter service?
Module Review and Takeaways
• Review Questions

• Best Practices

You might also like