2

I'm trying to make the same project to work with WCF and MVC.

My problem is:

MVC is working perfectly, than I included the interface and the .svc that I had in WCF service.

When I try something like this:

http://localhost:2986/PAGENAME.svc

I get the following error:

The resource cannot be found.

NOTE: PAGENAME.svc is in root (and so as the interface).

Looking forward this problem, I included the ignore methods in RegisterRoutes:

routes.IgnoreRoute("{resource}.svc/{*pathInfo}");
routes.IgnoreRoute("{resource}.svc");

But didn't work either =/

Does anyone know how to fix this?

Thank you!

tereško
  • 58,060
  • 25
  • 98
  • 150
briba
  • 2,857
  • 2
  • 31
  • 59

2 Answers2

2

You need to make sure that you have all the files required, which are referenced from the Service Host (.svc file), i.e.:

<@% ServiceHost Service="..."/>

Where Service specifies the service implementation. The service contract (the interface that the service implementation implements) is usually configured in web.config.

You don't need to ignore the route if the service host file is at the root of your solution.

You need to reference System.ServiceModel.

If you want to test your service you can by opening visual studio command prompt and running wcftestclient, File -> Add service and add the url for your service, e.g.:

http://locahost:12423/MyService.svc
Rui
  • 4,847
  • 3
  • 29
  • 35
  • I have the reference for "System.ServiceModel" =) And I have the service implementation too (the service is using a lib that MVC uses too) Interface is there either =/ – briba Dec 18 '13 at 17:25
  • Make sure that the right http handler is being called. It should be the one that handles .svc file extension. This is basicaly the same as saying that you should make sure your ignore route is working properly. If it's any help, you can check which handler is being called in IIS: http://serverfault.com/questions/350318/iis-7-5-how-to-find-which-handlers-are-being-called-for-a-request – Rui Dec 18 '13 at 20:12
  • 1
    Maybe a safer IgnoreRoute is: {*url}.svc. * means ignore the "/". See http://stackoverflow.com/questions/3156204/can-someone-explain-asp-net-routing-syntax-to-me. Also the order of the routes is important, so make sure you put that one first – Rui Dec 18 '13 at 20:16
  • "routes.IgnoreRoute("{*url}.svc");" didn't work either (and it is the first one) =,( I'm so lost hahaha... thank you for the article @Rui... at least I'm learning =D – briba Dec 18 '13 at 20:36
  • I just tried creating a new mvc project, add new service at the root of the solution (not inside views). I did not change the routes. And then I was able to get it running without a problem. Are you sure that the .svc file is at the root of the solution, and not inside the views folder – Rui Dec 18 '13 at 23:33
  • Pretty sure =/ ...I will do the same as you, create a MVC 4 Project and include WCF files (an empty interface and .svc) =) to check what's wrong – briba Dec 19 '13 at 12:46
  • I created a new WCF service inside the project and now it's working... I was doing only a copy of the files, and that wasn't working... thank you SO MUCH Rui! Can you edit your post so I can accept the answer? Thank you thank you \o/ – briba Dec 19 '13 at 13:31
  • I created an empty project (ASP.NET MVC 4 Web Application - WebAPI)... than I added a "WCF Service" inside... (working yay =D ) but when I run .svc VS doesn't open that WCF Test Window... Is there any way to make that possible? =) – briba Dec 19 '13 at 13:40
  • 1
    Open visual studio command prompt and type wcftestclient, add the url for the svc file (File -> Add service). You can then test the service – Rui Dec 19 '13 at 14:28
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43530/discussion-between-rui-and-breno-riba) – Rui Dec 19 '13 at 15:58
2

It's been a while since I've played with this, but I think when using MVC you need to register a service route... but I don't remember if that's what I had to do or if I just wanted to do that for cleaner routes.

To add a service using a service route, you would do something like the following

routes.Add("MyService", new ServiceRoute(
    "some/path",
    new ServiceHostFactory(),
    typeof(MyService)
));
Darren Kopp
  • 76,581
  • 9
  • 79
  • 93
  • 1
    That would eliminate the need for an .svc file – Rui Dec 18 '13 at 17:22
  • Ah that sounds right, it's been a while since I've played with WCF – Darren Kopp Dec 18 '13 at 17:23
  • Yep.. I don't want to eliminate the .svc file =/ ...Actually I want both possibilities.. using SOAP and HTTP =) – briba Dec 18 '13 at 17:23
  • Do I have to add a route for .svc file? – briba Dec 18 '13 at 17:48
  • 1
    @BrenoRiba the route can stand in for the .svc file, so you wouldn't need it there. The service file just provides information for asp.net like the service type and what not, and that's what the service route does as well. It also allows you to put the route as whatever you would like, rather than where you put the .svc file at. – Darren Kopp Dec 18 '13 at 17:59
  • Hmmm I get it! Still trying to solve this little problem xD ...but thank you @DarrenKopp! Appreciate your help! – briba Dec 18 '13 at 18:16
  • May I add that you can add the endpoint configuration in the web.config exactly the same way, either you choose to host your service with a .svc file or if you use the ServiceHostFactory – Rui Dec 18 '13 at 20:24