IIS 上的 WCF 路由服务
Posted
技术标签:
【中文标题】IIS 上的 WCF 路由服务【英文标题】:WCF Routing service on IIS 【发布时间】:2020-12-30 00:57:56 【问题描述】:我想在 IIS 上创建路由 WCF 服务,它将在 IIS 上调用我的另外两个服务:
JobWCF1.evgeny.net JobWCF2.evgeny.ne JobWCFRouter.evgeny.net如果我在浏览器中调用它们,第一个和第二个服务效果很好:
http://jobwcf1.evgeny.net/JobWCF.svc/Test
http://jobwcf2.evgeny.net/JobWCF.svc/Test
但是当我通过路由服务调用时,它不起作用(HTTP 404)(在浏览器中):
http://jobwcfrouter.evgeny.net/JobWCF.svc/Test
路由器服务的web.config:
<system.serviceModel>
<services>
<service name="System.ServiceModel.Routing.RoutingService" behaviorConfiguration="MyBehavior">
<host>
<baseAddresses>
<add baseAddress="http://JobWCFRouter.evgeny.net/JobWCF.svc"/>
</baseAddresses>
</host>
<!-- work through http in browser -->
<endpoint address=""
behaviorConfiguration="web"
binding="webHttpBinding"
bindingConfiguration="HttpBinding"
/>
</service>
</services>
<client>
<endpoint address="http://JobWCF1.local.net/JobWCF.svc" binding="webHttpBinding"
contract="*" name="myEndpoint1" />
<endpoint address="http://JobWCF2.local.net/JobWCF.svc" binding="webHttpBinding"
contract="*" name="myEndpoint2" />
</client>
<routing>
<filters>
<filter name="myEndpointFilter" filterType="MatchAll" />
</filters>
<filterTables>
<filterTable name="RoutingTable">
<add filterName="myEndpointFilter" endpointName="myEndpoint1" backupList="BackUps" />
</filterTable>
</filterTables>
<backupLists>
<backupList name="BackUps">
<add endpointName="myEndpoint2"/>
</backupList>
</backupLists>
</routing>
<bindings>
<webHttpBinding>
<binding name="HttpBinding" sendTimeout="00:15:00" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" crossDomainScriptAccessEnabled="true">
<readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
<security mode="None"/>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="MyBehavior">
<routing filterTableName="RoutingTable" />
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
另外,在路由器服务中,我有文件管理器 Router.svc,代码在哪里:
<%@ ServiceHost Language="C#" Debug="true"
Service="System.ServiceModel.Routing.
RoutingService, System.ServiceModel.Routing,
version=4.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35"
%>
我做错了什么?
【问题讨论】:
404
可能与 IIS 无法解析您尝试调用的 RoutingService
上的 Test
操作有关。从您共享的配置中,我不确定您是如何设置的?
您好,问题解决了吗?如果您认为我的回复对您有帮助,您可以将其标记为答案。
【参考方案1】:
这可能是因为您没有启动 RoutingService 实例。如果使用自托管,我们需要使用ServiceHost来启动RoutingService:
ServiceHost RoutingService= new ServiceHost(typeof(RoutingService));
RoutingService.Open();
如果部署在IIS,需要添加svc文件,在svc文件中添加如下代码:
<%@ ServiceHost Service="System.ServiceModel.Routing.RoutingService, System.ServiceModel.Routing, version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
在 IIS 中部署不需要宿主节点,路由的基地址由 IIS 中部署的绑定决定。
这是我的 web.config:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.7.2" />
<httpRuntime targetFramework="4.7.2"/>
</system.web>
<system.serviceModel>
<services>
<service name="System.ServiceModel.Routing.RoutingService" behaviorConfiguration="svbhv">
<endpoint name="routing_ep1" address="rout1" contract="System.ServiceModel.Routing.IRequestReplyRouter" binding="basicHttpBinding" ></endpoint>
<endpoint name="routing_ep2" address="rout2" contract="System.ServiceModel.Routing.IRequestReplyRouter" binding="basicHttpBinding" ></endpoint>
<endpoint name="routing_ep3" address="rout3" contract="System.ServiceModel.Routing.IRequestReplyRouter" binding="basicHttpBinding" ></endpoint>
</service>
</services>
<client>
<endpoint name="calc_sv1" address="http://127.0.0.1:3211" binding="basicHttpBinding" contract="*"></endpoint>
<endpoint name="calc_sv2" address="http://127.0.0.1:3212" binding="basicHttpBinding" contract="*"></endpoint>
</client>
<routing>
<filters>
<filter name="f1" filterType="EndpointName" filterData="routing_ep1"/>
<filter name="f2" filterType="EndpointName" filterData="routing_ep2"/>
<filter name="f3" filterType="EndpointName" filterData="routing_ep3"/>
</filters>
<filterTables>
<filterTable name="myfilter_tb">
<add filterName="f1" endpointName="calc_sv1"/>
<add filterName="f2" endpointName="calc_sv1"/>
<add filterName="f3" endpointName="calc_sv2"/>
</filterTable>
</filterTables>
</routing>
<behaviors>
<serviceBehaviors>
<behavior name="svbhv">
<serviceMetadata httpGetEnabled="true"/>
<routing filterTableName="myfilter_tb"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
下面是路由服务的基地址:
我们可以通过浏览器访问路由服务:
如果问题仍然存在,请随时告诉我。
【讨论】:
我可以通过 webHttpBinding 调用我的端点还是不可能?端点服务应该有哪些设置? 可以使用webHttpBinding。以上是关于IIS 上的 WCF 路由服务的主要内容,如果未能解决你的问题,请参考以下文章
为 http 和 https 端点配置带有路由 (global.asax) 的 WCF 4
在路由路径上公开属于 MVC 应用程序中区域的 WCF 服务