ServiceRoutes在WCF中不工作
这是我的。当我试图通过ServiceRoutes在WCF中不工作,wcf,Wcf,这是我的。当我试图通过http://localhost:55129/Cars.svc它工作正常 当我尝试http://localhost:55129/Cars它没有。我以为这条路线应该允许这样做,但我显然错过了一些东西 这是我的服务课 [ServiceContract(Name = "Cars", Namespace = "http://localhost:53329", SessionMode = SessionMode.NotAllowed)] public interface ICars
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>
首先,名称空间与服务地址无关,它用于wsdl上的targetnamespace并用于编写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)));
}
这与你正在尝试做的事情有关。
这里有一个关于这个问题的讨论“首先,名称空间与服务地址无关”-您上面提到的是哪里?添加这些路由时,这是web服务本身还是应用程序中的global.asax?aspNetCompatibilityEnabled已经启用,当您使用namespace=“”时,我仍然无法得到这个工作在ServiceContract上,这不会更改地址上的任何内容,只会更改WSDL上的TargetNamespace。是的,您需要在应用程序上添加新项global.asax.cs。
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}
private void RegisterRoutes()
{
RouteTable.Routes.Add(new ServiceRoute("Cars", new WebServiceHostFactory(), typeof(Cars)));
}
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)));
}