- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
Lesson 3: WCF Extensibility in C#.NET
Lesson 3: WCF Extensibility Painting Data Matrix In Visual C# Using Barcode generator for VS .NET Control to generate, create DataMatrix image in .NET applications. www.OnBarcode.comECC200 Decoder In Visual C# Using Barcode decoder for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications. www.OnBarcode.compublic void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) { MessageLogInspector inspector = new MessageLogInspector(); clientRuntime.MessageInspectors.Add(inspector); } public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { MessageLogInspector inspector = new MessageLogInspector(); endpointDispatcher.DispatchRuntime.MessageInspectors.Add(inspector); } public void Validate(ServiceEndpoint endpoint) { } } Paint Bar Code In Visual C#.NET Using Barcode printer for VS .NET Control to generate, create bar code image in VS .NET applications. www.OnBarcode.comBarcode Scanner In C# Using Barcode recognizer for VS .NET Control to read, scan read, scan image in .NET framework applications. www.OnBarcode.comNow that you have a behavior, it needs to be added to a behavior extension. After the behavior is added to such an extension, it can be added to the WCF pipeline through the configuration file. The behavior extension is actually a class that derives from the BehaviorExtensionElement class, and the minimum functionality for the class is to override the CreateBehavior method so that it returns an instance of the desired behavior. The following code demonstrates this, using the LoggingEndpointBehavior: Printing ECC200 In .NET Using Barcode printer for ASP.NET Control to generate, create DataMatrix image in ASP.NET applications. www.OnBarcode.comPainting Data Matrix 2d Barcode In Visual Studio .NET Using Barcode creation for VS .NET Control to generate, create Data Matrix 2d barcode image in .NET applications. www.OnBarcode.com' VB Public Class LoggingBehaviourExtensionElement Inherits BehaviorExtensionElement Public Overrides ReadOnly Property BehaviorType() As Type Get Return GetType(LoggingEndpointBehavior) End Get End Property Protected Overrides Function CreateBehavior() As Object Return New LoggingEndpointBehavior (); End Function End Class // C# public class LoggingBehaviorExtensionElement : BehaviorExtensionElement { public override Type BehaviorType { get { return typeof(LoggingEndpointBehavior); } } Painting Data Matrix In VB.NET Using Barcode maker for .NET framework Control to generate, create Data Matrix 2d barcode image in .NET framework applications. www.OnBarcode.comBar Code Creation In C#.NET Using Barcode encoder for .NET framework Control to generate, create bar code image in .NET applications. www.OnBarcode.com 6
Encoding ECC200 In C#.NET Using Barcode printer for VS .NET Control to generate, create ECC200 image in Visual Studio .NET applications. www.OnBarcode.comDrawing UPC Symbol In Visual C#.NET Using Barcode encoder for Visual Studio .NET Control to generate, create UCC - 12 image in Visual Studio .NET applications. www.OnBarcode.comInstrumentation
Code 39 Creation In C#.NET Using Barcode drawer for .NET Control to generate, create Code 3 of 9 image in .NET framework applications. www.OnBarcode.comMake USS Code 93, USS 93 In Visual C#.NET Using Barcode creation for Visual Studio .NET Control to generate, create Uniform Symbology Specification Code 93 image in .NET framework applications. www.OnBarcode.comprotected override object CreateBehavior() { return new LoggingEndpointBehavior(); } } Code 128C Generation In None Using Barcode generator for Online Control to generate, create Code128 image in Online applications. www.OnBarcode.comDrawing ANSI/AIM Code 128 In Objective-C Using Barcode printer for iPhone Control to generate, create Code 128A image in iPhone applications. www.OnBarcode.comAfter the behavior extension is implemented, you can add the behavior (which is a message inspector) by adding a behaviorExtension element in the extensions section of the serviceModel element. After the extension has been referenced, you can add it to an endpointBehavior element as shown in bold in the following code: EAN13 Drawer In None Using Barcode creator for Online Control to generate, create EAN13 image in Online applications. www.OnBarcode.comPrinting ANSI/AIM Code 128 In Java Using Barcode printer for Java Control to generate, create Code 128 Code Set C image in Java applications. www.OnBarcode.com<system.serviceModel> <behaviors> <endpointBehaviors> <behavior name="LoggingEndpointBehavior"> <messageLogger /> </behavior> </endpointBehaviors> </behaviors> <extensions> <behaviorExtensions> <add name="messageLogger" type="assembly.LoggingBehaviorExtensionElement, assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/> </behaviorExtensions> </extensions> </system.serviceModel> Code 3/9 Drawer In None Using Barcode maker for Online Control to generate, create Code 39 image in Online applications. www.OnBarcode.comUPC-A Supplement 2 Creation In None Using Barcode creation for Font Control to generate, create GS1 - 12 image in Font applications. www.OnBarcode.comYou might notice the fully qualified assembly name in the behaviorExtensions tag. This is a requirement, at least at the moment. If the type name is not fully qualified, a ConfigurationErrorsException exception is thrown. In fact, in what some consider to be a bug, the type name is actually white space sensitive. If you removed any of the spaces from the type name, the same ConfigurationErrorsException is thrown. Exam Tip Encoding Data Matrix ECC200 In Objective-C Using Barcode generator for iPhone Control to generate, create Data Matrix image in iPhone applications. www.OnBarcode.comGenerate Data Matrix 2d Barcode In None Using Barcode encoder for Office Excel Control to generate, create ECC200 image in Microsoft Excel applications. www.OnBarcode.comYou ll notice from the example that the name of the behavior extension matches the name of the node within the behavior. This is required for the extension to function properly. Alternatively, the behavior can be added imperatively by accessing the endpoint behaviors through the proxy object. Making the assumption that the proxy class is called UpdateServiceClient, the following code will added to the just created logging message inspector: ' VB Dim client As New UpdateServiceClient() client.Endpoint.Behaviors.Add(New LoggingEndpointBehavior()) // C# UdpateServiceClient client = new UpdateServiceClient(); client.Endpoint.Behaviors.Add(new LoggingEndpointBehavior()); Lesson 3: WCF Extensibility
Lab: Using Extensibility Points
In this lab, you will explore some of the extensibility points exposed by WCF. In particular, you will use the parameter inspection technique to validate that a particular parameter matches a regular expression pattern. You will also use message inspection to send the contents of the request and response message to an output window. Exercise 1 Use Message Inspection In this first exercise, you will use message inspection to direct a copy of the request and response messages to an output window. The purpose of the exercise is to demonstrate how you can use inspection to access the message. When accessed, you can modify the message prior to sending it, including adding information or changing the format. 1. Navigate to the <InstallHome>/6/Lesson3/Exercise1/<language>/Before directory and double-click the Exercise1.sln file to open the solution in Visual Studio. The solution consists of two projects. They are as follows: The DemoService project, a simple WCF service library that implements the IContact interface. This interface consists of a single method (UpdatePhone) that retrieves some information about the headers in the message sent to the service. For this exercise, the method displays the phone number that has been passed into the method in the trace output. The TestClient project, a Console application that enables you to consume the DemoService service. This client is the target of the message inspector injection. 2. In Solution Explorer, right-click the TestClient project. Select Add and then Class from the context menu. 3. In the Add New Item dialog box, change the name to MessageTracer and click Add. 4. Add the following statements to the top of the file: ' VB Imports System.ServiceModel.Dispatcher Imports System.ServiceModel.Channels Imports System.ServiceModel // C# using System.ServiceModel.Dispatcher; using System.ServiceModel.Channels; using System.ServiceModel; The MessageTracer class is the implementation of the inspection. As such, it needs to implement the IClientMessageInspector interface. 5. Change the class declaration to be the following:
|
|