Manual WCF

Embed Size (px)

Citation preview

Wcf Manual Bsico

CHAPTER 1 Contracts

LESSON 1: DEFINING BEHAVIORAL CONTRACTSSERVICE CONTRACTS Y SERVICE TYPES 3 atributos que caracterizan a los WCF: ServiceContractAttribute, OperationContractAttribute MessageParameterAttribute[ServiceContract()] public interface ITaskManagerService { [OperationContract()] int AddTask( string taskDescription, string assignedTo); // Etc... }

y

-

ServiceContractAttribute: Declara el tipo del ServiceContract (aplicarlo siempre en la interfaz)

Named Parameter Name

Namespace CallbackContract

Description Specifies a different name for the contract instead of the default, which is simply the interface or class type name. This contract name is what will appear in the portType name when consumers access the WSDL. Specifies a target namespace in the WSDL for the service. The default namespace is http://tempuri.org. Associates another Service contract as a Callback contract. This sets up the infrastructure for a duplex MEP and thereby allows the service to initiate sending messages to the client. The Message Exchange Patterns section of this lesson discusses MEPs in more detail. Enables the Service contract to specify constraints on how messages to all operations in the contract are protected on the wire, that is, whether they are signed and encrypted. See Chapter 8, User-Level Security, for more about security. Specifies the name attribute of the service element in a configuration file. The default is the name of the service implementation class, that is, the service type. Specifies whether sessions should be enabled by the endpoint exposing this Service contract. See Chapter 10, Sessions and Instancing, for more about sessions and session management. Aconsejable utilizar la propiedad Namespace para proporcionar un identificador uniforme de recursos(URI) Utilizar la propiedad Name, para eliminar el I de la interfaz

ProtectionLevel

ConfigurationName SessionMode

y y

[ServiceContract(Name = "TaskManagerService", Namespace = "http://schemas.fabrikam.com/2008/04/tasks/")] public interface ITaskManagerService { // Etc... }

-

OperationContractAttribute: Se aplica a los mtodos que son parte de un ServiceContract

Named Parameter Description Name Specifies a different name for the operation instead of using the default, which is the method name. Action Controls the action header for messages to this operation. ReplyAction Controls the action header for response messages from this operation. IsOneWay Indicates whether the operation is one-way and has no reply. When operations are one-way, ReplyAction is not supported. IsOneWay is covered in more detail later in this lesson, in the section called OneWay, while discussing MEPs. ProtectionLevel Enables the Service contract to specify constraints on how messages to all operations in the contract are protected on the wire, that is, whether they are signed and encrypted. See Chapter 8 for more about security. The setting for this property at the operation level overrides the ProtectionLevel property of the ServiceContractAttribute. IsInitiating IsTerminating Indicates whether invoking the operation initiates a new session between the caller and the service. See Chapter 10 for more about sessions and session management. Indicates whether invoking the operation terminates an existing session between the caller and the service. See Chapter 10 for more about sessions and session management.

[ServiceContract] public interface SomeCrudContract { [OperationContract(IsOneWay = true, Action = "urn:crud:insert")] void ProcessInsertMessage(Message message); [OperationContract(IsOneWay = true, Action = "urn:crud:update")] void ProcessUpdateMessage(Message message); [OperationContract(IsOneWay = true, Action = "urn:crud:delete")] void ProcessDeleteMessage(Message message); // The catch-all operation: [OperationContract(IsOneWay = true, Action = "*")] void ProcessUnrecognizedMessage(Message message); }

-

MessageParameterAttribute: Controla los nombres de los parmetros de los servicios y de

los valores que devuelve. Solo tiene el atributo Name.

[OperationContract()] [return: MessageParameter(Name = "responseString")] string SomeOp([MessageParameter(Name = "string")]string s);

PATRONES DE INTERCAMBIO DE MENSAJES (MEP) Los MEP describen el protocolo de intercambio de mensajes que el cliente debe realizar para conversar correctamente con el servicio. Por ejemplo, si un cliente enva un mensaje, necesita saber si debe esperar un mensaje de vuelta o si simplemente enviar la solicitud es suficiente. Los 3 MEP: Request/Response: El ms utilizado y el que menos hay que configurar. IsOneWay=false.[ServiceContract()] public interface ILogisticsService { [OperationContract()] WorkOrderAcknowledgement SubmitWorkOrder(WorkOrder workOrder); [OperationContract()] void CancelWorkOrder(int workOrderNumber); }

-

OneWay: Si despus del mensaje no quieres recibir nada del servicio[ServiceContract()] public interface ILogisticsService { [OperationContract(IsOneWay=true)] void CancelWorkOrder(int workOrderNumber); }

y y -

No se puede utilizar con FaultContract (utiliza duplex) Si se desea que la entrega de los mensajes sea en cola, se debe utilizar OneWay

Duplex: Doble canal de mensajes que se utiliza cuando el cliente enva un mensaje al servicio para iniciar un proceso largo de ejecucin y, posteriormente exige confirmacin. Este MEP esta declarado para asociar un ServiceContract con un Callback[ServiceContract] interface IGreetingHandler { [OperationContract(IsOneWay = true)] void GreetingProduced(string greeting); }

[ServiceContract(CallbackContract = typeof(IGreetingHandler))] interface IGreetingService { [OperationContract(IsOneWay = true)] void RequestGreeting(string name); } [ServiceBehavior(InstanceContextMode =InstanceContextMode.PerSession)] class GreetingService : IGreetingService { public void RequestGreeting(string name) { Console.WriteLine("In Service.Greet"); IGreetingHandler callbackHandler = OperationContext.Current.GetCallbackChannel(); callbackHandler.GreetingProduced("Hello " + name); } }

CANALES DUPLEX Y SERVIDORES PROXY DE CLIENTE Puntos a tener en cuenta antes de usar Duplex: Duplex es problemtico en el mundo real porque necesita volver a conectarse con el cliente. Da problemas por cuestiones de seguridad No escala muy bien Si se utiliza Duplex se pierde interoperabilidad Problemas de Threading cuando las operaciones en cada extremo del canal no son OneWay

LESSON 2: DEFINING STRUCTURAL CONTRACTSDATA CONTRACTS[DataContract(Namespace = "http://schemas.fabrikam.com/customers/")] public enum TitleOptions : int { [EnumMember()] Mr = 0, [EnumMember()] Ms = 1, [EnumMember()] Mrs = 2, [EnumMember()] Dr = 3, }

[DataContract(Namespace = "http://schemas.fabrikam.com/customers/")] public class ContactInfo { [DataMember(IsRequired = false)] public string PhoneNumber; [DataMember(IsRequired = false)] public string EmailAddress; }

y

DataContractAttribute: Tipo del DataContract. Se puede aplicar a enum, structs y class, no es heredable y tiene 2 parametros:

Named Parameter Description Name Determines the type name as it is generated in the resulting schema. The default is the .NET type name as it is declared. Namespace Sets the target namespace for the schema. This defaults to http://schemas.datacontract.org/2004/07/[CLR namespace], where [CLRnamespace] is the namespace in which the complex type is defined. y DataMemberAttribute: Se aplica a los miembros del DataContract.

Named Parameter Description Name Controls the schema element name generated for the member (that is, the field or property) the attribute adorns. The default behavior is to use the field or property name as defined in the .NET type. IsRequired Controls the minOccurs attribute for the schema element. The default value is false, that is, the element is optional, which translates into minOccurs = 0. Order Controls the order of each element in the schema. By default, if this property is not set explicitly, the data members appear alphabetically, followed by elements for which this property is set explicitly. EmitDefaultValue Controls whether default values will be included in the serialization. This property is true by default, so that all data members are serialized. If this property is set to false, any member that is set to its default value for its type (for instance, null for reference types) will not be serialized.

-

Collections: Se puede utilizar varios tipos de colecciones (siempre y cuando implementen las interfaces IEnumerable o IEnumerable)[ServiceContract()] interface ITaskManager { [OperationContract()] Task[] GetTasksByAssignedName( string name); }

-

Known Types: WCF tambin proporciona KnownTypeAttribute que permite designar clases derivadas de un determinado DataContract[DataContract()] [KnownType(typeof(LoanApprovalTask))] class Task { // Etc... } [DataContract()] class LoanApprovalTask : Task { // Etc... }

Cuando se trabaja KnownTypeAttribute

con

tipos

polimrficos

en

el

ServiceContract,

se

requiere

[ServiceContract()] interface ITaskManager { [OperationContract()] List GetTasksByAssignedName( string name); }

MESSAGE CONTRACTS Al desarrollar servicios WCF, los ServiceContracts permiten definir la estructura de los datos que sern enviados en el cuerpo de los mensajes SOAP, ya sea en la entrada (peticin) o en la salida (respuesta). Razones para usar Mensajes: - Controlar como los Mensajes SOAP estn estructurados y serializados - Dar y acceder a encabezados personalizados Atributos de los mensajes: y MessageContractAttributeNamed Parameter IsWrapped Description If true, the message body includes a wrapper element named by using the Message contract type or the WrapperName if specified. If false, the message body is unwrapped, and body elements appear directly beneath it. Enables the Service contract to specify constraints on how messages to all operations in the contract are protected on the wire, that is, whether they are signed and encrypted. See Chapter 8 for more about security. Supplies a custom name for the body wrapper element.

ProtectionLevel

WrapperName

WrapperNamespace Supplies a namespace for the body wrapper element.

y

MessageHeaderAttribute: Se aplican a los miembros del Message Contract para indicar que elementos pertenecen

Named Parameter Name Namespace ProtectionLevel

Description Controls the name of the serialized header element. Supplies a namespace for the header element and its children unless otherwise overridden at the type level. Enables the Service contract to specify constraints on how messages to all operations in the contract are protected on the wire, that is, whether they are signed and encrypted. See Chapter 8 for more about security. A URI value indicating which actor is the intended target of the header. By default, the receiving service is assumed. Indicates whether the recipient of the header (designated through the Actor property) is required to process this header. If this property is set to true and the actor doesnt understand the header, the actor should issue a fault. Indicates whether this header should be forwarded to the next recipient of the message in the event the message is not being processed by the actor.

Actor MustUnderstand

Relay

y

MessageBodyMemberAttribute : se aplica a los miembros del Message Contract para declarar qu elementos pertenecen al cuerpo del mensaje.

Named Parameter Name Namespace ProtectionLevel

Description Controls the name of the serialized body element. Supplies a namespace for the body element and its children unless otherwise overridden at the type level. Enables the Service contract to specify constraints on how messages to all operations in the contract are protected on the wire, that is, whether they are signed and encrypted. See Chapter 8 for more about security. Controls the order of each element in the schema. By default, if this property is not set explicitly, the data members appear alphabetically, followed by elements for which this property is set explicitly. The same rules are used to apply ordering as are used with Data contracts. (See the section titled The DataMemberAttribute.)

Order

CONTROLAR LA SERIALIZACIN Como asegurarnos que los mensajes enviados y recibidos estn en el formato XML correcto y Serialization vs Encoding: Cuando estamos trabajando con un objecto DataContract o con un objeto serializable y queremos devolverlo en alguna operacin debemos serializarlo en un objeto mensaje (representacin de un mensaje SOAP). WCF tiene 2 mtodos para serializar: DataContractSerializar, XmlSerializer. El siguiente paso es codificar el mensaje en un flujo de bytes, para ello WCF dispone de 4 codificadores: Estndar XML, MTOM, WCF-WCF binary encoding. Atributos de los Serializadores: Ambos serializadores tienen los atributos Rpc y Document, pero adems, XmlSerializer dispone de Use.

y

[ServiceContract()] [XmlSerializerFormat(Style=OperationFormatStyle.Rpc, Use=OperationFormatUse.Encoded)] interface ISomeLegacyService { [OperationContract()] string SomeOp1( string name); } [ServiceContract()] [DataContractFormat(Style=OperationFormatStyle.Rpc)] interface ISomeRpcService2 { [OperationContract()] string SomeOp2( string name);

} o

Caractersticas: El atributo por defecto es Document, Rpc est anticuado. DataContractSerializer es el serializador por defecto, aunque hay algunos esquemas complejos XML o XSD que es mejor serializar con XmlSerializer

FAULTCONTRACT FaultException FaultException FaultContract DataContract FaultException DataContract

y

y

Manejo de excepciones en el Servicio: o Un DataContract en un .cs para cada tipo de excepcin en Entities o En la interfaz del Servicio, para cada OperationContract declaramos un [FaultContract(typeof( .cs))] o En el Servicio lanzamos las excepciones (throw new ) Manejo de excepciones en Cliente o Capturamos las excepciones con bloques Using, Try, Catch

CHAPTER 2 Exposing the Service

LESSON 1: SERVICE ENDPOINT BASICABC DEL ENDPOINT y ADDRESS = Localizacin del servicio y BINDING =Como acceder al servicio o En ideas generales: Conexiones en el mismo Pc = netNamedPipeBinding A travs de varios Pc = netTcpBinding or netPeerTcpBinding Interoperabilidad crtica o Pc sin WCF = basicHttpBinding or wsHttpBinding Llamadas encoladas o descanectadas = Microsoft Message Queue (MSMQ) o Todos los Binding detalladamente:basicHttpBinding This interoperable binding is commonly used as a replacement for earlier Web services based on ASMX (Active Server Methods). It supports Hypertext Transfer Protocol (HTTP) and Hypertext Transfer Protocol over SSL (HTTPS) transport protocols as well as text and Message Transmission Optimization Mechanism (MTOM) encoding methods. This secure and interoperable binding uses SOAP over HTTP and supports reliability, transactions, and security over the Internet. It supports HTTP and HTTPS transport protocols as well as text and MTOM encoding methods. This interoperable binding is commonly used for duplex service contracts because it supports bidirectional communication. This secure and interoperable binding sends information directly over HTTP or HTTPS without creating a SOAP envelope. It is an efficient choice when SOAP is not required by the client.

wsHttpBinding

wsDualHttpBinding webHttpBinding

wsFederationHttpBinding This secure and interoperable binding supports federated security. It supports HTTP and HTTPS transport protocols as well as text and MTOM encoding methods. netTcpBinding This secure binding is used to send binary-encoded SOAP messages from one WCF computer to another. It uses Transmission Control Protocol (TCP) and includes support for reliability, transactions, and security. This secure binding should be used on a single WCF computer. Binary-encoded SOAP messages are sent over named pipes. This queued binding is used to send binary-encoded SOAP messages over MSMQ. Communication should occur between two computers. This secure binding is used for peer-to-peer communication Communication should occur between two or more computers. over TCP.

netNamedPipeBinding netMsmqBinding netPeerTcpBinding

msmqIntegrationBinding This interoperable binding can be used for existing MSMQ applications that use COM and native C++ application programming interfaces (APIs). basicHttpContextBinding This binding provides support for HTTP cookies and enables SOAP headers to exchange context. netTcpContextBinding wsHttpContextBinding This secure binding enables SOAP headers to be used in the exchange of content. This secure and interoperable binding enables SOAP headers to exchange context while also supporting reliability, transactions, and security.

y

CONTRACT = Operaciones del servicio

CREAR ENDPOINT USANDO UN CONFIGURATION FILE y USANDO SERVICE MODEL

o Si hay varios Endpoints, el Address debe ser diferente. Si tienen diferentes Contracts si pueden tener el mismo AddressUSANDO BASE ADDRESS

o

Ms eficiente si hay varios Endpoints

CREAR ENDPOINT USANDO CDIGO y Usamos el mtodo AddService

Uri httpAddress = new Uri("http://localhost:8000/OrderService/"); Uri tcpAddress = new Uri("net.tcp://localhost:8001/OrderService/"); Uri[] baseAddresses = {httpAddress, tcpAddress}; ServiceHost host = new ServiceHost(typeof(MyNamespace.OrderService),baseAddresses); BasicHttpBinding basicBinding = new BasicHttpBinding(); WSHttpBinding wsBinding = new WSHttpBinding(); NetTcpBinding netBinding = new NetTcpBinding(); host.AddServiceEndpoint( typeof(MyNamespace.IOrderService), basicBinding, ""); host.AddServiceEndpoint( typeof(MyNamespace.IOrderService), wsBinding, "secure"); host.AddServiceEndpoint( typeof(MyNamespace.IOrderService), netBinding,

"");

PUBLICAR METADATAS A TRAVS DE LOS ENDPOINT y y Se publica y se accede a Metadata usando Http-Get (por eso hay que habilitar a true su nodo) Creamos un endpoint especial:

LESSON 2: CUSTOMIZING & EXTENDING BINDINGSPERSONALIZAR UN STANDARD BINDING (en cliente) y yProperty AllowCookies BypassProxyOnLocal CloseTimeout

Existen muchas propiedades a cambiar y cada binding posee las suyas por defecto Parmetros personalizablesDescription Default Value Boolean value that determines whether the client accepts False and propagates cookies. Boolean value that determines whether to bypass the proxy False server for local addresses. TimeSpan value that indicates how much time should pass 1 minute while closing a connection before an exception is thrown.

HostNameComparisonMode Determines whether the hostname is used to reach the StrongWildcard service when matching the URI. MaxBufferPoolSize MaxRecievedMessageSize MessageEncodng Name Namespace OpenTimeout ProxyAddress ReaderQuotas Long value that determines maximum amount of memory 65,536 bytes allocated for the buffer manager. Long value that determines maximum size for a message. 65,536 bytes

Determines whether MTOM or Text/XML is used to encode Text messages. Name of the binding. Can be helpful if you use multiple Null reference versions of a certain binding. Determines the XML namespace for the binding. http://tempuri.org/

TimeSpan value that indicates how much time should pass 1 minute while opening a connection before an exception is thrown. Determines the URI address of the HTTP proxy. Property is Null reference ignored if UseDefaultWebProxy is true. Determines the constraints applied to string content within None a message. For example, can be used to specify a maximum length for string content. TimeSpan value that determines how long a connection can 10 minutes remain inactive before an exception is thrown. TimeSpan value that determines how long a write operation 1 minute can take before an exception is thrown. Determines the encoding used for message text. UTF8Encoding

ReceiveTimeout SendTimeout TextEncoding TransactionFlow UseDefaultWebProxy

Boolean value that determines whether the binding should false support flowing WS-Transactions. Boolean value that determines whether the autoconfigured True HTTP proxy should be used.

y

Tambin se puede crear un Binding desde cero, pero es mejor personalizar uno creado.

CHAPTER 3 Deploying Services

LESSON 1: HOSTING A SERVICE ON A WEB SERVERHOSPEDAR UN SERVICIO EN UN WEBSERVER IIS y PASOS: - Crear un WCF SERVICE - En el .SVC incluir el nombre del servicio y DESVENTAJA: Solo se puede utilizar el protocolo Http HOSPEDAR USANDO WAS y y Similar al IIS pero puedes utilizar los dems protocolos (MSMQ, TCP, PIPES) adems del Http Hay que configurar en el WAS los protocolos que se quieran usar

CREAR UN SERVICIO HOST FACTORY y Se utiliza para crear instancias de Servicios Host de forma dinmica conforme llegan las peticiones

LESSON 2: HOSTING A SERVICE IN A MANAGED APPHOSPEDAR USANDO CONSOLE APPLICATION y En el cdigo se debe crear y abrir especficamente una instancia del objeto ServiceHost:static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(OrderService))) { host.Open(); Console.WriteLine("OrderService is ready.\nPress a key to exit"); Console.ReadKey(true); host.Close(); }

}HOSPEDAR USANDO UN WINDOWS SERVICE y Los Windows Service son tiles cuando se trata de WCF de larga duracin que no requieren un interfaz de usuario y PASOS: o Usar la plantilla de VisualStudio WindowsService o Declarar la variable ServiceHost public ServiceHost serviceHost = null;

o

Para situar el servicio en una ConsoleWindows, configura la propiedad ServiceName. Tambin hay que declarar un punto de entrada para la aplicacin que hospeda.public OrderWindowsService() { ServiceName = "Order Service Windows Service"; } public static void Main() { ServiceBase.Run(new OrderWindowsService());

o

} Aadir cdigo al mtodo OnStart para crear una instancia del ServiceHost y al OnClose para cerrarloprotected override void OnStart(string[] args) { if (serviceHost != null) { serviceHost.Close(); } serviceHost = new ServiceHost(typeof(OrderService)); serviceHost.Open();

}protected override void OnStop() { if (serviceHost != null) { serviceHost.Close(); serviceHost = null; }

o

} Crear la clase ProjectInstaller[RunInstaller(true)] public class ProjectInstaller : Installer { private ServiceProcessInstaller process; private ServiceInstaller service; public ProjectInstaller() { process = new ServiceProcessInstaller(); process.Account = ServiceAccount.LocalSystem; service = new ServiceInstaller(); service.ServiceName = "Order Service WindowsService"; Installers.Add(process); Installers.Add(service); }

}

o

Configurar el AppConfig (baseAddress y los endpoints). Una vez compilado podemos crear un servicio ejecutable. Usando la lnea de cdigo en el prompt de VS puedes instalarlo installutil C:\MyProjects\bin\OrderWindowsService.exe Una vez instalado puedes accede a travs del Service Control Manager escribiendo en el prompt services.msc

HOSPEDAR USANDO WCF-PROVIDED HOST Para hospedar servicios de forma rpida y fcil para principiantesWcfSvcHost.exe /service:C:\MyProject\bin\SimpleService.dll /config:C:\MyProject\App.Config

CHAPTER 4 Consuming Services

LESSON 1: Consuming WCF ServicesCREAR PROXIES Y CLASES PROXIES WCF tiene varios mecanismos para crear proxies que se usan para comunicarse con Web Service. y Creando clases Proxy usando Servicio Metadata: Lo ms comn para crear Proxies es crear una clase basada en el Metadata del servicio y luego instanciar la clase para crear el objeto Proxy. y Usar SVCUTIL para crear la clase Proxy: Desde el prompt. Las opciones ms usadas de la herramienta svcutil son:Option /out: Description Specifies the filename for the generated code. Default: derived from the WSDL definition name, WSDL service name, or target namespace of one of the schemas (short form: /o). Specifies the output.config. filename for the generated config file. Default:

/config: /mergeConfig /language:

Merges the generated configuration file into an existing file instead of overwriting the existing file. Indicates the programming language in which to generate code. Possible language names are c#, cs, csharp, vb, visualbasic, c++, and cpp. Default: csharp (short form: /l). Maps a WSDL or XML schema target namespace to a .NET namespace. Using an asterisk (*) for the target namespace maps all target namespaces without an explicit mapping to the matching .NET namespace. Default: derived from the target namespace of the schema document for Data contracts. The default namespace is used for all other generated types (short form: /n). Generates Message contract types (short form: /mc). Generates both synchronous and asynchronous method signatures. Default: generates only synchronous method signatures (short form: /a). Generates data types that use the XmlSerializer for serialization and deserialization.

/namespace:

/messageContract /async

/serializer:XmlSerializer

Un ejemplo:svcutil /async /config:app.config /mergeConfig /namespace:*,SvcUtilProxy /out:ServiceUtilProxy.cs http://localhost:8080/orders/

Desde VS creamos un WPF Application . Aadir una Service Reference con el botn derecho en el Solution Explorer y configurarla para crear una clase Proxy (Address, Namespace, Generate Asynchronous, ObservableCollection, etc ). y Crear una clase Proxy manualmente: Suponiendo el siguiente ServiceContract[ServiceContract()] public interface IOrderEntryService { [OperationContract()] OrderAcknowledgement SumbitOrder(Order order);

// Etc...

Podemos crear la clase Proxy:public class OrderEntryServiceProxy : ClientBase,IOrderEntryService { public OrderEntryServiceProxy( Binding binding, EndpointAddress epAddr) : base(binding,epAddr) { } public OrderEntryServiceProxy( string endpointConfigurationName) : base(endpointConfigurationName) { } public OrderAcknowledgement SumbitOrder(Order order) { return this.Channel.SumbitOrder(order); } }

y

Crear un Proxy dinmicamente:Binding binding = new NetTcpBinding(); ChannelFactory factory; factory = new ChannelFactory( binding, "net.tcp://localhost:6789/orders/"); try { IOrderEntryService proxy = factory.CreateChannel(); Order order = new Order(); order.Product = "Widget-ABC"; order.Quantity = 10; // Etc... OrderAcknowledgement ack = proxy.SumbitOrder(order); Console.WriteLine( "Order submitted; tracking number: {0}", ack.TrackingNumber); } catch (Exception ex) { Console.WriteLine("Error: {0}",ex.Message);

USAR PROXIES PARA LLAMAR A SERVICIOS Es tan fcil como llamar a un mtodo del objeto Proxy. Suponiendo el siguiente servicio:[DataContract()] public class Order { [DataMemberAttribute()] public string Product; [DataMemberAttribute()] public int Quantity; // Etc... } [DataContract()] public class OrderAcknowledgement { [DataMemberAttribute()] public string TrackingNumber; // Etc... } [ServiceContract()] public interface IOrderEntryService { [OperationContract()] OrderAcknowledgement SumbitOrder(Order order); // Etc... } public class OrderEntryService : IOrderEntryService { public OrderAcknowledgement SumbitOrder(Order order) { OrderAcknowledgement ack = new OrderAcknowledgement(); ack.TrackingNumber = "alpha-bravo-123"; return ack; } // Etc... }

Podemos llamar al Proxy en varios pasos: 1) Usar la clase ChannelFactory para crear un Proxy dinmicamenteBinding binding = new NetTcpBinding(); ChannelFactory factory; factory = new ChannelFactory( binding, "net.tcp://localhost:6789/orders/"); IOrderEntryService proxy = factory.CreateChannel(); Order order = new Order(); order.Product = "Widget-ABC"; order.Quantity = 10;

// Etc... OrderAcknowledgement ack = proxy.SumbitOrder(order); Console.WriteLine( "Order submitted; tracking number: {0}", ack.TrackingNumber);

2) Definir manuelamente la clase Proxypublic class OrderEntryServiceProxy : ClientBase,IOrderEntryService { public OrderEntryServiceProxy( Binding binding, EndpointAddress epAddr) :base(binding,epAddr) { } public OrderEntryServiceProxy( string endpointConfigurationName) : base(endpointConfigurationName) { } public OrderAcknowledgement SumbitOrder(Order order) { return this.Channel.SumbitOrder(order); } }

3) Usar manualmente el ProxyBinding binding = new NetTcpBinding(); EndpointAddress epAddr = new EndpointAddress( "net.tcp://localhost:6789/orders/"); IOrderEntryService proxy = new OrderEntryServiceProxy(binding,epAddr); new Order(); order.Product = "Widget-ABC"; order.Quantity = 10; // Etc...

Order order =

OrderAcknowledgement ack = proxy.SumbitOrder(order); Console.WriteLine("Order submitted; tracking number: {0}", ack.TrackingNumber);

4) Si has utilizado SVCUTIL o VS para generar la clase Proxy, debes usar la clase generada as:OrderEntryServiceClient proxy = new OrderEntryServiceClient(); Order order = new Order(); order.Product = "Widget-ABC";

