Friday, November 17, 2017

Creating WCF Service without .SVC File

This blog demonstrates how to create a WCF service without having physical .SVC File and after .net 4.0 release, we can create a WCF service without having svc file.

Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Using WCF, you can send data as asynchronous messages from one service endpoint to another. A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application


In general, WCF Service have .SVC File and Service Code File as shown below







This SVC file is a simply text file and which contains the below information, which allows to host WCF Service and respond to incoming request/message.

·         Language: (C# / VB)
·         Service (Name of the service) : MoneyTransfer
·         CodeBehind (The location of service code ) : MoneyTransfer.svc.cs

Here is an example of SVC file:

<%@ ServiceHost Language="C#" Debug="true" Service="MoneyTransfer" CodeBehind="Money.MoneyTransfer.svc.cs" %>

A .SVC file contains a WCF-specific processing directive (@ServiceHost) that allows the WCF hosting infrastructure to activate hosted services in response to incoming messages. This file contains the details required for WCF service to run it successfully.


CodeBehind File : MoneyTransfer.svc.cs


namespace Money

{
    [ServiceContract]
    public interface IMoneyTransfer
    {
        [OperationContract]
        bool Post(decimal money, int accountNumber);

        [OperationContract]
        bool Withdrawal(decimal money, int accountNumber);
    }

    public class MoneyTransfer : IMoneyTransfer
    {
        public bool Post(decimal money, int accountNumber)
        {
            throw new NotImplementedException();
        }

        public bool Withdrawal(decimal money, int accountNumber)
        {
            throw new NotImplementedException();
        }
    }
}


WCF HOSTING


after .net 4.0 release, it allows to create a WCF service  without SVC file and all required setting for WCF hosting we can defined in web.config file.

The service activation is configuration element that allows you to define virtual service activation settings that map to your Windows Communication Foundation (WCF) service types. This makes it possible to activate services hosted in WAS/IIS without a .svc file.

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" >
      <serviceActivations>        <add relativeAddress ="MoneyTransfer.svc" service ="Money.MoneyTransfer"/>
      </serviceActivations>
    </serviceHostingEnvironment>

Breakdown the serviceActivations configuration element

·         RelativeAddress : the relative address within the current IIS application and it contain .SVC file address, which does not physically exist

·         Service: the service type that implements the service.




No comments:

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...