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
{
 [ServiceContract]
public interface IMyService
 {
 [OperationContract]
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"?>
<configuration>
 <appSettings>
 <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
 </appSettings>
 <system.web>
 <compilation debug="true" targetFramework="4.5" />
 <httpRuntime targetFramework="4.5"/>
 </system.web>
 <system.serviceModel>
 <behaviors>
 <serviceBehaviors>
 <behavior>
 <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
 <serviceDebug includeExceptionDetailInFaults="false"/>
 </behavior>
 </serviceBehaviors>
 </behaviors>
 <protocolMapping>
 <add binding="basicHttpsBinding" scheme="https" />
 </protocolMapping> 
 <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
 </system.serviceModel>
 <system.webServer>
 <modules runAllManagedModulesForAllRequests="true"/>
 <directoryBrowse enabled="true"/>
 </system.webServer>
</configuration>

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

<table>
<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>
<tr><td colspan="2"><asp:Label ID="lblResult" runat="server"></asp:Label></td></tr>
</table>

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;
 client.Close();
 }

Lets run the myFirstApp first and after that WCFClientApp.

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

1
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.
[ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)]
Output

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

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

[ServiceContract(Name="IMyService")]
public interface IMyServiceNew
 {
 [OperationContract]
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: , , ,