order.Quantity = 10; // Etc... OrderAcknowledgement ack = proxy.SumbitOrder(order); Console.WriteLine("Order submitted; tracking number: {0}", ack.TrackingNumber); y

Invocar operaciones del Servicio Asncronamente: En muchas ocasiones, una llamada a un mtodo en un Proxy, el cual es transformado en un mensaje puede tardar demasiado tiempo. Una buena solucin es llamar al mtodo asncronamente. Podemos usar la opcin /async del SVCUTIL. Despus en Add Service Reference->Advanced seleccionamos Generate Asynchronous Methods. Suponemos que se ha generado un proxy para OrderEntryService con mtodos asincrnicos. Adems de la operacin SubmitOrder en la clase de proxy, existen los mtodos BeginSubmitOrder y EndSubmitOrder. Estos mtodos pueden ser usado tal que as:OrderEntryServiceClient proxy = new OrderEntryServiceClient(); Order order = new Order(); order.Product = "Widget-ABC"; order.Quantity = 10; // Etc... AsyncCallback cb = new AsyncCallback(HandleCallback); proxy.BeginSumbitOrder(order, cb, proxy); Console.WriteLine( "Order submitted asynchronously; waiting for callback");

El Mtodo HandleCallback (definido abajo) es llamado cuando la invocacin del mtodo asncrono ha sido completada.static void HandleCallback(IAsyncResult result) { OrderEntryServiceClient proxy = result.AsyncState as OrderEntryServiceClient; OrderAcknowledgement ack = proxy.EndSumbitOrder(result); Console.WriteLine( "Order submitted; tracking number: {0}", ack.TrackingNumber); }

