Introduction
I will demonstrate how to create the WCF service without
using interface. The general process to
create the WCF service is to create interface and implement it into the class. This
is best practice which is defined in the SOA model but similar to Web Service,
we can create the WCF without implementing the interface.
In this WCF demonstration, you will see that from the code and implementation
perspective, both web service and WCF are similar except WCF has the entire
configuration detail in web.config which has almost complete control on
runtime characteristics of a service without forcing a change in code.
Using the Code
In this demonstration, I use only one class and define WCF method inside this class. Also, instead of Interface name specifying the Class name in config file end point, I show the Service1.cs file code which has Service1
class and DisplayName
WCF method.
using System;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace WCFServiceWithOutUsingInterface
{
[ServiceContract ]
public class Service1
{
[OperationContract]
public string DisplayName(String value)
{
return string.Format("You Name is : {0}", value);
}
}
}
I am showing the Web.config file where instead of Interface name, I specify the Class name as Contract name in the end point .
="1.0"
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name ="WCFServiceWithOutUsingInterface" >
<endpoint contract ="Service1" binding="wsHttpBinding"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
Note about the Attached Code
WCFServiceWithOutUsingInterface.zip contains the WCFServiceWithOutUsingInterface.sln which has WCF service and consumes WCF service projects.