0

I'm in the process of trying out a few things with MVC whilst designing a system and wanted to try and see if I could use the concept of Controllers outside of the MVC framework. When I say outside, I mean within my own C# service as opposed to a web-site.

I started a simple console application to test the theory and it was simple enough to change the profile to a non-client profile, add in System.Web.Mvc, create a controller and have it return a JsonResult. The ease of which this got set up pleased me as that is half the work done if I want a service to respond with JSON.

The next step is to set up a Http Server class, and I would love it if I could leverage the other part of the framework which will map incoming requests to my controllers. Unfortunately, this is the part where I am lost and I have no idea what code goes on behind to arrive at a particular controller's function with the parameters all in place.

Has anyone got an idea on how to accomplish this, or a resource to look at?

In short: I'd like to leverage the use of Controllers in my own service, running it's own HTTP Server.

4 Answers 4

1

You can use the design pattern without using the framework - what I mean is, you can apply the model view controller pattern wherever you believe it solves the problem - if you think that you can replace "view" with "service", you could apply some of the concepts...

http://msdn.microsoft.com/en-us/library/ff649643.aspx

However, there are other patterns that may lend themselves better to services, although if we are talking specifically about a JSON service, then just using the ASP.NET MVC framework as it is would work well (rather than trying to re-write it).

Sign up to request clarification or add additional context in comments.

1 Comment

After some digging in to other options, I agree. I don't have to use views at all and just have a purely controller-driven interface to the data (JSON in and out). I'll delegate long-running tasks to a separate service (that shares the same model DLLs).
1

Are you not trying to reinvent the wheel?

If returning JSON is one of your main purpose then WCF fulfills your need. Having WCF with you you are free to host it in IIS. This serves your second purpose having its own HTTP server.

You are trying to achieve some kind of routing where based on URL your different actions will be called. Isn't it similar to having a WCF service with different methods and client is calling each of them with different URL?

Trying controller concept in a non web application seems to be innovative, however in your case it looks like over-engineering.

2 Comments

No, I do not wish to reinvent the wheel. The MVC framework provides a nice mechanism for taking an incoming web request, mapping it to a method on a controller and responding to it. I could throw away the controllers from Mvc altogether and follow the "concept" as Sohnee suggests, but if it is possible to use EXISTING classes to do this OUTSIDE of an MVC ASP.NET application, I'd like to use it. As you can see, I'm not trying to reinvent the wheel here.
That said, I am looking at WCF as we speak :)
1

The basic MVC pattern isn't all the difficult to replicate. I would seriously consider writing your own, rather than trying to shoehorn the MVC classes into your app.

Simon

Comments

1

If it helps you, the entire ASP.Net MVC Framework is open source, you can download it all from http://aspnet.codeplex.com/ . You can use the libraries here to view how the Framework is doing things behind the scenes and adapt things for your own use as appropriate.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.