2

When I try to access my WCF service at the following address it works fine:

http://localhost/webservices/main.svc

Great. But I want to be able to call this service in following way:

http://localhost/api/main

So I am attempting to put in the proper ServiceRoute within the RouteConfig definitions in my MVC. I am able to make the routing work when the routePrefix variable is singular. But I cannot get it to work when routePrefix it is a path such as, as in my case, I want to map a route to ~/api/main.

I've scoured around The Google and this particular question is not common. I am pretty much seeking to accomplish the similar to what is stated in the accepted answer of the following previously posted question on this topic:

Expose WCF services that belong to an Area in MVC app at a routed path

I also attempted to implement a route constraint as suggested here: http://geekswithblogs.net/michelotti/archive/2010/09/22/wcf-rest-services-inside-mvc-projects.aspx

But still get the following error:

No HTTP resource was found that matches the request URI 'http://localhost/api/main'.
No type was found that matches the controller named 'main'.

Here is what I've attempted so far:

Imports System.Web.Mvc 
Imports System.Web.Routing 
Imports System.ServiceModel.Activation
Imports MyServiceLibrary

Public Class RouteConfig
    Public Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
       [..other routing stuff...]

       routes.Add(New ServiceRoute("api/main", New ServiceHostFactory(), _
           GetType(MyServiceLibrary.main)))

       routes.MapRoute("Default", "{controller}/{action}/{id}", New With { _
            Key .controller = "Default1", _
            Key .action = "Index", _
            Key .id = UrlParameter.Optional}, _
            New With {Key .controller = "^(?!main).*"})


       [..other routing stuff...]
    End Sub
End Class

Can't seem to figure out the fix on this.

EDIT 2:

When I first posted this question I had a problem with what turned out to be malformed VB syntax.

The original INCORRECT syntax:

routes.Add(new ServiceRoute("api/main", new ServiceHostFactory(), _
              typeof(MyServiceLibrary.main)))

The CORRECT vb syntax should be:

routes.Add(New ServiceRoute("api/main", New ServiceHostFactory(), _
           GetType(MyServiceLibrary.main)))

Notice that in VB you should use GetType instead of typeof. Fixing this was a good step forward, but did not solve the underlying problem.

EDIT 3:

Well, turns out if I modify the ServiceRoute as follows, then IT WORKS:

routes.Add(New ServiceRoute("main", New ServiceHostFactory(), _
           GetType(MyServiceLibrary.main)))

So, the question is how to create the route i desire "api/main"...

EDIT 4:

I thought I might have found the solution here: Can a WCF ServiceRoute Route Prefix contain a path value?

The solution there notably implements WebServiceHostFactory instead of ServiceHostFactory. I tried it, but it didn't work for me.

Community
  • 1
  • 1
brando
  • 8,215
  • 8
  • 40
  • 59
  • No idea why your question is being downvoted. Both the MSDN documentation and the ASP.Net documentation for Web API are awful, and are even less helpful than if they hadn't documented anything in the first place. Nothing but blind alleys... – jerhewet Jul 14 '14 at 19:00

1 Answers1

0

You should pass in the wcf service type as Type. That is the service defined in your web.config file.

Take a look at this page: http://blogs.msdn.com/b/endpoint/archive/2010/01/25/using-routes-to-compose-wcf-webhttp-services.aspx

Jocke
  • 2,189
  • 1
  • 16
  • 24
  • Ok I added a snippet from service section of web.config (see edit 1). You are referring the name of the service "MyServiceLibrary.main"? – brando Jun 01 '14 at 20:53
  • Does it work for you? If not - what error do you get? – Jocke Jun 01 '14 at 21:24
  • No doesn't work. Under "Edit 1" above I put the intellisense errors i am getting. – brando Jun 01 '14 at 21:28
  • Hello Jocke, thanks for your feedback here. Seems I had bad VB syntax, which I have fixed. See what I have written under "EDIT 2" – brando Jun 02 '14 at 00:17