Why ASMX web services are not an excuse anymore with WCF 4.0

ASXM web services has been the favorite choice for many developers for building soap web services in .NET during a long time because of its simplicity. With ASMX web services, you get a web service up and running in a matter of seconds, as it does not require any configuration. The only thing you need to do is to build the service implementation and the message contracts (xml serialization classes), and that’s all. However, when you build a system as a black box with most of the configuration hardcoded, and only a few extensibility points in mind, you will probably end up with something that is very easy to deploy and get running, but it can not be customized at all. That’s what an ASMX web service is after all, you don’t have a way easily change the protocol versions, encoders, security or even extend with custom functionality (SOAP extensions are the only entry point for extensibility, which work as message inspectors in WCF).

On the other hand, you have WCF, which is extensible beast for building services among other things. The number of extensibility points that you will find in WCF is extremely high, but the downside is that configuration also becomes extremely complex and a nightmare for most developers that only want to get their services up and running.

Fortunately, the WCF team has considerably improved the configuration experience in WCF 4.0, making possible to run a service with almost no configuration. The approach that they have taken for this version is to make everything work with no configuration, and give the chance to override what you actually need for a given scenario.

For instance, a WCF service that uses http as transport behaves a ASMX web service by default (it uses basicHttpBinding with SOAP 1.2, transport security, text encoding and Basic profile 1.1) unless you change that. So, how can you create a new WCF service as you did before with ASMX ?. That’s simple and you need to follow these steps,

1. Create a new WCF service in Visual Studio

visualstudio_newservice

2. Modify the service and data contract to expose the operations you actually need in the service.

 

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{

[OperationContract]
string GetData(int value);

[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);

// TODO: Add your service operations here
}


// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";

[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}

[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}

3. Optionally, enable the service metadata page for the service, so any client application can use this to generate the proxies.

<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>

 

4. Optionally, enable the ASP.NET Compatibility mode to use the ASP.NET security context (Otherwise, the service will use the default security settings for the basicHttpBinding). That will require two additional steps, adding the “serviceHostingEnvironment” element in the existing service model configuration.

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

 

And adding an attribute in the service,

[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1

That’s all you need to implement a new WCF service that will behave as a traditional ASMX webservice. As you can see, no service or binding configurations were required for the service. In addition, the behavior element does not have any name, so it applies to all the services running in the same host.

12 Comments

  • If you really want to push the limits you can configure wcf to use the xml serializer :)

  • I think the problem was ASPX was just too good and covered the 95% scenario and for the other 5% there was always a work-around.

    Th other problem is just develop training (develpers have been using ASPX for so many years now and it has worked really well for them).

    For most developers, ASPX is fast, easy, well-understood and covers their use-case scanario.

    Of course....we should all be using WCF, but .... it is the education thing. I have been programming in .NET for over 10 years (seriously), and even I am still starting with ASMX just because I know when I hit a problem I can solve it in 5-mins. I just know too much about the internals of the infrastructure.

    It is funny really. If ASMX has not been so good, we would probably all be using WCF by now ;-)

    Dave

  • In My experience WCF suffers enormously due to its concurrent connections limit, which no amount of tinkering with configs seems to solve.

    Case in point: I have a web app that uses javascript to retrieve some statistics from a web service. When using WCF you can see a visible delay as each stat is retrieved, and that is just in a test environment with only one client. ASMX has no such issue.

  • The problem is that the majority of the big companies where I live are still with .net 2.0 and aren't planning on upgrading any time soon...

  • John H, what concurrency and instance context modes are you using when experiencing this behavior?

  • Your article could do with explaining why you need to "Modify the service and data contract to expose the operations you actually need in the service"

    its all very well pointing out that it needs to be done, but it doesnt help the reader understand why they are doing it.

  • So, what are the benefits of WCF over ASMX?

  • Hi Pablo M. Cibraro ,

    I had been working with Web Services for quite some time. Very simple and easy way to start with WCF, but when start using the ASp.Net security setting by using 'aspNetCompatibilityEnabled' we usually get an error that's because we have to include the 'System.ServiceModel.Activation' namespace. If u could include this in the article then it would be easy for someone like me to start off with out worrying much.

    Thank You


  • @Andrew, Have you seen it already ?. That's all clear after you read this article

  • Nice post Pablo :) ASMX should already be deprecated, as web service references.

  • Hi,

    Very nice article. We got a ton of advantage using the WCF service. Silverlight is one of the utility that gets most of WCF.

    Thanks,
    Thani

  • The reason you need to decorate the service and service operations with the [attributes] is because it causes .NET to create a lot of code related to channels, etc. that you do not have to create. [attributes] are a form of aspect oriented programming where a common pattern of coding is applied here and there as needed. In this case Microsoft has provided the implementation. In more advanced cases you can create your own [attribute].

Comments have been disabled for this content.