3

Here is what I have. When I try to access my service via http://localhost:55129/Cars.svc it works fine.

When I try http://localhost:55129/Cars it does not. I thought the Route should have allowed this but I'm obviously missing something.

Here's my service class

[ServiceContract(Name = "Cars", Namespace = "http://localhost:53329", SessionMode = SessionMode.NotAllowed)]
public interface ICars
{
    [OperationContract(Name="Get"), WebGet(UriTemplate = "/cars",
                        BodyStyle= WebMessageBodyStyle.Wrapped,
                        RequestFormat = WebMessageFormat.Json,
                        ResponseFormat = WebMessageFormat.Json)]
    string Get();

    [OperationContract(Name = "GetById"), WebGet(UriTemplate = "/cars/?id={id}",
                        BodyStyle= WebMessageBodyStyle.Wrapped,
                        RequestFormat = WebMessageFormat.Json,
                        ResponseFormat = WebMessageFormat.Json)]
    Car Get(int id);
}

Here's my web.config

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>

  <system.serviceModel>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
        <serviceActivations>
            <add relativeAddress="Cars.svc" service="Sandbox.WCF.API.Cars"/>
        </serviceActivations>
    </serviceHostingEnvironment>

    <bindings>

      <!-- SOAP Binding -->
      <basicHttpBinding>
        <binding name ="soapBinding">
            <security mode="None"></security>
        </binding>
      </basicHttpBinding>

      <!-- Enable RESTful Endpoints-->
      <webHttpBinding>
        <binding name="webBinding"></binding>
      </webHttpBinding>

    </bindings>


    <behaviors>

      <endpointBehaviors>

        <!-- allow XML REST -->
        <behavior name="poxBehavior">
            <webHttp defaultOutgoingResponseFormat="Xml"/>
        </behavior>

        <!--<behavior name="jsonBehavior"><enableWebScript/></behavior>-->
        <!-- allow JSON REST -->
        <behavior name="jsonBehavior">
            <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json"/>
        </behavior>

      </endpointBehaviors>

      <serviceBehaviors>

        <behavior name="defaultBehavior">
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceMetadata httpGetEnabled="true" />
        </behavior>

      </serviceBehaviors>

    </behaviors>


    <services>

      <service name="Sandbox.WCF.API.Cars" behaviorConfiguration="defaultBehavior">
        <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
        <endpoint address="soap" binding="basicHttpBinding" bindingConfiguration="soapBinding" contract="Sandbox.WCF.API.Interfaces.ICars" />
        <endpoint address="json" binding="webHttpBinding" bindingConfiguration="webBinding" behaviorConfiguration="jsonBehavior" contract="Sandbox.WCF.API.Interfaces.ICars" />
      </service>

    </services>

  </system.serviceModel>


  <system.webServer>

    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>

  </system.webServer>

</configuration>

UPDATE - Per Luiz's comments

might have something I am still not doing right, because it's not working yet:

  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
    <serviceActivations>
      <add relativeAddress="Cars.svc" service="OurCompany.API.Service.Cars"/>
    </serviceActivations>
  </serviceHostingEnvironment>

In my service project's global.asax

protected void Application_Start(object sender, EventArgs e)
{
    RegisterRoutes();
}

private void RegisterRoutes()
{
    RouteTable.Routes.Add(new ServiceRoute("Cars", new WebServiceHostFactory(), typeof(Cars)));
}
PositiveGuy
  • 46,620
  • 110
  • 305
  • 471

1 Answers1

0

First, the namespace has nothing to do with service address, its used on targetnamespace on wsdl and to compose the soap action, can be used also to do message routing betweeen endpoints.

The aspNetCompatibilityEnabled will make the request go throught the aspnet pipeline, then you can use aspnet url rewrite to solve your problem.

But you are using dotnet 4.5, then just put this on Global.asax.cs its easier than the url routing module.

    void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes(RouteTable.Routes);
    }

    private void RegisterRoutes(RouteCollection routes)
    {
        routes.Add(new ServiceRoute("Cars",
                   new ServiceHostFactory(), typeof(Sandbox.WCF.API.Cars)));
    }

This Eliminate the .svc in the URL of a WCF 4 service using Routes? is related to what you are trying to do. There is a discussion about this problem here ServiceRoute + WebServiceHostFactory kills WSDL generation? How to create extensionless WCF service with ?wsdl

Community
  • 1
  • 1
Luiz Felipe
  • 1,123
  • 8
  • 14
  • "First, the namespace has nothing to do with service address" - where are you referring to above?? – PositiveGuy Oct 14 '13 at 17:04
  • is this global.asax in the web service itself or application when adding these routes? – PositiveGuy Oct 14 '13 at 17:09
  • aspNetCompatibilityEnabled is already enabled and I still can't get this working – PositiveGuy Oct 14 '13 at 17:14
  • When you use Namespace = "http://localhost:53329" on ServiceContract this does not change anything on the address, it just change TargetNamespace on WSDL. – Luiz Felipe Oct 15 '13 at 17:27
  • Yes, you need to add new item global.asax.cs on application. – Luiz Felipe Oct 15 '13 at 17:28
  • Also, have you tried to set your route using ServiceHostFactory, if you use WebServiceHostFactory then it overrides some behavior, I think this is wrong. Download this project http://pastelink.me/dl/50e376, it is working that way (with the ServiceHostFactory instead). – Luiz Felipe Oct 15 '13 at 17:32