y

Cerrar el Proxyusing(MyServiceClient proxy = new MyServiceClient()) { proxy.SomeOp1(); }

Si se trata de una interfaz:IMyService proxy = new MyServiceClient(); using (proxy as IDisposable) {

proxy.SomeOp1(); }

y

Duplex Channels con Proxies Para habilitar un Proxy usando un Duplex Channel o un Callback Channel, debes seguir los siguiente pasos: o Define la clase que implemente el Callback contract o Crea una instancia de la clase y pasala al constructor InstanceContext o Pasa el objeto InstanceContext al constructor de la clase Proxy o La clase Proxy debe heredar de DuplexClientBase en vez de ClientBase Service Agents Es muy comn que desee ajustar el uso de un proxy en otra clase que tiene algunas capacidades adicionales para interactuar con el servicio remoto. Lo que hace que estos objetos faciliten el acceso a un servicio remoto es un Service Agent.

y

CHAPTER 5 Configuring WCF

LESSON 1: Configuring the client EndpointLa configuracin de los endpoints de un cliente no es tan diferente como la del servicio. Cada endpoint est asociado a un Binding y cada endpoint tiene 1 o varios Behaviors. La diferencia es que el cliente no soporta service behavios, en su lugar puede definir callback behaviors. CONFIGURACIN DECLARATIVA

