02_WCF1x

grapeafterthoughtSoftware and s/w Development

Dec 14, 2013 (3 years and 7 months ago)

97 views

COMPSCI 335


WCF 1






radu/2009



1
.
27

















Windows Communication Foundation (WCF)


Part 1

Readings:

o

[
reference
]
MSDN Library
: .NET Development / .NET Framework SDK /.NET Framework /
Windows Communication Foundation / …

o

[
this handout is based on this great

introduction
]
Aaron Skonnard:

Learn The ABCs Of Programming Windows Communication Foundation
, MSDN
Magazine, February 2006

http://msdn.microsoft.com/en
-
us/magazine/cc163647.aspx

o

[
additional
introductory readings
] Aaron Skonnard:

o

WCF Addressing In Depth, MSDN Magazine, June 2007,

http://msdn.microsoft.com/en
-
us/magazine/cc163412.aspx

o

WCF Binding In Depth, MSDN Magazine, Jul
y 2007,

http://msdn.microsoft.com/en
-
us/magazine/cc163394.aspx

o

Serialization in Windows Communication Foundation, MSDN Magazine, August 2006,

http://msdn.microsoft.com/en
-
us/magazine/cc163569.aspx

o

WCF Messaging Fundamentals, MSDN Magazine, April 2007,

http://msdn.microsoft.com/en
-
us/magazine/cc163447.aspx



COMPSCI 335


WCF 1






radu/2009



2
.
27

















Windows Communication Foundation and Service Unification


WCF is a
modular

(orthogonal plugins),
configurable

(properties, behaviours)
extensible

framework
that
unifies

other
existing high
-
level communications

o

ASP.NET Web services

(ASMX)


o

WSE services

(addressing/routing, security, attachments, standalone)


o

Distributed Objects with .NET Remoting

(≈ RMI, CORBA, C
OM/DCOM)


o

Enterprise Services (transactions, COM+)


o

Message queues (MSMQ)


o

also connections to Workflows (e.g., Windows Workflow Foundation, BizTalk)


COMPSCI 335


WCF 1






radu/2009



3
.
27





















ABC

=
Address
,
Binding
,
Contract


o

Service

o

Endpoints



Address

(
where
, URI)



Logical = Physical



Logical Physical



Binding

(
how
)



Network transport protocols (HTTP, HTTPS, TCP, ...)



WS specific protocols (Basic, WS
-
Addressing, WS
-
Security, ...)



Message encoding (SOAP1.1, SOAP 1.2, MTOM, XML/POX, JSON,
binary
, ...)



Contract

(
what
)



Service Contract
(methods)



Data Contract (classes, types)



Message Contract


COMPSCI 335


WCF 1






radu/2009



4
.
27





















Builtin
Bindings

(partial list)

Binding Class Name

Default
Transport

Default Message
Encoding

Default Message
Version

Def Security
Mode

BasicHttpBinding

HTTP

XML Text

SOAP 1.1

None

WSHttpBinding
1

HTTP

XML Text

SOAP 1.2 WS
-
Addressing 1.0

Message

WSDualHttpBinding
1

2

HTTP

XML Text

SOAP 1.2 WS
-
Addressing 1.0

Message

WSFederationHttpBinding

HTTP

XML Text

SOAP 1.2 WS
-
Addressing 1.0

Message

NetTcpBinding
1

2

TCP

Binary

SOAP 1.2

Transport

NetPeerTcpBinding

P2P

Binary

SOAP 1.2

Transport

NetNamedPipesBinding
1

2

Named Pipes

Binary

SOAP 1.2

Transport

NetMsmqBinding

MSMQ

Binary

SOAP 1.2

Message

MsmqIntegrationBinding

MSMQ

uses pre
-
WCF

uses pre
-
WCF

Transport

CustomBinding

You
Decide

You Decide

You Decide

You Decide

o

1

builtin transaction support

o

2

builtin dual messaging support

COMPSCI 335


WCF 1






radu/2009



5
.
27




















Basic S
cenario



Bird’s Eye View


Client


Service Proxy (
C
)

here, aka Channel


ServiceHost








Service

Interface

(
C
)


Service

Implementation

(
C
)

SOAP …

(
B
)

