Introduction
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 {
public abstract class WebServiceControllerWrapper<T> : Controller where T : WebService {
#region Constructors
public WebServiceControllerWrapper() {
this.Service = Activator.CreateInstance<T>();
}
#endregion
#region Properties
private T Service { get; set; }
#endregion
#region Overriding Methods
protected override void HandleUnknownAction(string actionName) {
MethodInfo method = this.Service
.GetType()
.GetMethods()
.Where(found =>
found.IsPublic &&
found.Name.Equals(actionName, StringComparison.OrdinalIgnoreCase) &&
found.GetCustomAttributes(typeof(WebMethodAttribute), true).Count() > 0
)
.FirstOrDefault();
if (method == null) { return; }
List<object> arguments = new List<object>();
ParameterInfo[] parameters = method.GetParameters();
foreach (ParameterInfo param in parameters) {
object arg =
this.ValueProvider[param.Name].ConvertTo(param.ParameterType);
arguments.Add(arg);
}
object result = method.Invoke(this.Service, arguments.ToArray());
this.Response.ContentType = "text/xml";
if (method.ReturnType != null) {
if (result is XObject) {
using (StreamWriter writer =
new StreamWriter(this.Response.OutputStream)) {
writer.Write(result.ToString());
}
}
else {
XmlSerializer serialize = new XmlSerializer(result.GetType());
serialize.Serialize(this.Response.OutputStream, result);
}
}
}
#endregion
}
}
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…
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.