Develop and Consume OData Web Services in WCF

What is OData?

OData is an Open Data Protocol used to access data from various sources like Relational databases, file systems, etc. and exposes it to the consumers that can query or update data. OData is based upon Web technologies such as HTTP, Atom Publishing Protocol (AtomPub) and JSON to provide access to information from variety of data sources. Consumers can query the data using HTTP protocol and get the results in AtomPub and JSON formats. These are the formats through which the service can deliver the requested resources or collection of entities.

OData is released under the Open Specification Promise http://www.microsoft.com/openspecifications/en/us/programs/osp/default.aspx and anyone can freely interoperate with OData implementations.

Developing WCF Service using OData Protocol

Microsoft has first introduced OData support in Visual Studio 2008 SP1 in terms of ADO.Net Data Services (subsequently known as WCF Data Services). Developers can create services on the fly and exposes data using Entity Framework or LINQ 2 SQL ORM tools.

Let’s create a simple WCF Data Service and expose data using Entity Framework.

  1. Start Visual Studio 2010 and create a new WCF Service Application project.


  1. Right click the WCF Service Application project and click on the “Add New Item”, then select WCF Data Service.


  2. If you open the WCF Data Service class file you can see the Class will be derived from the generic DataService class. This is the main entry point for developing WCF Data services. We have to specify the data source class name. In order to proceed further I will add entity framework model which creates the entities model on the fly.
  3. Right click on the Service Project and Add “ADO.Net Entity Data Model” from the dialog.


    When you add ADO.Net Entity Model it will ask you to select the data source, go through the wizard by selecting the data source and finish a dialog.

  4. Now open the .edmx file click on properties.


    Here’s the entity container named as “DDMSEntities”. Just specify the name DDMSEntities in your WCF Data Service base class like below

public class WcfDataService1 : DataService<DDMSEntities>

  1. You can now add the SetEntitySetAccessRule for each entity you want to expose. Default configuration is to deny access to resources. When a service is initialized the access rights must be enabled.

    Sample code snippet below


public static void InitializeService(DataServiceConfiguration config)
{

// TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.

config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;

config.SetEntitySetAccessRule (“Levels”, EntitySetRights.AllRead);

config.SetEntitySetAccessRule (“NodeTypes”, EntitySetRights.All);

}

In Above code, I have added two tables i.e. Levels and NodeTypes and given permissions as AllRead. There are many permission levels you can set as follows. Below are the enums of EntitySetRights with their description

  • All – Authorize to create, read, update and delete data.
  • AllRead- Authorize to read data.
  • AllWrite- Authorize to write data.
  • None- Denies all rights to access data.
  • ReadMultiple – Authorize to read sets of data.
  • ReadSingle – Authorize to read single data of items.
  • WriteAppend- Authorize to create new data items in the data sets.
  • WriteDelete –Authorize to delete data items from the data sets.
  • WriteMerge – Authorize to merge data.
  • WriteReplace – Authorize to replace data.
  1. Now let’s build a service and test it. Once you hit the web service in a browser, you will see the page as below


    The above feed representing the data as per Atom Syndication format. There are two entity collections namely “Levels” and “NodeTypes”.

                                      http://localhost:32555/WcfDataService1.svc/Levels?filter= LevelTree gt

                                     “gt” is used for greater than where as “lt” is used for less than.

Consuming WCF Data Service in Web Application

  1. Create a new Web Application project in Visual Studio
  2. Add a service reference by right clicking on Add Service Reference and select the newly service created.


  3. Add a button to call the service on its click event.
  4. On the click event you can consume the service like below

