Skip to content
Development

How to Create a Dummy/Mock ASMX Web Service from a WSDL File

3 min read

Legacy technology notice: ASMX web services are a .NET Framework 1.x/2.0-era technology that Microsoft considers deprecated. For new projects, consider REST/OpenAPI (with mock tools such as WireMock), gRPC, or at minimum WCF (itself also in maintenance mode). This post is preserved for those maintaining older systems.


Key Takeaways

  • Use wsdl.exe /server to generate a C# server-side stub from any WSDL URL.
  • Inherit your ASMX code-behind from the generated stub class to satisfy the service contract automatically.
  • Stub out each method with throw new NotImplementedException() first, then replace with controlled responses as needed.
  • This approach lets you control service responses in tests without depending on an external endpoint.

Why Mock an ASMX Web Service?

Sometimes it is helpful to mock an XML web service for testing without invoking the actual service. There are two common reasons:

  • The service is hosted externally and is not under your control.
  • Calls to the service incur a financial or rate-limit cost.

In this case, the goal was to mock out a service so that unit tests covered only the integration layer, with full control over response payloads to exercise a range of scenarios.

A WCF-based mock would have been equally valid, but an ASMX stub is simpler for a throw-away test double — consistent with the KISS principle.


Step 1: Create the ASMX File

In your test web application, right-click the project and choose Add > New Item > Web Service.

Name the file something descriptive, following the pattern <ServiceName>Mock.asmx — for example, PaymentServiceMock.asmx.

If you do not already have a test web application, create a basic ASP.NET Web Application project in Visual Studio to host the stub.


Step 2: Generate a Server Stub from the WSDL

Open the Visual Studio Developer Command Prompt (available from the Start menu under the Visual Studio folder) and navigate to the directory containing your ASMX file.

Run the following command:

Bash
wsdl <URL of WSDL file> /language:CS /server

Parameter breakdown:

ParameterPurpose
<URL>Full URL to the WSDL — typically ending in ?wsdl
/language:CSGenerate C# (use VB for Visual Basic)
/serverEmit a server-side abstract class rather than a client proxy

The tool generates a .cs file in the current directory. Run wsdl with no arguments to see the full list of options.


Step 3: Wire the ASMX to the Generated Stub

By default, the code-behind for your ASMX derives from System.Web.Services.WebService:

C#
public class PaymentServiceMock : System.Web.Services.WebService
{
}

You need to change the base class to the generated stub. Open the generated .cs file and search for a class decorated with [System.Web.Services.WebServiceAttribute] — that is the class you want to inherit from:

C#
public class PaymentServiceMock : PaymentServiceHttpPost
{
}

Once you have updated the base class, build the project. The build will fail because you have not yet implemented the abstract service methods — this is expected and confirms the contract is being enforced correctly.

Implement each method by throwing NotImplementedException so the project compiles:

C#
[WebMethod]
public override PaymentResponse ProcessPayment(PaymentRequest request)
{
    throw new NotImplementedException();
}

Build again. Once it compiles cleanly, open the ASMX URL in a browser. You should see the standard ASP.NET web service description page with the service name displayed in the blue header panel.


Step 4: Customise the Mock Responses (Optional)

With the stub compiling and hosted, replace the NotImplementedException throws with controlled return values. This allows you to drive specific test scenarios — success responses, fault responses, edge-case payloads — without any external dependency.

C#
[WebMethod]
public override PaymentResponse ProcessPayment(PaymentRequest request)
{
    // Return a controlled response for testing
    return new PaymentResponse
    {
        Success = true,
        TransactionId = "TEST-001"
    };
}

Each method can be tailored independently, giving you full control over the service contract behaviour in your test environment.

David Christiansen
David Christiansen

Solution Architect with 30 years in cloud infrastructure, security, identity, and .NET engineering.

Related Posts