In this article we will walkthrough on creating a WCF Service by choosing WCF Service Library project template.
Step 1
Create a WCF Service Library project.
Delete all the default codes created by WCF.
Step 2
Modify Operation Contract as below,
Implement the service as below
Step 3
Leave App.config file as it is with EndPoint with wsHttpBinding .
If needed, modify default port number to some other port to avoid getting run time exception while testing the service in WCF Service client.
Step 4
Now run the WCF service library. Just press F5. In WCF Test client service will be running.
Now we can see our service is tested and running in WCF Test client.
Step 5
Add a Web Application to the same solution. We will be hosting the WCF Service library to this web application.
Make web application project as startup project. To do so right click and make the project as start up project.
Step 6
Right click on the web application project and add reference of WCF Service library project , we created in step 1.
Step 7
Now right click on the Web Application project and from general tab add a text file. Give any name to the text file but make sure extension is .svc.
Add the below code in newly added .svc file.
In above code WcfServiceLibrary2 is the namespace of the service we created in step 1.
Step 8
Now we need to modify the Web.Config file of web application project and add the System.serviceModel .
We need to enable the Metadata exchange end point. So add below markup
Now add the service behavior to enable meta data exchange endpoint
And now add the service configuration behavior to the service. After adding that System.serviceModel will look like below
For reference config file will look like below,
Web.Config
<?xml version="1.0"?> <!-- For more information on how to configure your ASP.NET application, please visit <a href="http://go.microsoft.com/fwlink/?LinkId=169433">http://go.microsoft.com/fwlink/?LinkId=169433</a> --> <configuration> <connectionStrings> <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" /> </connectionStrings> <system.web> <compilation debug="true" targetFramework="4.0" /> <authentication mode="Forms"> <forms loginUrl="~/Account/Login.aspx" timeout="2880" /> </authentication> <membership> <providers> <clear/> <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" /> </providers> </membership> <profile> <providers> <clear/> <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/> </providers> </profile> <roleManager enabled="false"> <providers> <clear/> <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" /> <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" /> </providers> </roleManager> </system.web> <system.serviceModel> <services> <service name ="WcfServiceLibrary2.Service1" behaviorConfiguration="WcfServiceLibrary2.Service1Behavior"> <endpoint address ="" binding ="wsHttpBinding" contract ="WcfServiceLibrary2.IService1" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="WcfServiceLibrary2.Service1Behavior"> <serviceMetadata httpGetEnabled="True"/> <serviceDebug includeExceptionDetailInFaults="False" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration>
Press F5 to run the service
Step 9
Now we will create a client to call the service. Right click on the same solution and add console project. Make console application as start project.
Add the service reference
Now call the service as below,
Programs.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using ConsoleApplication1.ServiceReference1; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Service1Client proxy = new Service1Client(); var r = proxy.GetMessage(); Console.WriteLine(r); Console.ReadKey(true); } } }
On running we will get the expected output as below,
Leave a Reply