y y

ADDRES: Evalua la direccin asociada al endpoint del servicio. Es un string que representa el URI del servicio porque el cliente no soporta base address ni relative address. BINDING:Description Used to communicate with earlier (that is, pre-WCF) ASMX Web services. The same as basicHttpBinding except that context is provided to the requests through cookies. Uses MSMQ as part of the transportation channel for requests. It is expected that the service on the receiving end of the request will be a non-WCF application such as a COM+ service. Uses MSMQ as part of the channel for requests. This binding is used when both ends of the channel are WCF applications. Provides a reliable, secure channel that is optimized for same-machine, cross-process communication, using named pipes. Allows for multiparty communication, using peer networking over TCP. Provides a channel optimized for cross-machine message passing, using TCP as the underlying communications protocol. Uses the same TCP mechanism as netTcpBinding to transport messages, but context is provided through information added to the message, typically in the SOAP header. For use with endpoints that accept HTTP formatted requests (as opposed to SOAP-formatted messages). Defines a duplex channel to a service that uses SOAP formatted messages. Configures the requests for communications with a service that supports WSFederation. Uses HTTP for transport along with providing support for WS-Security and WS-Reliable Messaging. Uses the same channel as wsHttpBinding, but context is provided through cookies.

Binding Name basicHttpBinding basicHttpContextBinding msmqIntegrationBinding

