Can an asp.net mvc application also have a web service?

17,024

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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.

Share:
17,024
Mike Roosa
Author by

Mike Roosa

Updated on June 14, 2022

Comments

  • Mike Roosa
    Mike Roosa almost 2 years

    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.

  • Mike Roosa
    Mike Roosa about 15 years
    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
    Mike Roosa about 15 years
    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.
  • Blake Taylor
    Blake Taylor about 15 years
    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.
  • Mike Roosa
    Mike Roosa about 15 years
    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?
  • John Saunders
    John Saunders about 15 years
    Yes, it can. Just have the WCF service use "basicHttpBinding", and the .NET 2.0 client won't know the difference.
  • Pat James
    Pat James over 14 years
    Just curious...why do you strongly recommend against ASMX? Just a few links to references to why it is bad would be nice.