How to Make Changes to WCF Service Without Breaking Client in WCF: Part 3

Before reading this article, I highly recommend reading the previous parts:
Let's 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
  1. using System.ServiceModel;  
  2.   
  3. namespace myFirstApp  
  4. {  
  5.     [ServiceContract]  
  6.     public interface IMyService  
  7.     {  
  8.         [OperationContract]  
  9.         int AddTwoNo(int intFirstNo, int intSecondNo);  
  10.     }  
  11. }  
In MyService.svc.cs
  1. namespace myFirstApp  
  2. {  
  3.     public class MyService : IMyService  
  4.     {  
  5.         public int AddTwoNo(int intFirstNo, int intSecondNo)  
  6.         {  
  7.             return intFirstNo + intSecondNo;  
  8.         }  
  9.     }  
  10. }  
In web.config 
  1. <?xml version="1.0"?>  
  2. <configuration>  
  3.   <appSettings>  
  4.     <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />  
  5.   </appSettings>  
  6.   <system.web>  
  7.     <compilation debug="true" targetFramework="4.5" />  
  8.     <httpRuntime targetFramework="4.5"/>  
  9.   </system.web>  
  10.   <system.serviceModel>  
  11.     <behaviors>  
  12.       <serviceBehaviors>  
  13.         <behavior>  
  14.           <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>  
  15.           <serviceDebug includeExceptionDetailInFaults="false"/>  
  16.         </behavior>  
  17.       </serviceBehaviors>  
  18.     </behaviors>  
  19.     <protocolMapping>  
  20.         <add binding="basicHttpsBinding" scheme="https" />  
  21.     </protocolMapping>      
  22.     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />  
  23.   </system.serviceModel>  
  24.   <system.webServer>  
  25.     <modules runAllManagedModulesForAllRequests="true"/>  
  26.     <directoryBrowse enabled="true"/>  
  27.   </system.webServer>  
  28. </configuration>  
This is the default configuration of web.config.
 
Let's create a new website and name it WCFClientApp and add the service reference.
 
In default.aspx
  1. <table>  
  2.         <tr><td>First No</td><td><asp:TextBox ID="txtFirst" runat="server"></asp:TextBox></td></tr>  
  3.         <tr><td>Second No</td><td><asp:TextBox ID="txtSec" runat="server"></asp:TextBox></td></tr>  
  4.         <tr><td colspan="2"><asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" /></td>  
  5.              
  6.         </tr>  
  7.         <tr><td colspan="2"><asp:Label ID="lblResult" runat="server"></asp:Label></td></tr>  
  8.     </table>  
In default.aspx.cs
  1. protected void btnAdd_Click(object sender, EventArgs e)  
  2.         {  
  3.             int intFirstNo = 0, intSecNo = 0, intResult = 0;  
  4.             intFirstNo = Convert.ToInt16(txtFirst.Text);  
  5.             intSecNo = Convert.ToInt16(txtSec.Text);  
  6.             WCFReference.MyServiceClient client = new WCFReference.MyServiceClient();  
  7.             intResult = client.AddTwoNo(intFirstNo, intSecNo);  
  8.             lblResult.Text = "Result is :"+intResult;  
  9.             client.Close();  
  10.         }  
Let's run the myFirstApp first and then WCFClientApp.
 
 
 
After filling in the data click on the add button and you will get an error screen.
 
 
 
We can resolve this issue in one of the following two ways:
  1. Declaring an endpoint
  2. Adding an AddressFilterMode in ServiceBehavior
As of now I am using the second method. Let's add the following code above the MyService class in MyService.svc.cs and run the application. We will talk later about ServiceBehavior in WCF. ServiceBehavior is present in the System.ServiceModel namespace.
  1. [ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)]  
Output
 
 
 
The result is as expected. Next we will change the interface name IMyService to ImyServiceNew. Let's see what happens.
 
 
 
This would break the existing clients. How to prevent this? We can prevent this by using the Name property of ServiceContractAttribute.
 
In IMyService.cs
  1. [ServiceContract(Name="IMyService")]  
  2. public interface IMyServiceNew  
  3.     {  
  4.         [OperationContract]  
  5.         int AddTwoNo(int intFirstNo, int intSecondNo);  
  6.     }  
Run the application and see the output. It works as expected.
 
I hope this article is helpful for you.
 
Thanks :)
 


Similar Articles