views:

637

answers:

6

I have an asp.net mvc application and now I need to add a web service to go along with it. What is the best solution for this? Just create a new web service project and add it to my solution then deploy it to the same web server using a different url? I would like it to be a part of the mvc application only because I have all my database code in there.

Any suggestions would be appreciated. Thanks.

A: 

MVC is built on the asp.net framework. You should be able to include a web service within the same project. I haven't done it but I know that you can combine asp.net forms applications with MVC applications in the same project. The same should go for web services.

Andrew Robinson
A: 

Unless your application is very small I would recommend you create different projects for each logical part of the application. A good staring point is having a project for each of these:

  • Domain objects
  • Data access
  • Web Services
  • UI (your ASP.NET MVC app)

This provides a clean separation corresponding to your architecture and supports reuse.

Brett Carswell
+3  A: 

Web service could mean a soap based web service or a RESTful web service. I can't think of any reason why you would not be able to simply add an asmx file to your project and be in business. That is the soap based route. If you want to be really cool though you can simple return xml from a controller action and implement a RESTful solution right over the MVC framework.

Blake Taylor
REST / Cool factor: +1!
Andrew Robinson
A requirement is for soap based. We have an existing service and need to basically make it work the same for the new app I have.
Mike Roosa
If I do a soap based service, would all my clients have to update their reference everytime i changed and deployed my mvc app or only if I changed the asmx page. Never done this before which is why I ask. Thanks.
Mike Roosa
Clients will need to update references only if you change the interface exposed by the *.asmx or its URL. You will be safe to change implementation of the service as well as recompile and redeploy your site at will.
Blake Taylor
+3  A: 

There's no reason not to add a web service project.

You state that all your database code is in your MVC project. I strongly recommend you remove it from there into a separate class library project. This third project would be used both by the web service and by the MVC application.

I also strongly recommend that you not use ASMX web services for any new development.

Use WCF only, unless you have no choice at all. There's a misconception that WCF services don't do SOAP - they do, and WCF has replaced ASMX.

John Saunders
Our customer already has a client for our webservice. The client is a .net 2.0 app. The goal is that once I'm done they just need to update their reference to my new webservice and make no code changes. Can a .net 2.0 app set a reference to a wcf just like a asmx web service?
Mike Roosa
Yes, it can. Just have the WCF service use "basicHttpBinding", and the .NET 2.0 client won't know the difference.
John Saunders
Just curious...why do you strongly recommend against ASMX? Just a few links to references to why it is bad would be nice.
Pat James
See http://johnwsaundersiii.spaces.live.com/blog/cns!600A2BE4A82EA0A6!860.entry
John Saunders
A: 

If you want to use a regular ASP.NET asmx web service, it's certainly possible. Here's an example from Scott Hanselman that does just what you are asking about and it throws in some other ASP.NET technologies for good measure.

All you have to do is File -> New Item -> Web Service and it should work like a regular ASP.NET application in your Mvc project.

Steven Lyons
A: 

i think there's a couple of things here.

you can indeed add a web service to an MVC application. you may even consider identifying the web service(s) as a script service to make REST like operations easier to perform via javascript. this may not be necessary due to your circumstances.

i think there is a stronger question as to the underlying architecture. If you are placing the web service withing your mvc application, because, your database code is already there...should it be? it might be a good time to abstract your data layer out a little. However, if you're dealing with a relatively small project and don't need the flexibilty, then certainly, add a web service right in. i guess what it really boils down to is addressing the true needs of your application.

Robert Sweeney