Two easy ways to expose a WCF as a WebService (even asmx)

Assuming we’re all using Visual Studio 2010, start by creating two projects: a WCF Service library project and a WCF

image image

The service library will contain the contracts and their interfaces with all the service attributes while the service application will be the project exposed by our web server, it will have the *.svc and *.asmx files along with the config file containing all the WCF settings. Let’s see the simplest contract possible:

[ServiceContract]
public interface ITimeManager
{
   [OperationContract]
   DateTime GetActualTime();
}

public class TimeManager : ITimeManager
{
   public DateTime GetActualTime()
   {
       return DateTime.UtcNow;
   }
}

We create two services in the service application containing the reference to the contact, nothing prevent us from creating:

image

Where the markup will be the following:

//This will be included in TimeServiceSVC.svc
<%@ ServiceHost Language="C#" Debug="true" Service="Giorgio.WCFasWS.Library.TimeManager" %>

//This will be included in TimeServiceASMX.asmx
<%@ WebService  Language="C#" Debug="true" Class="Giorgio.WCFasWS.Library.TimeManager, Giorgio.WCFasWS.Library "%>

If we now try to browse one of the services on IIS we will probably get the following exception:  Could not load type ‘System.ServiceModel.Activation.HttpModule’ from assembly ‘System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’

Register Asp.Net 4.0 to fix the error by running aspnet_regiis -iru (go here for more details)

If you want your WS to be fully exposed (and declare it to use basic profile) decorate the contract as follows:

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1, EmitConformanceClaims = true)]
[ServiceContract]
public interface ITimeManager
{
    [WebMethod]
    [OperationContract]
    DateTime GetActualTime();
}

public class TimeManager : ITimeManager
{
    public DateTime GetActualTime()
    {
        return DateTime.UtcNow;
    }
}



No Comments


You can leave the first : )



Leave a Reply

Your email address will not be published. Required fields are marked *