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
- 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 the default configuration of web.config.
Let's 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();
- }
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:
- Declaring an endpoint
- 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.
- [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.
- [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 :)