Click here to Skip to main content
15,397,517 members
Articles / Web Development / ASP.NET
Technical Blog
Posted 27 Sep 2009

Tagged as


21 bookmarked

Combining Web Services with MVC

Rate me:
Please Sign up or sign in to vote.
4.53/5 (12 votes)
29 Sep 2009CC (ASA 2.5)2 min read
WebServices make for a convenient way to share functionality across many different applications. MVC makes it easy to call methods by using standard HTTP calls. With a little bit of Reflection, you can map an MVC Controller to a Web Service.


I always liked the concept behind Web Services. Having a single place to store a bunch of complex but commonly used functions is a great way to decrease the complexity of other programs that all sit on the same network. If you’re like me and tend to do a lot of intranet applications, a Web Service can prevent a lot of duplicate code.

My only real problem with Web Services was having to use SOAP. Adding a Web Reference to a project wasn’t that big of a deal, but if you ever wanted to just call a function real quick, say from a script file (yes, I do VBScript occasionally… ick), then it isn’t quite as I’d prefer. You end up spending more time making sure your XML is well formed and less time on the logic inside your quick script.

MVC helps to get around that problem by allowing you to perform normal HTTP calls and plug all your arguments into the query string or the body of the request – something much easier to do. The problem, however, is that you end up losing the convenience of using a Web Service with other applications.

A Simple Solution

The idea here is to create a controller that acts as a wrapper to a Web Service. By doing this, we can override a few methods on our Controller that use Reflection to invoke the matching method on the Web Service. Also, because the Controller is still a unique class, we can still attach any number of Actions to it as we normally would. Below is some code that I wrote the other day. It isn’t battle tested, so if you use it, be sure to verify it does everything that you need it to do.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Mvc;
using System.Reflection;
using System.Xml.Serialization;
using System.Xml.Linq;
using System.IO;

namespace Interface.Controllers {

    /// <summary>
    /// Abstract wrapper that handles calling
    /// WebService methods on behalf of a controller
    /// </summary>
    public abstract class WebServiceControllerWrapper<T> : Controller where T : WebService {

        #region Constructors

        /// <summary>
        /// Creates a new Controller Wrapper for a WebService
        /// </summary>
        public WebServiceControllerWrapper() {
            this.Service = Activator.CreateInstance<T>();


        #region Properties

        //The service that is being used for this call
        private T Service { get; set; }


        #region Overriding Methods

        //finds the correct method for the WebService method
        protected override void HandleUnknownAction(string actionName) {

            //find if the method exists
            MethodInfo method = this.Service
                .Where(found =>
                  found.IsPublic &&
                  found.Name.Equals(actionName, StringComparison.OrdinalIgnoreCase) &&
                  found.GetCustomAttributes(typeof(WebMethodAttribute), true).Count() > 0

            //if no method was found, just give up
            if (method == null) { return; }

            //check if all the arguments were found
            List<object> arguments = new List<object>();
            ParameterInfo[] parameters = method.GetParameters();
            foreach (ParameterInfo param in parameters) {

                //check if this argument was found
                object arg = 

            //with the arguments try and call the result
            object result = method.Invoke(this.Service, arguments.ToArray());

            //if there is a return value, serialize it and write it
            this.Response.ContentType = "text/xml";
            if (method.ReturnType != null) {

                //if this is an XElement of some kind, just use it as is
                if (result is XObject) {

                    //write the string
                    using (StreamWriter writer = 
                           new StreamWriter(this.Response.OutputStream)) {
                else {

                    //try and serialize it
                    XmlSerializer serialize = new XmlSerializer(result.GetType());
                    serialize.Serialize(this.Response.OutputStream, result);

The idea here is to inherit this class instead of the standard Controller class and provide the name of the Web Service we want to wrap around as our Generic argument. For example…

//that's about it - actions are mapped automatically
// to the correct method on the webservice
public class AccountController : WebServiceControllerWrapper<AccountWebService> { }

By doing this, when our Controller receives an Action, is checks the Web Service instance for the same method and then tries to call the method with the arguments it finds as part of the request!

And that’s it! A quick and simple way to map incoming actions to a matching method on a Web Service!

The Return Type

I’m not sure the best way to handle the return type at this time. It seems to me that the XmlSerializer should be sufficient for objects with the exception of XML, which should probably just be written out as a string. If you have a suggestion on a better way to respond to incoming requests, please let me know. :)

Remember: This is just some quick and dirty code – This needs some more polish and exception handling before I’d use it in a real project, but at least, it might help you get going in the right direction.


This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License


About the Author

United States United States
No Biography provided

Comments and Discussions

GeneralMy vote of 3 Pin
Manoj kumar V21-Jan-14 0:33
professionalManoj kumar V21-Jan-14 0:33 
QuestionError in the code Pin
lzliu21-Aug-12 5:44
Memberlzliu21-Aug-12 5:44 
AnswerRe: Error in the code Pin
Jeff West12-Sep-13 10:55
MemberJeff West12-Sep-13 10:55 
GeneralCongratulations Pin
Carlos Gutierrez28-Dec-11 6:55
MemberCarlos Gutierrez28-Dec-11 6:55 
GeneralPlease give me a sample application Pin
jibugeo29-Sep-09 3:38
Memberjibugeo29-Sep-09 3:38 
GeneralRe: Please give me a sample application Pin
webdev_hb29-Sep-09 3:48
Memberwebdev_hb29-Sep-09 3:48 
It wouldn't really be an Application in the sense of browsing and clicking links, but instead a second way to invoke WebService functionality.

Let's say we had a WebService method...
GetAccountsForCompany(CompanyID, AccountTypes)

If you need to use this WebService you need to create the SOAP information, prepare the headers and then post the request to the server.

The idea behind this code was to simply wrap the functionality for a WebService with an easier to approach Controller. That same web service, could be called all from a single HTTP call without the additional SOAP setup, for example...

The Setup
public class AccountInformationController : WebServiceControllerWrapper<AccountInformationWebService> { }

The HTTP Call

This way you still have a WebService that you can add a reference to inside of Visual Studio, but an additional, more convenient way to call the same methods from other places, such as my example of a script file.

I can't say if this is a good idea or best practice - It was just a little trick I was messing around with the other day that I wanted to share.
GeneralRe: Please give me a sample application Pin
Member 1005599013-Dec-14 1:12
MemberMember 1005599013-Dec-14 1:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.