【问题标题】:ServiceRoutes not working in WCFServiceRoutes 在 WCF 中不起作用
【发布时间】:2023-04-06 01:42:01
【问题描述】:

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

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

这是我的服务类

[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 的 cmets

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

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

【问题讨论】:

    标签: wcf


    【解决方案1】:

    首先,命名空间与服务地址无关,它用于wsdl上的targetnamespace和组成soap动作,也可以用来做端点之间的消息路由。

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

    但是您使用的是 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

    【讨论】:

    • “首先,命名空间与服务地址无关” - 你上面指的是哪里??
    • 添加这些路由时,这个 global.asax 是在 Web 服务本身还是应用程序中?
    • aspNetCompatibilityEnabled 已启用,但我仍然无法正常工作
    • 当您在 ServiceContract 上使用 Namespace = "localhost:53329" 时,这不会更改地址上的任何内容,它只会更改 WSDL 上的 TargetNamespace。
    • 是的,您需要在应用程序上添加新项目 global.asax.cs。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    相关资源
    最近更新 更多