wcf - ServiceRoutes 在 WCF 中不起作用

标签 wcf

这是我的。当我尝试通过 http://localhost:55129/Cars.svc 访问我的服务时,它工作正常。

当我尝试 http://localhost:55129/Cars 时,它没有。我认为路线应该允许这样做,但我显然遗漏了一些东西。

这是我的服务等级

[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);
}

这是我的 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>

更新 - Per Luiz 的评论

可能有一些我还没有做对的事情,因为它还没有工作:

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

在我的服务项目的 global.asax 中

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

private void RegisterRoutes()
{
    RouteTable.Routes.Add(new ServiceRoute("Cars", new WebServiceHostFactory(), typeof(Cars)));
}

最佳答案

首先,命名空间与服务地址无关,它用于 wsdl 上的目标命名空间并组成 soap 操作,也可用于在端点之间进行消息路由。

aspNetCompatibilityEnabled 将使请求通过 aspnet 管道,然后您可以使用 aspnet url 重写来解决您的问题。

但是您使用的是 dotnet 4.5,那么只需将它放在 Global.asax.cs 上,它比 url 路由模块更容易。

    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)));
    }

Eliminate the .svc in the URL of a WCF 4 service using Routes?与你正在尝试做的事情有关。 这里有关于这个问题的讨论ServiceRoute + WebServiceHostFactory kills WSDL generation? How to create extensionless WCF service with ?wsdl

关于wcf - ServiceRoutes 在 WCF 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19362988/

相关文章:

wcf - 当 SSL 在 IIS 场 (WCF) 前面的代理上时如何使用 Forms Auth?

wcf - 基于 WCF 中的接口(interface)序列化数组的最佳方法是什么?

c# - 使用 ASMX 并模拟方法

.net - 复制到其他计算机时无法在 WcfTestClient 中添加服务

c# - 在 ServiceHost 中运行的 WCF 服务(控制台应用程序)

c# - WCF 日志记录,设置最大文件大小?

wcf - net.tcp 服务对该地址的协议(protocol)不可用

asp.net - 我可以将 ASP.NET Web 服务升级到 WCF 并仍然从 ASP.NET 1.1 调用它吗?

wcf - 如何防止对 Web 服务的调用可见

.net - 如何配置 WCF 服务以在没有 HTTP 绑定(bind)的情况下通过 HTTPS 工作?