<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Bahar Sezin on Medium]]></title>
        <description><![CDATA[Stories by Bahar Sezin on Medium]]></description>
        <link>https://medium.com/@baharsezin?source=rss-f1ee949ab27c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dmbNkD5D-u45r44go_cf0g.png</url>
            <title>Stories by Bahar Sezin on Medium</title>
            <link>https://medium.com/@baharsezin?source=rss-f1ee949ab27c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 12:43:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@baharsezin/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[WCF Service Behaviors and How to Customize Them]]></title>
            <link>https://medium.com/adessoturkey/wcf-service-behaviors-and-how-to-customize-them-fb87a004cf8f?source=rss-f1ee949ab27c------2</link>
            <guid isPermaLink="false">https://medium.com/p/fb87a004cf8f</guid>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[services]]></category>
            <category><![CDATA[wcf]]></category>
            <category><![CDATA[soa]]></category>
            <dc:creator><![CDATA[Bahar Sezin]]></dc:creator>
            <pubDate>Mon, 02 Jan 2023 08:00:16 GMT</pubDate>
            <atom:updated>2023-01-02T08:00:16.799Z</atom:updated>
            <content:encoded><![CDATA[<p>This article will discuss the WCF service behaviors and how to customize them. But before diving into the details of those topics, let’s kick off by describing what WCF is. Then, we’ll look at one of the most important layers of WCF. Throughout, service runtime behaviors will be explained in depth, and a few samples will be shown about customizing service behaviors in WCF.</p><p><strong>Service runtime layer of WCF</strong></p><p>The layer known as the “service runtime behaviors” specifies and manages the service’s behaviors that take place during service operation, or service runtime. Service-type behaviors are governed by service behaviors. They have no control over endpoint or message behaviors. Likewise, endpoint and message behaviors have no control over service behaviors.</p><p>The following lists the various behaviors managed by the service runtime layer:</p><p>Throttling Behavior, Error Behavior: Instance Behavior, Message Inspection, Transaction Behavior, Dispatch Behavior, Concurrency Behavior Parameter Filtering, Transaction Behavior and Metadata Behavior.</p><p>In this article, service behavior implementation will be shown step by step with a generic code sample. Before jumping into the implementation, services and behaviors will be described briefly for a better understanding.</p><p><strong>Services</strong></p><p>A service is a unit of functionality exposed to the world. In that respect, it is the next evolutionary step in the long journey from functions to objects to components to services. Service orientation (SO) is an abstract set of principles and best practices for building serviceoriented applications. And WCF is a good example of SOA.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/543/1*o8RcaijQwxx7G4IkKLgQ7w.png" /></figure><p>The services can be local or remote, can be developed by multiple parties using any technology, can be versioned independently, and can even execute on different timelines. Inside a service, you will find concepts such as languages, technologies, platforms, versions, and frameworks, yet between services, only prescribed communication patterns are allowed. The client of a service is merely the party consuming its functionality. The client can be anything — for instance, a Windows Forms, WPF, or Silverlight class, an [ASP.NET] page, or another service.</p><p>WCF applications <strong><em>expose functionality through services</em></strong>. A service is a common language runtime (CLR) type that encapsulates business functionality and exposes a set of methods that can be accessed by remote clients. In order for a regular CLR type to be considered a service, it must implement a <strong><em>service contract</em></strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/697/1*7Q7KtoyR8NQuWlAaI8MzCg.png" /></figure><p><strong><em>A service contract is defined</em></strong> by applying the <strong><em>ServiceContractAttribute</em></strong> to a class or interface. When applied to a class, the class becomes a service type. When applied to an interface, any class that implements the interface becomes a service type. In either case, <strong><em>methods exposed by</em></strong> the class or interface must be decorated with the <strong><em>OperationContractAttribute</em></strong> to be considered part of the service contract. Methods with this attribute are considered service operations. A service type must be hosted before clients can invoke service operations.</p><p><strong>What is a behavior?</strong></p><p>A behavior controls the runtime characteristics of a service or endpoint. Behaviors are grouped according to their scope, in that they control aspects relevant to the object to which they are applied. For example, service behaviors affect aspects related to the service, and endpoint behaviors affect only properties related to the endpoint.</p><p>To support that and a few other local service-side aspects, WCF defines the notion of behaviors. A behavior is a local attribute of the service or the client that does not affect its communication patterns. Clients should be unaware of service behaviors, and behaviors do not manifest themselves in the service’s binding or published metadata.</p><p>In WCF, behavior types are added to the service or endpoint description objects prior to those objects being used by WCF. Together, the behavior types and description objects create a runtime that executes a WCF service or client, which when called during runtime, can access runtime properties and methods that modify the runtime behaviors that are constructed via the address, binding, and contract. WCF contains four kinds of behaviors, each corresponding to a specific scope of access. These are service, contract, endpoint, and operation.</p><p><strong>Service:</strong> Primary method of modifying the service runtime as a whole via the <strong>IServiceBehavior interface</strong>.</p><p>Service behaviors can be added by:</p><ol><li>Applying an attribute on the service class.</li><li>Adding the behavior to the behaviors collection on ServiceDescription programmatically.</li><li>Adding a custom BehaviorExtension element to the configuration.</li></ol><p><strong>Defining a service</strong></p><ol><li>The first step in creating a service is to define a service contract. You create a service contract by applying the ServiceContractAttribute to an interface or type. Methods on the interface or type will not be included in the service contract until the OperationContractAttribute is applied.</li><li>In a typical service contract, all methods will be included in the contract. After all, the entire reason for defining a service contract is to expose operations as part of a service.</li><li>The service layer should instead be explicitly defined with the sole purpose of exposing public functionality and should internally consume business components rather than embed business logic with the service implementation. When you implement a service contract as an interface, the service type implements this interface.</li><li>When you apply the ServiceContractAttribute to the service type, the service type name becomes the official name of the service contract. Thus, this is the name that must be provided when you create a new endpoint.</li></ol><p>In the example below, a service implements a single service contract, ICalcService. This contract contains a single operation, AddNumbers().</p><pre>[ServiceContract (Namespace=&quot;http://www.samples.com/samples/2006/06&quot;)]    <br>public interface ICalcService <br>{ <br>[OperationContract] <br>int AddNumbers(int number1, int number2);<br>}</pre><p>Once a service is defined and hosted, a behavior for this service can be configured.</p><p><strong>Service behaviors</strong></p><p>When implemented, service contracts create a class that makes the service available to clients by combining address and binding information. However, despite all of this information and functionality, it is still essential to control the service’s execution aspects and characteristics.</p><p>For example, how can service instances be managed? The answer is simple: behaviors! The runtime characteristics of <strong>WCF services </strong>are <strong><em>manipulated and controlled by</em></strong> objects known as <strong>service behaviors.</strong></p><p>WCF defines two types of declarative service-side behaviors, managed by two corresponding attributes:</p><ol><li>The<strong> ServiceBehaviorAttribute </strong>is used to configure service behaviors; that is, behaviors that affect all endpoints (all contracts and operations) of the service. Apply the ServiceBehavior attribute directly to the service implementation class.</li><li>Use the <strong>OperationBehaviorAttribute</strong> to configure operation behaviors; that is, behaviors that affect only the implementation of a particular operation. The OperationBehavior attribute can be applied only to a method that implements a contract operation, never to the operation definition in the contract itself.<br></li></ol><blockquote><strong>PitFall: </strong>Unlike other attributes, such as the [ServiceContract] and [OperationContract] attributes, which are defined inside an interface, the <strong>[ServiceBehavior] </strong>and <strong>[OperationBehavior] </strong>attributes are applied to the class that implements the interface.</blockquote><p>So, let’s merge all the information above for defining a service behavior. We’ve already defined our ServiceContract and OperationContract. Here’s how to define a behavior with this structure:</p><p>Firstly, a behavior should be implemented through an interface.</p><pre>[ServiceBehavior]<br>public class ServiceBehaviorClass : ICalcService<br>{<br> [OperationBehavior]<br> public int AddNumbers(int number1, int numbers2)<br> {<br> return number1 + number2;<br> }<br>}</pre><p>The [ServiceBehavior] and [OperationBehavior] attributes, like the other elements, offer a number of accessible properties that help determine the behaviors of the service. Because no properties were specifically given in the example above, the attributes would take on the default values of each property. <strong><em>The [ServiceBehavior] attribute</em></strong> comes from the System.ServiceModel.ServiceBehaviorAttribute class and specifies the execution behavior of a service contract implementation. <strong><em>The OperationBehavior attribute</em></strong> comes from the System.ServiceModel.OperationBehaviorAttribute class and specifies the execution behavior of a service operation.</p><p>If the service behavior class extends from .Net Attribute class; you can use it to annotate your service implementation. WCF is able to detect such service behaviors and it will load and attach them to the service.</p><p><strong>Add custom service behaviors via web.config file</strong></p><p>The above section tried to explain how service behaviors can be added with code in order to better understand the working structure of WCF.</p><p>But all the behaviors can be managed on the config file, and it can be customized with an extension as well. If you don’t want to add the service behavior in code but rather in the web.config file, all you must do is to implement a class that extends from BehaviorExtensionElement. Again, this is easy:</p><pre>public class ServiceBehaviorExtensionElement : BehaviorExtensionElement<br>{<br>    public override Type BehaviorType<br>    {<br>        get { return typeof (ServiceBehaviorAttribute); }<br>    }<br> <br>    protected override object CreateBehavior()<br>    {<br>        return new ServiceBehaviorAttribute();<br>    }<br>}</pre><p>The example given below is a generic structure for customized service behaviors. It shows how to add customized service behaviors with a config file. All the code implementations can be managed in the config file, you only need to know tag descriptions, and how they work.</p><pre>&lt;?xml version=&quot;1.0&quot;?&gt;<br>     &lt;configuration&gt;<br>     &lt;system.serviceModel&gt;<br>         &lt;services&gt;<br>               &lt;service name=&quot;Service&quot; behaviorConfiguration=&quot;ServiceBehavior&quot;&gt;<br>           &lt;endpoint address=&quot;&quot; binding=&quot;wsHttpBinding&quot; contract=&quot;IService&quot;&gt;         <br>               &lt;identity&gt;<br>                    &lt;dns value=&quot;localhost&quot;/&gt;<br>               &lt;/identity&gt;<br>             &lt;/endpoint&gt;<br>              &lt;endpoint address=&quot;mex&quot; binding=&quot;mexHttpBinding&quot; contract=&quot;IMetadataExchange&quot;/&gt;<br>           &lt;/service&gt;     <br>         &lt;/services&gt;   <br>              &lt;behaviors&gt;<br>                &lt;serviceBehaviors&gt;<br>                  &lt;behavior name=&quot;ServiceBehavior&quot;&gt;<br>                &lt;serviceMetadata httpGetEnabled=&quot;true&quot;/&gt;<br>               &lt;serviceDebug includeExceptionDetailInFaults=&quot;false&quot;/&gt;<br>              &lt;/behavior&gt;<br>            &lt;/serviceBehaviors&gt;<br>           &lt;/behaviors&gt;<br>           &lt;extensions&gt;  <br>          &lt;behaviorExtensions&gt;  <br>             &lt;add<br>                name=&quot;&quot;  <br>                type=&quot;classNameSpace_Name, HostApplication, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null&quot;  <br>                /&gt;  <br>          &lt;/behaviorExtensions&gt;  <br>        &lt;/extensions&gt;<br>         &lt;/system.serviceModel&gt;<br>     &lt;/configuration&gt;</pre><p><strong> &lt;services&gt;:</strong> The section contains a collection of services. For each service defined in the assembly, this element contains a service element specifying settings for the service.<br><strong> &lt;behaviors&gt;: </strong>This element defines two child collections named endpointBehaviors and serviceBehaviors. Each collection defines behavior elements consumed by endpoints and services respectively. Each behavior element is identified by its unique name attribute.<br><strong> &lt;serviceBehaviors&gt;:</strong> This configuration section represents all the behaviors defined for a specific service.<br><strong> &lt;extensions&gt;: </strong>This element contains extended behaviors.</p><p><strong>Summary</strong></p><p>WCF’s service layer, how services and their behavior work, and the difference between ServiceContracts and ServiceBehaviors were explained. The last section gave an example of how to change/add service or serviceBehaviors without touching the code with a config file.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fb87a004cf8f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/adessoturkey/wcf-service-behaviors-and-how-to-customize-them-fb87a004cf8f">WCF Service Behaviors and How to Customize Them</a> was originally published in <a href="https://medium.com/adessoturkey">adessoTurkey</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>