簡體   English   中英

IIS 上的 WCF 路由服務

[英]WCF Routing service on IIS

我想在 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>

同樣在路由器服務中,我有 filer Router.svc 代碼:

<%@ ServiceHost Language="C#" Debug="true" 
    Service="System.ServiceModel.Routing.
    RoutingService, System.ServiceModel.Routing, 
    version=4.0.0.0, Culture=neutral, 
    PublicKeyToken=31bf3856ad364e35" 
%> 

我究竟做錯了什么?

這可能是因為您沒有啟動 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中部署不需要host節點,路由的基地址由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>

下面是路由服務的基地址:

在此處輸入圖片說明

我們可以通過瀏覽器訪問路由服務:

在此處輸入圖片說明

如果問題仍然存在,請隨時告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM