How to make changes to wcf service without breaking client – WCF Part 3

Lets create a wcf application and name it myFirstApp. Please read my previous article Introduction to WCF, if you are new to wcf. I am using visual studio 2012. I am using my previous demo application.
In IMyService.cs

using System.ServiceModel;
namespace myFirstApp
public interface IMyService
int AddTwoNo(int intFirstNo, int intSecondNo);

In MyService.svc.cs

namespace myFirstApp
public class MyService : IMyService
public int AddTwoNo(int intFirstNo, int intSecondNo)
return intFirstNo + intSecondNo;

In web.config

<?xml version="1.0"?>
 <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
 <compilation debug="true" targetFramework="4.5" />
 <httpRuntime targetFramework="4.5"/>
 <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
 <serviceDebug includeExceptionDetailInFaults="false"/>
 <add binding="basicHttpsBinding" scheme="https" />
 <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
 <modules runAllManagedModulesForAllRequests="true"/>
 <directoryBrowse enabled="true"/>

This is default configuration of web.config.
Lets create a new website and name it WCFClientApp and add the service reference.
In default.aspx

<tr><td>First No</td><td><asp:TextBox ID="txtFirst" runat="server"></asp:TextBox></td></tr>
<tr><td>Second No</td><td><asp:TextBox ID="txtSec" runat="server"></asp:TextBox></td></tr>
<tr><td colspan="2"><asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" /></td>
<tr><td colspan="2"><asp:Label ID="lblResult" runat="server"></asp:Label></td></tr>

In default.aspx.cs

protected void btnAdd_Click(object sender, EventArgs e)
int intFirstNo = 0, intSecNo = 0, intResult = 0;
 intFirstNo = Convert.ToInt16(txtFirst.Text);
 intSecNo = Convert.ToInt16(txtSec.Text);
 WCFReference.MyServiceClient client = new WCFReference.MyServiceClient();
 intResult = client.AddTwoNo(intFirstNo, intSecNo);
 lblResult.Text = "Result is :"+intResult;

Lets run the myFirstApp first and after that WCFClientApp.

After filling data click on add button and I got an error screen.

This error say that endpoint is missing for this service. If you are new to endpoint please read my previous article on endpoint.
We can resolve this issue by two methods.

  1. Declaring endpoint
  2. Adding AddressFilterMode in ServiceBehavior

As of now I am using second method
Lets add the following code above MyService class in MyService.svc.cs and run the application. We will talk later on ServiceBehavior in wcf. ServiceBehavior is present in System.ServiceModel namespace.

Result as expected.
Now later if we changed the interface name IMyService to ImyServiceNew. Lets see what happen.

This would break the existing clients. Then how to prevent this. We can prevent this by using Name property of ServiceContractAttribute.
In IMyService.cs

public interface IMyServiceNew
int AddTwoNo(int intFirstNo, int intSecondNo);

Run the application and see the output. It works as expected.

I hope this article is helpful for you.

Thanks :)

Tags: , , ,