- Create a new Console Application project in a new solution. This project will
contain a WCF service, its contract and a host (like in the
previous example).
- Add a reference to the System.ServiceModel assembly.
- Add an interface (named IMySecondService) serving as a contract
for the service:
[ServiceContract]
public
interface
IMySecondService
{
[OperationContract]
int
GetNumberWords(string
s);
}
- Add a class (named MySecondService) with implementation of the
IMySecondService interface:
public
class
MySecondService
: IMySecondService
{
public
int
GetNumberWords(string
s)
{
return
s.Split(
new
char[]
{ ' ',
';',
',',
'.',
'?'
},
StringSplitOptions.RemoveEmptyEntries).Length;
}
}
- Add to the Main method code running the service's host:
static
void
Main(string[]
args)
{
ServiceHost
serviceHost = new
ServiceHost(typeof(MySecondService));
serviceHost.Open();
Console.WriteLine("This
console application hosts the MySecondService WCF service");
Console.WriteLine("Press
ENTER to finish hosting");
Console.ReadLine();
serviceHost.Close();
}
- Note differences between the code presented above and
the code from the previous example. In this code there
are no settings (in particular, no endpoint is defined), all
necessary settings will be placed in a configuration file.
- Add an Application Configuration File item to the project:

- Add the following content to the configuration file:
<?xml
version="1.0"
encoding="utf-8"
?>
<configuration>
<system.serviceModel>
<services>
<service
name="SecondService.MySecondService">
<host>
<baseAddresses>
<add
baseAddress="http://localhost:1235/MySecondService"/>
</baseAddresses>
</host>
<endpoint
address=""
binding="basicHttpBinding"
contract="SecondService.IMySecondService"
/>
</service>
</services>
</system.serviceModel>
</configuration>
- The name attribute of the service tag is a name (including
a namespace) of the service's class.
- The contract attribute of the endpoint tag is a name of the service's contract, i.e. the service's interface.
- The baseAddress contains the URL of the service. It must
be consistent with the binding set for the endpoint. In this
example, HTTP binding and address are used.
- Run the console application. While it is running, open address of the service (http://localhost:1235/MySecondService) in a
browser :

- Now it is time to create a client.
- Add new Console Application project to the solution.
- Run the service and while it is running, try to add a service
reference to the client's project. The try will fail:

- The reason of the failure is that by default WCF
services do not expose a MEX endpoint. An MEX endpoint allows a
client to discover how to communicate with the service.
- To add a MEX endpoint to the service, modify its configuration
by adding a new special endpoint and so-called 'behavior' to allow to access
the MEX endpoint via HTTP.
<?xml
version="1.0"
encoding="utf-8"
?>
<configuration>
<system.serviceModel>
<services>
<service
name="SecondService.MySecondService"
behaviorConfiguration="mySecondServiceBehavior">
<host>
<baseAddresses>
<add
baseAddress="http://localhost:1235/MySecondService"/>
</baseAddresses>
</host>
<endpoint
address=""
binding="basicHttpBinding"
contract="SecondService.IMySecondService"
/>
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"
/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior
name="mySecondServiceBehavior">
<serviceMetadata
httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
- Run the service and look at the page generated for it:

- The difference is very important - the WSDL document
describing the service is available now.
- Run the service and while it is running, try to add a service
reference to the client's project. Now, it works:


- Calling the service's method from the client is very simple now:
ServiceReference1.MySecondServiceClient
client = new
ServiceReference1.MySecondServiceClient();
int
numberWords = client.GetNumberWords("Sample
text for the service");
- Note the configuration file generated automatically for the
client:
<?xml
version="1.0"
encoding="utf-8"
?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding
name="BasicHttpBinding_IMySecondService"
closeTimeout="00:01:00"
openTimeout="00:01:00"
receiveTimeout="00:10:00"
sendTimeout="00:01:00"
allowCookies="false"
bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536"
maxBufferPoolSize="524288"
maxReceivedMessageSize="65536"
messageEncoding="Text"
textEncoding="utf-8"
transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas
maxDepth="32"
maxStringContentLength="8192"
maxArrayLength="16384"
maxBytesPerRead="4096"
maxNameTableCharCount="16384"
/>
<security
mode="None">
<transport
clientCredentialType="None"
proxyCredentialType="None"
realm=""
/>
<message
clientCredentialType="UserName"
algorithmSuite="Default"
/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint
address="http://localhost:1235/MySecondService"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IMySecondService"
contract="ServiceReference1.IMySecondService"
name="BasicHttpBinding_IMySecondService"
/>
</client>
</system.serviceModel>
</configuration>
- Note also the 'Service References\ServiceReference1' directory
created in the project's directory and its content:
