Today, in this article we will try to perform simple arithmetic operation
importing some created service into our web application. Once the application is
fully developed we can request values from our web form which retrieve the
output by performing an expected operation to the values specified by the
service.
The Complete Code for created IService1.cs looks like this:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Runtime.Serialization;
using
System.ServiceModel;
using
System.ServiceModel.Web;
using
System.Text;
namespace
Simple_WCF
{
// NOTE: You can use the "Rename" command on the "Refactor"
menu to change the interface name "IService1" in both code and config file
together.
[ServiceContract]
public interface
IService1
{
[OperationContract]
int Add(int
x, int y);
[OperationContract]
int Sub(int
x, int y);
[OperationContract]
int Mul(int
x, int y);
[OperationContract]
int Div(int
x, int y);
}
}
The Complete Code for created Service1.svc looks like this: It implements
IService1.cs:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Runtime.Serialization;
using
System.ServiceModel;
using
System.ServiceModel.Web;
using
System.Text;
namespace
Simple_WCF
{
// NOTE: You can use the "Rename" command on the "Refactor"
menu to change the class name "Service1" in code, svc and config file together.
public class
Service1 : IService1
{
public int Add(int
x, int y)
{
return x + y;
}
public int Sub(int
x, int y)
{
return x - y;
}
public int Mul(int
x, int y)
{
return x * y;
}
public int Div(int
x, int y)
{
return x / y;
}
}
}
The Complete Code for Created Web.Config looks like this:
We are creating a
simple endpoint with some service related information:
<?xml
version="1.0"?>
<configuration>
<system.web>
<compilation
debug="true"
targetFramework="4.0"
/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior
name="Application">
<!--
To avoid disclosing metadata information, set the value below to false and
remove the metadata endpoint above before deployment
-->
<serviceMetadata
httpGetEnabled="true"/>
<!--
To receive exception details in faults for debugging purposes, set the value
below to true. Set to false before deployment to avoid disclosing exception
information
-->
<serviceDebug
includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment
multipleSiteBindingsEnabled="true"
/>
<services>
<service
name
="Simple_WCF.Service1"
behaviorConfiguration="Application">
<endpoint
address="/MyAddress"
binding="basicHttpBinding"
contract="Simple_WCF.IService1">
</endpoint>
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange">
</endpoint>
<host>
<baseAddresses
>
<add
baseAddress="http://localhost:51861/Service1.svc"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
<system.webServer>
<modules
runAllManagedModulesForAllRequests="true"/>
</system.webServer>






Dorababu MekaPosted Dec 13, 2011, 7:32 AM
Hi good example. Can you attach the code please