By default, IIS supports only HTTP Protocol only. If you want to support NON-HTTP Protocols like TCP, then you need to install "Windows Communication Foundation Non-HTTP Protocol". To do that, you have to follow below steps:
- First go to Control Panel.
- Click on Programs and Features.
- Click on Turn Windows Features Off.
- Expand a window like "Microsoft. NET Framework 3.5.1".
- In that, look for an option like "Windows Communication Foundation Non-HTTP Protocol" and Click on OK.
Below screen shows the process in detail.
After clicking on
OK, it will take several minutes to install the features.
And, next step is to enable the Non-HTTP Protocols, for that to open IIS (Internet Information Service). After opening the ISS, following screens appears. Below screens shows how to add Non-HTTP Protocols.
After clicking on OK button in above screen, all the settings are affected to config file automatically.
Write a code in
MathCal.svc file like below code:
[csharp]using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WCFIISHOSTING
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "MathCal" in code, svc and config file together.
public class MathCal : IMathCal
{
public int additin(int x, int y)
{
return x + y;
}
public int Cube(int x)
{
return Factorial(x) * x;
}
public int Square(int x)
{
return x * x;
}
public int Factorial(int x)
{
int r = 1;
for (int i = 1; i <= x; i++)
{
r = r * 1;
return r;
}
return x * x;
}
}
}
[/csharp]
And, write a code in IMathcal.cs for implementing the service.
[csharp]using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WCFIISHOSTING
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IMathCal" in both code and config file together.
[ServiceContract]
public interface IMathCal
{
[OperationContract]
int additin(int x, int y);
[OperationContract]
int Square(int x);
[OperationContract]
int Cube(int x);
[OperationContract]
int Factorial(int x);
}
}
[/csharp]
In the web.Config file, create an endpoint with '
netTcpBinding'. The service metadata will be published using the Metadata Exchange point. So create the Metadata Exchange endpoint with the address as '
mex' and the binding as '
mexTcpBinding'. Like below:
[csharp]
<configuration> <system.serviceModel>
<services>
<service behaviorConfiguration="WCFIISHOSTING.Service1Behavior" name="WCFIISHOSTING.Service1">
<endpoint address="" binding="wsHttpBinding" contract="WCFIISHOSTING.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
<service behaviorConfiguration="WCFIISHOSTING.MathCalBehavior" name="WCFIISHOSTING.MathCal">
<endpoint address="" binding="wsHttpBinding" contract="WCFIISHOSTING.IMathCal">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WCFIISHOSTING.Service1Behavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior name="WCFIISHOSTING.MathCalBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
[/csharp]
After wirting all the code, move to IIS and browse the service as shown in below screen. Now, service has been implemented successfully and one endpoint address is obtained.
By using that end point address, client can consume the service.