Web application calling WCF services
Below is a simple and straight forward tutorial on how you can get your web application to call a WCF service hosting on IIS6. I've broken the tutorial down into steps so as to faciliate a hands-on walkthrough.
Step 1
Create your WCF service
First of all, create your WCF service by specifying the interface as well as the implementor
//define the interface
public interface IWCFService
{
[OperationContract]
string myString(string input);
}
//implementation of interface
public class WCFService : IWCFService
{
public string myString(string input)
{
string output = input;
return output; //simply returning the input as output again
}
}
Step 2
Create your WCF service's web.config
You will need to create the web.config file for your WCF service to work. For WCF we can either dynamically programming these settings at code level or do it in the configuration. For this tutorial we'll do it in the configuration file.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<services>
<service
name="WCFService.WCFService"
behaviorConfiguration="WCFServiceBehavior">
<endpoint address=""
binding="wsDualHttpBinding"
contract="WCFService.IWCFService" />
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WCFServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors></system.serviceModel>
</configuration>
Step 3
Create your svc file
The svc file is required if you're hosting your service in IIS. This will not be required if you're hosting it in another application. The code base is however the same regardless of the chosen host method. Create a new file and give it a .svc extension. Paste the following code into the file
%@ServiceHost language=c# Debug="true" Service="WCFService.WCFService" %
Step 4
Host it in IIS
Now create a virtual folder in IIS and copy the compiled .dll into the bin directory, and copy the .svc file and web.config file into the virtual directory. Remember to change the .Net framework into 2.0.
Step 5
Create the web application client.
Now you can start to create your web application. Create a simple web application. Go to Windows SDK cmd and execute the following command
svcutil.exe http://localhost/wcfservice/interface.svc /out: proxy.cs
This will create the proxy and web.config file that is required for your web application to call the WCF service hosted on http://localhost/wcfservice/interface.svc
Step 6
Write the code to call the WCF service in your webpage
Open your default.aspx, copy the proxy.cs files into the App_Code folder and call the WCF service
using (WCFServiceClient client = new WCFServiceClient())
{
string output = client.myString("myinput");
}
Step 7
Merge the generated .config file into the web application web.config file.
Now simply merge the auto generated configuration file into the web application's web.config file.
<system.serviceModel>
<bindings>
<wsDualHttpBinding>
<binding name="WSDualHttpBinding_IOperationalService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
<reliableSession ordered="true" inactivityTimeout="00:10:00"/>
<security mode="Message">
<message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default"/>
</security>
</binding>
</wsDualHttpBinding>
</bindings><client>
<endpoint address="http://dms_poc/operationalservice/interface.svc" binding="wsDualHttpBinding" bindingConfiguration="WSDualHttpBinding_IOperationalService" contract="OperationalService_Common.IOperationalService" name="WSDualHttpBinding_IOperationalService">
<identity>
<servicePrincipalName value="host/dms_poc"/>
</identity>
</endpoint>
</client>
</system.serviceModel>
Step 8
Test your application.
Test the application by adding any simple text control which will show the return results and a command button to trigger the call.
I hope this simple tutorial will come across as useful to you.