290 likes | 489 Views
WCF 101. Barry Dorrans http://idunno.org. About the Speaker (the ego slide). MVP – Visual Tools, Security barryd@idunno.org. Agenda What is WCF? The ABCs of WCF Defining a contract Hosting the service Handling Errors Logging. What is WCF?. Indigo
E N D
WCF 101 Barry Dorrans http://idunno.org
About the Speaker(the ego slide) MVP – Visual Tools, Security barryd@idunno.org
Agenda What is WCF? The ABCs of WCF Defining a contract Hosting the service Handling Errors Logging
What is WCF? Indigo Replaces Web Services, WSE & Remoting Interoperability
The ABCs of WCF Address Binding Contract
Client Server Messaging Layer Endpoint Service Model Address Binding Channel Behaviour Behaviour Contract Binding Binding Channel Behaviour Behaviour Factory Listener Channel Address Address Channel *
Address scheme:// <machineName>[:port]/path http://localhost:8080/Account/ net.tcp://localhost:8080/Account
Binding How you communicate with a service A service may have multiple bindings Defines the shape; security etc.
Contract The external interface Contracts for service, data and message
The ABCs Address = WHERE Binding = HOW Contract = WHAT
Service Contracts [ServiceContract()] public interface IMyService { [OperationContract] string MyOperation1(string myValue); [OperationContract] string MyOperation2(MyDataContract value); }
Data Contracts [DataContract] public class MyDataContract { string _stuff; [DataMember] public string Stuff { get { return _stuff; } set { stuff = value; } } }
So what have we learned? VS2008 gives us a test harness Service behaviour in config Barry can’t type under pressure.
Why data contracts? Message body or Message Header Control over the order of members Required versus optional parameters
So what have we learned? We need data contracts Why? – Version Tolerance WCF exposes nothing by default This is the opposite of XML Serialization
So what have we learned? We can host in IIS or a .net program So we can use WCF for inter-process or inter-program communication
Hosting in a program Create a new ServiceHost and Open using (ServiceHost host = new ServiceHost(typeof(MyService))) { host.Open(); ..... }
Hosting in IIS Add a .svc file <%@ServiceHost Service="Namespace.ServiceType" %> <%@Assembly Name="AssemblyName" %>
Replacing ASMX Double Decorate Contracts Watch the action & message names Remap ASMX to WCF Only use basicHttpBinding
Gotcha! Registering addresses netsh http add urlaclurl=http://+:8080/echoService user=BUILTIN\Users
Getting service information foreach (ServiceEndpoint endpoint in myServiceHost.Description.Endpoints) { Console.WriteLine("Endpoint - address: {0}", endpoint.Address); Console.WriteLine("- binding name:\t\t{0}", endpoint.Binding.Name); Console.WriteLine("- contract name:\t\t{0}", endpoint.Contract.Name); }
So what have we learned? Raw exceptions are not helpful Inner exceptions are still not good Contracted faults are the way to go