netMsmqBinding netNamedPipeBinding netPeerTcpBinding netTcpBinding netTcpContextBinding

webHttpBinding wsDualHttpBinding wsFederationHttpBinding wsHttpBinding wsHttpContextBinding

ws2007FederationHttpBinding Provides the same functionality as ws2007HttpBinding along with the support

Binding Name

Description for WS-Federation.

ws2007HttpBinding

Defines a binding that provides support for the correct versions of security, reliable session, and transaction flow binding elements.

El Binding tambin se puede configurar con el atributo BindingConfiguration y CONTRACT: El nombre de la interfaz del servicio

DEFINIR ENDPOINT HEADERS Dan informacin adicional al endpoint. En este ejemplo se crean 2 endpoint con un tipo de respuesta diferente determinado en los headers (Expedite y Standard). Expedite Standard

Si se configuran los headers en el cliente, en el servicio se deben configurar de igual manera. METADATA EN EL ENDPOINT DEL CLIENTE Definen como los metadatas del servicio van a ser procesados en el cliente.Name Description policyImporters Defines the types that process the policy information provided by the services metadata. wsdlImporters Defines the types that process the policy information found in the Web Services Description Language (WSDL) associated with a service.

CONFIGURAR BINDINGS La figura muestra la comunicacin entre un servicio y un cliente.

Un ejemplo de como configurar un Binding desde el atributo bindingConfiguration:

CONFIGURAR BEHAVIORS De igual manera se puede configurar los Behaviors con el atributo behaviorConfiguration:

ELEMENTO clientCredentials Muchos servicios requieren Autentificacin y Autorizacin. ClientCredentials permite que se haga esto. Element clientCertificate Description Indicates the client certificate used to authenticate the client to the service. This element must be of type X509ClientCertificateElement. The attributes for this element are broken into two groups. The first group (storeLocation and storeName) specifies the store and location in which the X.509 certificate is found. The second group (findValue and X509FindType) identifies the certificate within the store to be used. Here is an example:

httpDigest Specifies the digest type of credential to be used when authenticating a service. An impersonationLevel attribute specifies the value. Possible values are: Identification, Impersonation, Delegation, Anonymous, and None. Here is an example:

issuedToken Specifies the custom token passed to the service for authentication. The attributes on this element are used to specify caching parameters, the channel behaviors to use when communicating with the token issuer, and the entropy mode used when handshaking with the token issuer. Here is an example: