Best Practices for Handling Change in Your WCF Applications

Best Practices for Handling Change in Your WCF Applications

hange is the one constant you can depend on: Requirements change, environments change, and processes change. Together, these factors ensure that your WCF services will change as well. Fortunately, you can make some basic design decisions at the outset that will make these changes much less disruptive to your service consumers and, in the end, to yourself.

This article explores not only the upfront decisions you can make to minimize changes, but also the strategies you can employ to deal with any big changes that your service consumers may not have foreseen, but that you know are coming.

Defining Change

Before diving into how to deal with change, it makes sense to explain what change means in a WCF-based service. The following actions (listed by type of contract) constitute changes:

  • Data contracts
    • Adding a data member
    • Removing a data member
    • Renaming a data member
    • Changing the type of a data member
  • Service contracts
    • Adding an operation
    • Removing an operation
    • Renaming the service contract
  • Operation contracts
    • Renaming the operation
    • Changing the signature of an operation

These changes might be the result of new business requirements, hardware consolidation, business mergers, new regulations, or any number of other external factors. The bottom line is that when something outside the developer’s control changes, the software must adjust. Dealing with change in the WCF world is a constant case of good news/bad news. You can handle some scenarios quite easily, while others will lead you to give the dreaded “yes, but?” response.

Versioning and Change in WCF

In the .NET world, one of the first considerations that come to mind when dealing with change is versioning. You can version assemblies to allow for unforeseen or breaking changes in later revisions of a component. That way, the affected clients can continue to utilize an older version of an assembly and you can avoid the headaches associated with a breaking change.

The logical question then is ‘does WCF support versioning?’ The answer is that dreaded “yes, but?” When you create a data contract in WCF, the contract generates an XML schema. Consumers reference this schema and use it to generate a proxy class. The data is not, strictly speaking, validated against this schema going forward. As you will see, this can sometimes cause unexpected and frustrating behaviors for service consumers.

Before diving into specifics, familiarize yourself with the following sample service. It provides the basis for the discussion in the remainder of the article.

namespace SampleService{    [ServiceContract]    public interface IPersonService    {        [OperationContract]        Person GetPerson(int personId);        [OperationContract]        void UpdatePerson(Person p);    }    public class Person    {        private string _firstName = string.Empty;        private string _lastName = string.Empty;        [DataMember]        public string FirstName        {            get { return _firstName; }            set { _firstName = value; }        }        [DataMember]        public string LastName        {            get { return _lastName; }            set { _lastName = value; }        }    }}

Data Contract Changes

The Person DataContract defines two attributes: FirstName and LastName. If a client were to reference this service and you were to subsequently change LastName to SurName, the client would not actually break but the LastName attribute on the client’s proxy class would appear to be empty. This is because when the client deserializes the message into the Person class, it will no longer find any element named LastName to place into the attribute.

This simple change won’t cause the client to experience an error; it will cause something even worse: an unexpected behavior. The error would be easy to track down, but tracking down the change in behavior is much more difficult.

This simple example demonstrates why it is critical to consider any service changes and their downstream effects. Unless you are personally aware of every client application utilizing your web service, changes can be disastrous. As a developer, you should do everything in your power to shield your clients from changes.

Initially, you can first apply a few best practices that will help isolate clients from internal changes. An updated version of the data contract might look like this:

[DataContract(Namespace="http://types.mycompany.com/2009/05/25", Name="PersonContract")]public class Person : IExtensibleDataObject{    private string _firstName = string.Empty;    private string _lastName = string.Empty;    private ExtensionDataObject _extensionData;    [DataMember(Name="FirstName")]    public string FirstName    {        get { return _firstName; }        set { _firstName = value; }    }    [DataMember(Name="LastName")]    public string LastName    {        get { return _lastName; }        set { _lastName = value; }    }    public ExtensionDataObject ExtensionData    {        get { return _extensionData; }        set { _extensionData = value; }    }}

The addition of the Namespace, Name, and Order parameters (you will see Order in an upcoming code example) on the DataContract and DataMember attributes controls the DataContractSerializer’s behavior. This addition generates the client proxy when a reference to the service is added. The Name parameters cause the serializer to use the indicated value as opposed to the name of the actual public member or property. This approach allows the internal implementation to change without affecting the client. For example, consider the following change:

[DataMember(Name="LastName")]    public string SurName    {        get { return _lastName; }        set { _lastName = value; }    }

The change of the property name from “LastName” to “SurName” will not break existing clients because the Name parameter that clients use is still “LastName.” Only the internal implementation has changed.

The second noticeable change is the addition of the IExtensibleDataObject interface. Implementing this interface allows a client to retain data that is not explicitly defined in the contract. This may not seem immediately useful, but in cases where the client is expected to perform processing on the sample Person object and return it, the client can retain new data items. For example, updating the PersonContract with the following new member will not force existing clients to be updated:

[DataMember(Name = "MiddleName", Order = 3)]public string SurName{    get { return _middleName; }    set { _middleName = value; }}

This member will, in fact, allow existing clients to retain a value placed in “MiddleName” by the service over the round trip. Implementing the IExtensibleDataObject is a useful way to future-proof your data contracts. As a best practice, you should use it on all data contracts.

Keep in mind that clients actually have the option of validating messages against an external schema. (For a thorough discussion of adding message schema validation to a WCF application, read this article.) As a result, you have two scenarios to consider when dealing with changes to data contracts: those with schema validation and those without schema validation.

When clients add schema validation, adding, changing, or subtracting any items in a data contract will cause that validation to fail. So, in cases where strict schema validation is being used, contracts simply should not be changed. Instead, you should create an entirely new contract and use a different namespace in the contract to indicate the new version.

For example, from an implementation perspective, you would need two separate service endpoints to make both these versions available:

Original Version: 
[DataContract(Namespace="http://schemas.mycompany.com/2009/05/25")]New Version:
[DataContract(Namespace="http://schemas.mycompany.com/2009/06/18")]

Fortunately, strict schema validation is not the default behavior. This means you can actually add and remove data members without breaking the client. However, because of the unexpected behavior previously discussed, removing a data member is not a good idea. On the other hand, adding a data member is easy to do, and consumers will ignore extra members that they are not aware of.

A key practice is to use the Order parameter of the DataMember attribute (as previously discussed). Using this parameter tells the serializer in what order each member should appear in the XML. An unintended change in the order can cause the XML to no longer conform to the original schema. Simply using the Order parameter from the beginning can avoid this problem. If you do not use the Order parameter, the serializer follows this order:

  1. Members from base types
  2. Members without an Order parameter (alphabetically)
  3. Members with an order parameter (in order by value)

The final scenario to consider with data contracts is changing the type of a data member. In this case, the best approach is to create a new version of the data contract along with a new service contract, implementation, and endpoint.

Service Contract Changes

Once again, all service contracts should follow the best practice of using both the Name and Namespace parameters on the ServiceContract attribute. An updated version of the Person service contract might look like this:

[ServiceContract(Name="PersonService", Namespace="http://services.mycompany.com/2009/05/25"]public interface IPersonService

As with the data contract, using the Name isolated the service consumer from the actual interface name, allowing the internal implementation to change as needed. The addition of the Namespace allows you to version the contract at some point in the future. Remember that new versions also require new endpoints.

You can add operations to service contracts without breaking existing consumers. Consumers will simply ignore the new operations. Removing operations, on the other hand, will break existing consumers. As with all breaking changes, the removal of an operation requires a new version along with a new endpoint.

Operation Contract Changes

As with the service and data contracts, you should use the Name parameter with the OperationContract attribute:

[OperationContract(Name="GetPerson"]Person GetPerson(int personId);

The consumer is, once again, isolated from changes in the internal implementation.

Finally, the last change to consider is in the signature of an operation contract. This is a breaking change for which you have two solutions: create a new version or add a new operation to the service contract.

Honor Your Commitments

Change is inevitable, but with a little planning and a few key principles, you can minimize the impact changes have on your WCF services. Always remember that when you release a service, you make a commitment to the service consumers to honor the contract. Making changes to an existing contract is bad form.

To that end, keep some best practices in mind:

  1. Always use the Name and Namespace parameters on all contracts.
  2. Always use the Order parameter on data members.
  3. Implement the IExtensibleDataObject on all data contracts.
  4. Use namespaces for contract versioning.
  5. Always remember that new versions require new endpoints.
  6. When using strict schema validation, do not change contracts. Create new versions.
  7. When removing an operation from a service contract, create a new version.
  8. When changing the signature of an operation, create a new version.

With these practices in mind, you can make change just a little easier to dealing with for yourself and your service consumers.

devx-admin

devx-admin

Share the Post:
Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India,

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in and explore the leaders in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India, and kickstart your journey to

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner for your online project. Your

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the state. A Senate committee meeting

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor supply chain and enhance its

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with minimal coding. These platforms not

Cybersecurity Strategy

Five Powerful Strategies to Bolster Your Cybersecurity

In today’s increasingly digital landscape, businesses of all sizes must prioritize cyber security measures to defend against potential dangers. Cyber security professionals suggest five simple technological strategies to help companies

Global Layoffs

Tech Layoffs Are Getting Worse Globally

Since the start of 2023, the global technology sector has experienced a significant rise in layoffs, with over 236,000 workers being let go by 1,019 tech firms, as per data

Huawei Electric Dazzle

Huawei Dazzles with Electric Vehicles and Wireless Earbuds

During a prominent unveiling event, Huawei, the Chinese telecommunications powerhouse, kept quiet about its enigmatic new 5G phone and alleged cutting-edge chip development. Instead, Huawei astounded the audience by presenting

Cybersecurity Banking Revolution

Digital Banking Needs Cybersecurity

The banking, financial, and insurance (BFSI) sectors are pioneers in digital transformation, using web applications and application programming interfaces (APIs) to provide seamless services to customers around the world. Rising

FinTech Leadership

Terry Clune’s Fintech Empire

Over the past 30 years, Terry Clune has built a remarkable business empire, with CluneTech at the helm. The CEO and Founder has successfully created eight fintech firms, attracting renowned

The Role Of AI Within A Web Design Agency?

In the digital age, the role of Artificial Intelligence (AI) in web design is rapidly evolving, transitioning from a futuristic concept to practical tools used in design, coding, content writing

Generative AI Revolution

Is Generative AI the Next Internet?

The increasing demand for Generative AI models has led to a surge in its adoption across diverse sectors, with healthcare, automotive, and financial services being among the top beneficiaries. These

Microsoft Laptop

The New Surface Laptop Studio 2 Is Nuts

The Surface Laptop Studio 2 is a dynamic and robust all-in-one laptop designed for creators and professionals alike. It features a 14.4″ touchscreen and a cutting-edge design that is over

5G Innovations

GPU-Accelerated 5G in Japan

NTT DOCOMO, a global telecommunications giant, is set to break new ground in the industry as it prepares to launch a GPU-accelerated 5G network in Japan. This innovative approach will

AI Ethics

AI Journalism: Balancing Integrity and Innovation

An op-ed, produced using Microsoft’s Bing Chat AI software, recently appeared in the St. Louis Post-Dispatch, discussing the potential concerns surrounding the employment of artificial intelligence (AI) in journalism. These

Savings Extravaganza

Big Deal Days Extravaganza

The highly awaited Big Deal Days event for October 2023 is nearly here, scheduled for the 10th and 11th. Similar to the previous year, this autumn sale has already created

Cisco Splunk Deal

Cisco Splunk Deal Sparks Tech Acquisition Frenzy

Cisco’s recent massive purchase of Splunk, an AI-powered cybersecurity firm, for $28 billion signals a potential boost in tech deals after a year of subdued mergers and acquisitions in the

Iran Drone Expansion

Iran’s Jet-Propelled Drone Reshapes Power Balance

Iran has recently unveiled a jet-propelled variant of its Shahed series drone, marking a significant advancement in the nation’s drone technology. The new drone is poised to reshape the regional

Solar Geoengineering

Did the Overshoot Commission Shoot Down Geoengineering?

The Overshoot Commission has recently released a comprehensive report that discusses the controversial topic of Solar Geoengineering, also known as Solar Radiation Modification (SRM). The Commission’s primary objective is to

Remote Learning

Revolutionizing Remote Learning for Success

School districts are preparing to reveal a substantial technological upgrade designed to significantly improve remote learning experiences for both educators and students amid the ongoing pandemic. This major investment, which

Revolutionary SABERS Transforming

SABERS Batteries Transforming Industries

Scientists John Connell and Yi Lin from NASA’s Solid-state Architecture Batteries for Enhanced Rechargeability and Safety (SABERS) project are working on experimental solid-state battery packs that could dramatically change the

Build a Website

How Much Does It Cost to Build a Website?

Are you wondering how much it costs to build a website? The approximated cost is based on several factors, including which add-ons and platforms you choose. For example, a self-hosted

Battery Investments

Battery Startups Attract Billion-Dollar Investments

In recent times, battery startups have experienced a significant boost in investments, with three businesses obtaining over $1 billion in funding within the last month. French company Verkor amassed $2.1