break
Jul 17

This basically means that you must now explicitly configure metadata endpoints for your service by adding the ServiceMetadataBehavior.

This is accomplished in 2 steps:

1- First add a behavior configuration to your <service> . For example

<service name=”WCFServiceLibrary2.service1″ behaviorConfiguration=”MyServiceTypeBehaviors”>

2- Add a behavior named “MyServiceTypeBehaviors” and add a <ServiceMetadata>. For example

<behaviors>
<serviceBehaviors>
<behavior name=”MyServiceTypeBehaviors” >
<serviceMetadata httpGetEnabled=”true” />
</behavior>
</serviceBehaviors>
</behaviors>

Your final configuration file should look like this:

<configuration>
<system.serviceModel>
<services>
<service name=”WCFServiceLibrary2.service1″ behaviorConfiguration=”MyServiceTypeBehaviors”>
<endpoint contract=”WCFServiceLibrary2.IService1″ binding=”wsHttpBinding”/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name=”MyServiceTypeBehaviors” >
<serviceMetadata httpGetEnabled=”true” />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

May 28

When I had a WCF Service, the next step was to host the WCF Service. I was amazed when I noticed that the Class1.cs file (created by visual studio while I was trying to create a WCF Service Library project) has instructions of how to host a WCF service.

They suggest this can be done in 9 steps:
1- Create a Host project. I personally added a new project to the solution that had the WCF service. To do this you right click the solution, select add, select new project, and then I choose a Console Application project.

2- Add to your host project a CodeFile, which is a blank C# file.
3- In this blank codefile you need to pase a code they provide in the Class1.cs file which is the following:

using System;
using System.ServiceModel;
internal class MyServiceHost
{
internal static ServiceHost myServiceHost = null;
internal static void StartService()
{
//Consider putting the baseAddress in the configuration system
//and getting it here with AppSettings
Uri baseAddress = new Uri("http://localhost:8080/service1");
//Instantiate new ServiceHost
myServiceHost = new ServiceHost(typeof(WCFServiceLibraryExample.service1), baseAddress);
//Open myServiceHost
myServiceHost.Open();
}
internal static void StopService()
{
//Call StopService from your shutdown logic (i.e. dispose method)
if (myServiceHost.State != CommunicationState.Closed)
myServiceHost.Close();
}
}

If you notice this internal class has an instantiated object of the ServiceHost class set to null. A ServiceHost class according to MSDN “implements the host used by the Windows Communication Foundation (WCF) service model programming model”. This instantiation of the class (myServiceHost) will help us to start and stop our service. Notice that the open and close of the service happens inside the methods StartService and StopService. This methods will be extremely helpful when we will attempt to write a little console application that can start and stop our service.

4-At this point we need to add an Application Configuration File to our host project.
5-In this blank App.config file you need to pase a code they provide in the Class1.cs file which is the following:

<system.servicemodel>
<services>
<service name=”WCFServiceLibraryExample.service1″>
<endpoint contract=”WCFServiceLibraryExample.IService1″ binding=”wsHttpBinding”>
</endpoint>
</service>
</services>
</system.servicemodel>

The tag “system.servicemodel” you could think of it as the tag you use to start connecting web service. In the “services” tag you can specify a specific “service” which you need to give a name with the name attribute of the service tag. Inside the “service” tag you specify the “endpoint” and the contract attribute of the endpoint tag. The contract attribute (which is what the endpoint is going to communicate to the outside world about our service) will be the interface that I had in my example “WCF: Creating a WCF Service”.
6- After this you want to add the code that will host, start and stop the service. This is my code for this step:

using System;
using System.Collections.Generic;
using System.Text;
namespace WCFConsoleService
{
class Program
{
static void Main(string[] args)
{
MyServiceHost.StartService();
Console.WriteLine("To stop the service type 'e'");
string exit = Console.ReadLine();
while (!exit.Equals("e"))
{
Console.WriteLine("To stop the service type 'e'");
exit = Console.ReadLine();
}
MyServiceHost.StopService();
}
}
}

I basically start the service, and wait for the user to type the character ‘e’ to stop the service.
7- After this you need to add a Reference to System.ServiceModel.dll in your host project. This is required because in your codefile the namespace System.ServiceModel is used. To do this right click on your host project, select add reference,select “System.ServiceModel.dll” in the .NET tab.
8- Now you need to add a Reference from your Host project to your Service Library project. To do this right click on your host project, select add reference, and in the projects tab select your wcf service project.
9-Set the host project as the “StartUp” project for the solution.

After this step you are ready to host your WCF Service. You can press F5 in Visual Studio and you should see a console with the following message:

This means your wcf service is running and you can stop it by pressing ‘e’. If you go to http://localhost:8080/service1 you should see the following:

Congratulations, you are now hosting a WCF Service.

May 28

I was trying to create a WCF Service Library in order to get more familiar with WCF. When I created a WCF Service Library project, a Class1.cs file was created.

This file has an interface and a public class that implements the interface. Basically just by creating a WCF Service Library project you get a WCF Service example. What’s good about this example, is that if you are new to this, you can use this example as a guideline to create your own WCF Service. I’m going to explain the file created by visual studio.

Class1.cs
using System.ServiceModel;
[ServiceContract()]
public interface IService1
{
[OperationContract]
string MyOperation1(string myValue);
[OperationContract]
string MyOperation2(DataContract1 dataContractValue);
}

If you noticed before the interface it says [ServiceContract()]. According to MSDN this is “a collection of Operations that specifies what the Endpoint” (when 2 entities are transferring data, the endpoint is the name for an entity on one end of the transfer ) “communicates to the outside world. Each operation is a simple message exchange.”

So basically the outside world is only going to be able to communicate with the methods MyOperation1 and MyOperation2 in this WCF Service. This methods need to be annotated with the attribute [OperationContract]. This is because they are methods of the service.

The implementation of the interface is

public class service1 : IService1
{
public string MyOperation1(string myValue)
{
return "Hello: " + myValue;
}
public string MyOperation2(DataContract1 dataContractValue)
{
return "Hello: " + dataContractValue.FirstName;
}
}

This class is very straightforward, it inherits an interface, and implements the methods of the interface. However if you notice the method MyOperation2 receives a DataContract1 parameter. This is a class this file created by visual studio also provides. I’ll show the code of this class and explain some interesting things about the class.

[DataContract]
public class DataContract1
{
string firstName;
string lastName;
[DataMember]
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
[DataMember]
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
}

Before the class definition there is something like this [DataContract]. According to MSDN “a data contract is an abstract description of a set of fields with a name and data type for each field”. In this case the set of fields of the class will be firstName and lastName, the data type will be string. In this class only properties that include [DataMember] will be accessed.