Services.DMSEntities.DDMSEntities entities = new Services.DMSEntities.DDMSEntities(new
Uri(http://localhost:32555/WcfDataService1.svc&#8221;));

var lst = entities.Levels.FirstOrDefault();

This will return the first item in the collection. You can execute all CRUD operations depending on the rule set in the Service for that entity.

Hope this helps, happy coding!

Logging Messages from Windows Service to Windows Form using WCF Duplex Messaging

As you all know windows services run in background and user can start or configure them through Services panel in Windows Operating System. In order to do the logging to check what the service is doing you can log messages in database, message queues, event logs etc. But in order to send back and forth messages to any windows application to show real time task execution is only possible via TCP socket connection, remoting etc.

In this post I will show you the way of logging messages from Windows Services to Windows form based Monitor application using WCF Duplex Messaging.

Step 1: Creating WCF Service Library

  1. Create a WCF Service Library project from Visual Studio
  2. Add a Service Contract interface named as “IMonitorService”

[ServiceContract(

Name = “IMonitorService”,

SessionMode = SessionMode.Required,

CallbackContract = typeof(IMonitorServiceCallback))]


public
interface
IMonitorService

{

[OperationContract]


void Login();

[OperationContract]


void Logout();

[OperationContract]


void LogMessages(String message);

}

    In the above code, I have created an interface and provided three methods signature Login, Logout and LogMessages. Login and Logout will be used to connect or disconnect to the Service and LogMessages is used to log messages to client. You will notice that in the ServiceContract attribute I have specified CallbackContract which actually holds the type information of the Call back contract interface through which when the client invoke any of the login, logout or logmessages server can get the callbackcontract object and sends the response back to client itself.

  1. Here is the code for IMonitorServiceCallback contract interface.


public
interface
IMonitorServiceCallback

{

[OperationContract]


void NotifyClient(String message);

}

    In the above code there is a callback contract interface which holds one method signature “NotifyClient” which takes string as a parameter. Server can call NotifyClient method and send messages to the connected clients.

  1. Now create another class MonitorService and implement IMonitorService interface. Following code shows the complete

    code shows the complete implementation.

[ServiceBehavior(

ConcurrencyMode = ConcurrencyMode.Reentrant,

InstanceContextMode = InstanceContextMode.PerCall)]


public
class
MonitorService : IMonitorService

{


public
static
List<IMonitorServiceCallback> callBackList = new
List<IMonitorServiceCallback>();


public MonitorService()

{

}


public
void Login()

{


IMonitorServiceCallback callback = OperationContext.Current.GetCallbackChannel<IMonitorServiceCallback>();


if (!callBackList.Contains(callback))

{

callBackList.Add(callback);

}

}


public
void Logout()

{


IMonitorServiceCallback callback = OperationContext.Current.GetCallbackChannel<IMonitorServiceCallback>();


if (callBackList.Contains(callback))

{

callBackList.Remove(callback);

}

callback.NotifyClient(“You are Logged out”);

}


public
void LogMessages(string message)

{


foreach (IMonitorServiceCallback callback in callBackList)

{

callback.NotifyClient(message);

}

}

The above code shows the implementation of the IMonitorService interface.

Step 2: Create Windows Service project and use WCF Service Library

  1. Create a new “Windows Service” project using Visual Studio.
  2. In the Start method write some code to let service app do some work.
  3. Add project reference of the WCF Service Application
  4. Initialize the ServiceHost object of WCF framework

    ServiceHost host = new
    ServiceHost(typeof(MonitorService));

host.Open();

  1. Implement the LogMessage method and notify callback contracts.


foreach (IMonitorServiceCallback callback in
MonitorService.callBackList)

{

callback.NotifyClient(message);

}

  1. App.config for Windows Service

<?xml
version=1.0
encoding=utf-8 ?>

<configuration>

<system.web>

<compilation
debug=true />

</system.web>

<system.serviceModel>

<bindings>

<netTcpBinding>

<binding
name=DefaultNetTCPBinding
receiveTimeout=Infinite>

<reliableSession
inactivityTimeout=Infinite />

</binding>

</netTcpBinding>

</bindings>

<services>

<service
name=MonitorService>

<host>

<baseAddresses>

<add
baseAddress=net.tcp://localhost:9909/MonitorService/ />

</baseAddresses>

</host>

<!– Service Endpoints –>

<!– Unless fully qualified, address is relative to base address supplied above –>

<endpoint


address=service


binding=netTcpBinding
bindingConfiguration=DefaultNetTCPBinding


contract=IMonitorService


name=TcpBinding />

</service>

</services>

<behaviors>

<serviceBehaviors>

<behavior>

<!– To avoid disclosing metadata information,

set the value below to false and remove the metadata endpoint above before deployment –>

<serviceMetadata
httpGetEnabled=False/>

<!– To receive exception details in faults for debugging purposes,

set the value below to true. Set to false before deployment

to avoid disclosing exception information –>

<serviceDebug
includeExceptionDetailInFaults=False />

</behavior>

</serviceBehaviors>

</behaviors>

</system.serviceModel>

</configuration>

Step 3: Developing Monitor Application

  1. Create a new Windows Application project using Visual Studio.
  2. Add RichTextBox control to log messages
  3. Add two buttons connect and disconnect.
  4. Now Add the WCF Service reference
  5. Implement Login, Logout and NotifyClient messages
  6. Add following code in the Login method
  7. Implement IMonitorServiceCallback interface and write below code.


try

{

client = new
MonitorServiceClient(new
InstanceContext(this), “TcpBinding”);

client.Open();

client.Login();

WriteTextMessage(“Monitor successfully connected to the Windows Service for logging messages”);

}


catch (Exception ex)

{

WriteTextMessage(“Couldn’t connect to the Service, cause “ + ex.Message);

}

  1. Add following code in the Logout nethod

client.Close();

  1. Add following code in the NotifyClient method.

public
void LogMessage(string message)

{


if (this.InvokeRequired == false)

{


this.BeginInvoke(new
WriteMessage(WriteTextMessage),message);

}


else

{


this.Invoke(new
WriteMessage(WriteTextMessage), message);

}

}

  1. As the application thread is different so we need to invoke the WriteTextMessage using BeginInvoke. In that case I have declared a delegate with the same method signature as of WriteTextMessage and set messages in the RichTextBox control.

public
delegate
void
WriteMessage(String str);


public
void WriteTextMessage(String str)

{

rchTextBox.Text += str + “\n”;

rchTextBox.ScrollToCaret();

}

  1. App.config for Monitor App

<?xml
version=1.0
encoding=utf-8 ?>

<configuration>

<appSettings>

<add
key=DatabaseServer
value=.\sqlexpress/>

</appSettings>

<system.serviceModel>

<bindings>

<netTcpBinding>

<binding
name=TcpBinding
closeTimeout=00:01:00
openTimeout=00:01:00


receiveTimeout=00:10:00
sendTimeout=00:01:00
transactionFlow=false


transferMode=Buffered
transactionProtocol=OleTransactions


hostNameComparisonMode=StrongWildcard
listenBacklog=10
maxBufferPoolSize=524288


maxBufferSize=65536
maxConnections=10
maxReceivedMessageSize=65536>

<readerQuotas
maxDepth=32
maxStringContentLength=8192
maxArrayLength=16384


maxBytesPerRead=4096
maxNameTableCharCount=16384 />

<reliableSession
ordered=true
inactivityTimeout=00:10:00


enabled=false />

<security
mode=Transport>

<transport
clientCredentialType=Windows
protectionLevel=EncryptAndSign />

<message
clientCredentialType=Windows />

</security>

</binding>

</netTcpBinding>

<wsDualHttpBinding>

<binding
name=WSDualHttpBinding_IMonitorService
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=net.tcp://localhost:9909/MonitorService/service


binding=netTcpBinding
bindingConfiguration=TcpBinding
contract=IMonitorService


name=TcpBinding>

<identity>

<userPrincipalName
value=ovais />

</identity>

</endpoint>

<endpoint
address=http://localhost:8732/Design_Time_Addresses/MonitorService/


binding=wsDualHttpBinding
bindingConfiguration=WSDualHttpBinding_IMonitorService


contract=IMonitorService
name=WSDualHttpBinding_IMonitorService>

<identity>

<dns
value=localhost />

</identity>

</endpoint>

</client>

</system.serviceModel>

</configuration>

Step 4: Running solution

  1. Install the service and start it
  2. Start the Monitor app and click on connect
  3. Once the service start it will send messages to client and real time logging is achieved.