EP

ABC

EP

ABC

Metadata WSDL+XSD

1.

Disco(very via HTTP/GET)

2.

MEX (Metadata Exchange)

3.

other ad
-
hoc means
, pigeon carrier


COMPSCI 335


WCF 1






radu/2009



6
.
27

















Service Contracts

[
ServiceContract
(
Namespace
="
http://example.org/echo/
")]

public interface IEchoService

{



[
OperationContract
]



string Echo(string msg);

}


o

The
ServiceContract

attribute annotates the interface that will be published to the clients

o

The optional (but highly recommended)
Namespace

property
is a virtual URL,
in
dicating an

XML namespace, used
for several purposes



D
efault Namespace

=
"
http://tempuri.org
" (same for everybody in the world


)

o

The
Operatio
nContract

attribute
s annotate all the

method
s

that
are made

accessible to the clients

o

Incoming messages are dispatched to methods by a parameter called
Action

o

Often related to the method name, but not identical

o

Implicit Action = Namespace + MethodName =
"
http://example.org/echo/Echo
"


COMPSCI 335


WCF 1






radu/2009



7
.
27

















Service Contracts and Explicit Dispatching


[ServiceContract(Namespace="http://example.org/echo/")]

public interface IEchoService

{



[OperationContract(
Action
="urn:echo:string")]



string Echo(string msg);




[OperationContract(
Action
="*")]



Message

EchoMessage(
Message

msg);

}

o

The optional
Action

property indicates the Action used for dispatching (overrides the default)

o

Could be any virtual URL, or, more
general, an URN (what does it mean?)

o

Could be
"*"
, which indicates that any message with no matching Action will be dispatched to
this method (this allows flexible formats)

o

The
Message

type is a system defined base type for more specific messages, that fle
xibly matches
any incoming message (SOAP)


COMPSCI 335


WCF 1






radu/2009



8
.
27

















Data Contracts

[
DataContract
(
Namespace
="
http://example.org/person
")]

public class Person
{



[
DataMember
(
Name
="first",
Order
=0)]


public string First;



[
DataMember
(
Name
="last",
Order
=1)]


public string Last;



[
DataMember
(
IsRequired
=false,
Order
=2)]

private string id;

}

o

The
DataContract

attribute annotates a class that will be serialized by DataContractSerializer, the
main serialization engine used by WCF (there are several others)

o

The optional Namespace property indicates the XML namespace used to serialize this class

o

The
DataMember

att
ributes annotate all class members that will be serialized (regardless if they are
public or not)

o

The optional
Name

property overrides the member name in the serialization

o

The optional
Order

property indicates the relative serialization order (default in t
extual order)

o

The optional
IsRequired

property indicates that a member must be present when deserializing
(otherwise it raises a SerializationException)


COMPSCI 335


WCF 1






radu/2009



9
.
27

















Data Contracts


Other Options

[ServiceContract]

[
XmlSerializerFormat
]

public
interface IEchoService { ... }


o

The
XmlSerializerFormat

attribute annotates an interface that will be serialized by XmlSerializer, an
older serializer, with a few exotic options not present in the newer DataContractSerializer


[
Serializable
]

public
class

Person

{ ... }


o

The
Serializable

attribute annotate a class that will be serialized by the Remoting serializer, an older
serializer initially used by Remoting (making easier to port or use older Remoting applications)


COMPSCI 335


WCF 1






radu/2009



10
.
27

















Message Contracts

[
MessageContract
]

public class EchoPersonMessage

{



[
MessageBod
yMember
]



public Person Person;



[
MessageHeader
]



public Authorization Authorization;

}


o

The
MessageContract

attribute annotates a class whose members are explicitly mapped to a SOAP
message

o

The
MessageBod
yMember

attribute indicates a member that will be transferred as a SOAP
body element

o

The
MessageHeader

attribute indicates a member that will be transferred as a SOAP header



COMPSCI 335


WCF 1






radu/2009



11
.
27

















Service Contract Implementation

[
ServiceBehavior
(



InstanceContextMode
=InstanceContextMode.
Single
,



ConcurrencyMode
=ConcurrencyMode.
Multiple
)]

public class
EchoService : I
EchoService
{



public string Echo(string msg) { return msg; }

}

o

The

optional

ServiceBehavior

attribute annotates an implementation class, indicating its additional
internal behaviours (not directly visibile in the service contract)

o

The
InstanceContextMode

property indicates how
instances

of the implementation class are
automatically created and recycled



(briefly)
Single
: one instance shared by all clients



PerCall
: one instance for each client call



PerSession
: one instance for each client

session (a series of calls from the same client,
more later)

o

The
ConcurrencyMode

attribute indicates how many
threads

should be automatically used to
service client calls:
Single
,
Multiple
,
Reentrant

(more later)



COMPSCI 335


WCF 1






radu/2009



12
.
27

















Hosting the Service
in a Standalone .NET Application

o

Cre
a
te a new instance of the
ServiceHost

class, indicating

o

The service implementation class



Instances will be
automatically created and threaded
,
as indicated by the ServiceBehavior
(see before)

o

The
base

URL



A listener will

start listening at this URL



For
HTTP

(only), and
if

enabled, a
discovery

facility will become available at

o

base URL +
"
wsdl
"

query string =
"
http://localhost:8080/echo?wsdl
"



Various service endpoints will either



use directly the base URL



use a relative URL to the base URL



use a
new URL (with its own listener)

ServiceHost host = new
ServiceHost
(



typeof(EchoService),



new Uri("
http
://localhost:8080/echo
")
);


COMPSCI 335


WCF 1






radu/2009



13
.
27

















Hosting the Service in a Standalone .NET Application (cont’d)


o

AddServiceEndpoint
‘s, indicating

o

The service
Contract

interface exposed (there could be several)

o

The
Binding

used (predefined or custom)

o

The
Address

used, relative to the base or absolute (possibly using another protocol)


host.
AddServiceEndpoint
(



typeof(IEchoService),



new
BasicHttpBinding
(),



"
svc
");


host.
AddServiceEndpoint
(



typeof(IEchoService),



new
NetTcpBinding
(),



"
net.tcp://localhost:8081/echo/svc
");


COMPSCI 335


WCF 1






radu/2009



14
.
27


















Hosting the Service in a Standalone .NET Application (cont’d)


o

Optionally, customize the binding
s, here

o

Basic HTTP binding

o

With secure transport (HTTPS)

o

And Basic authentication scheme (name +
password)


BasicHttpBinding b = new
BasicHttpBinding
();

b.Security.Mode = BasicHttpSecurityMode.
Transport
;

b.Security.T
ransport.ClientCredentialType =
HttpClientCredentialType.
Basic
;

host.AddServiceEndpoint(typeof(IEchoService), b, "
svc
");



COMPSCI 335


WCF 1






radu/2009



15
.
27






















Hosting the Service in a Standalone .NET Application (cont’d)

o

Optionally,
enable the discovery
"
?wsdl
"

method

ServiceMetadataBehavior smb = new ServiceMetadataBehavior();

host.Description.Behaviors.Add(smb);

smb.
HttpGetEnabled

= true;


o

Or the MEX Metadata Exchange Endpoint, that uses
one of
its predefined protocol

o

Here will listen at base URL +
"
mex
"

=
"
http://localhost:8080/echo/mex
"

host.AddServiceEndpoint(


typeof(
IMetadataExchange
),


MetadataExchangeBindings.CreateMexHttpBinding(),


"
mex
");


o

Finally
,
open the service host

host.
Open
();

o

The service starts running


COMPSCI 335


WCF 1






radu/2009



16
.
27




















Code samples

-

please see and study the actual code


o

LinqPad

sample

o

A bit simpler

o

Needs a
timeout

to keep the program and service running

o

Needs the additional
System.ServiceModel.dll

library

o

And the
System.ServiceModel

and
System.ServiceModel
.D
escription

namespaces

o

CSC

sample

o

More complex

o

Command
-
line progra
m

o

Needs a
Console.ReadLine()

prompt
,

to keep the program an
d

service running

o

Can be configured to



either use code exclusively (ignoring the CONFIG file),



or take most of its required setup from the CONFIG file, automatically



COMPSCI 335


WCF 1






radu/2009



17
.
27


















Retrieving
Service

Runtime Info

in a Standalone Host


o

The code that retrieves such runtime info is part of the CSC

sample

(only)
.

o

This code is not required

o

T
he
following runtime info is output
;
please relate the items of the following listing to the code
snippets presented!


>ServiceCsc.exe

EchoService is open at the following base addresses:


http://localhost:8080/echo


...



COMPSCI 335


WCF 1






radu/2009



18
.
27






















Retrieving
Service Runtime Info

in a Standalone Host

(cont’d)

...

EchoService has the following endpoints:


Endpoint #1 BasicHttpBinding_IEchoService


Physical Address: http://localhost:8080/echo/svc


Logical Address: http://localhost:8080/echo/svc


Binding: BasicHttpBinding


Contract: IEchoService


Endpoint #2
NetTcpBinding_IEchoService


Physical Address: net.tcp://localhost:8081/echo/svc


Logical Address: net.tcp://localhost:8081/echo/svc


Binding: NetTcpBinding


Contract: IEchoService


...


o

Optionally,

COMPSCI 335


WCF 1






radu/2009



19
.
27






















Retrieving
Service Runtime Info

in a Standalone Host (cont’d)

...


Endpoint #3 MetadataExchangeHttpBinding_IMetadataExchange


Physical Address: http://localhost:8080/echo/mex


Logical Address: http://localhost:8080/echo/mex


Binding: M
etadataExchangeHttpBinding


Contract: IMetadataExchange


The following EndpointListeners are active:


http://localhost:8080/echo/svc

net.tcp://localhost:8081/echo/svc

http://localhost:8080/echo/mex

http://localhost:8080/echo


<ENTER> to exit



COMPSCI 335


WCF 1






radu/2009



20
.
27


















Testing a Host with a
Browser

-

The WCF
Help

Page

o

Point the browser to
GET
the base URL:



COMPSCI 335


WCF 1






radu/2009



21
.
27






















Testing a Host with a
Browser

-

The 1
st

WSDL

Page

o

WSDL = Web Service Description Language

o

Point the browser to
GET
the base URL

+
"
?wsdl
"
:



COMPSCI 335


WCF 1






radu/2009



22
.
27






















Tes
ting a Host with the system
WcfTest
Client

o

Run the WcfTestClient from a batch file, with the base URL as parameter:

SET WCFC=
"
C:
\
Program Files
\
Microsoft Visual Studio
9.0
\
Common7
\
IDE
\
WcfTestClient.exe
"

%WCFC%
http://localhost:8080/echo

Formatted request /response



COMPSCI 335


WCF 1






radu/2009



23
.
27




















Testing a Hos
t with the system
WcfTest
Client

XML (SOAP) request /response

(more about this later)



COMPSCI 335


WCF 1






radu/2009



24
.
27

















Configu
ring a Host with a
CONFIG

file

(
instead of
“hardwritten”
code
)

Please relate the items of the following listing to the code snippets presented!


<configuration>


<system.serviceModel>


<
services>


<service name="EchoService"


behaviorConfiguration="HttpGetBehavior">


<host>


<baseAddresses>


<add baseAddress="http://localhost:8080/echo"/>


</baseAddresses>


</host>

...



COMPSCI 335


WCF 1






radu/2009



25
.
27

















Configuring a Host with a CONFIG file
(cont’d)


...


<endpoint address="svc"


binding="basicHttpBinding"


contract="IEchoService" />


<endpoint address="net.tcp://localhost:8081/echo/svc"


binding="netTcpBinding"


contract="IEchoService" />


<endpoint addr
ess="mex"


binding="mexHttpBinding"


contract="IMetadataExchange" />


</service>


</services>

...


COMPSCI 335


WCF 1






radu/2009



26
.
27





















Configuring a Host with a CONFIG file (cont’d)


...


<behaviors
>


<serviceBehaviors>


<behavior name="HttpGetBehavior">


<serviceMetadata httpGetEnabled="True" policyVersion="Policy15" />


</behavior>


</serviceBehaviors>


</behaviors>


<
/system.serviceModel>

</configuration>


COMPSCI 335


WCF 1






radu/2009



27
.
27
















Configuring a Host with a CONFIG file
using

the
SvcConfigEditor

tool