簡體   English   中英

在 IIS 和 Windows 服務中托管相同的 WCF 服務

[英]Hosting the same WCF Service in IIS and in Windows Service

我正在嘗試決定更改我的 web 服務的架構。 我需要做一個 WCF 服務。 我只想提供一項服務,然后將其托管在 IIS 或 Windows 服務中。 這是否可能,使這種 WCF 服務的重用? 我將如何 go 這樣做? 場景是我們的一些客戶無權啟動 Windows 服務,但可以在 IIS 中安裝 WCF。

先感謝您。

WCF 服務只是一個遵守 WCF托管接口的程序集,然后提供允許訪問它的客戶端接口。

托管 WCF 服務同樣發生在 IIS、Windows 服務、WinForm 應用程序或控制台應用程序中。 真的沒關系。

客戶端接口保持不變,盡管接口的公開方式可能會根據托管方案而改變。 例如,您可能會在 IIS 案例中使用 http 綁定之一,但可能會使用 TCP 綁定為 ZAEAZ063489CE3AAB9404 服務。 這些綁定可以在配置文件中定義,因此不必更改代碼以適應以一種或另一種方式托管。

簡而言之,創建 WCF 服務應該獨立於它最終將如何托管。 不過,為了便於您進行維護,我會選擇其中一個 - Windows 服務或 IIS。

you could have a windows service host the WCF and expose all end points on it..Http,TCP.. Windows service is better than IIS because IIS is a process in itself and then we place upon it a VD to host our website/WCF .As for the Windows Service,it will be one dedicated thread catering only to the WCF.I am sharing the app.config of windows service (details changed) to show how we have hosted WCF...hope it helps..

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel"
          switchValue="Off" propagateActivity="true" >
        <listeners>
          <add name="SERVICE_MONITOR" type="System.Diagnostics.XmlWriterTraceListener"
               initializeData="MyApp_MONITOR.svclog" />
        </listeners>
      </source>      
      <source name="MyApp_TRACE" switchValue="All" >
        <listeners>
          <add name="MyApp_TRACE_LISTENER" type="System.Diagnostics.XmlWriterTraceListener"                                         
               initializeData="MyApp_TRACE.svclog" />
        </listeners>
      </source>
    </sources>
    <trace autoflush="true" />
  </system.diagnostics> 

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="OverAllServiceBehavior">
          <serviceSecurityAudit 
            auditLogLocation="Application" 
            serviceAuthorizationAuditLevel="Failure" 
            messageAuthenticationAuditLevel="Failure" 
            suppressAuditFailure="true" />          
          <serviceDebug includeExceptionDetailInFaults="True" />
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
          <serviceThrottling maxConcurrentCalls="10000" maxConcurrentSessions="10000"

          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
          <serviceCredentials>
            <userNameAuthentication 
              userNamePasswordValidationMode="Custom" 
              customUserNamePasswordValidatorType="MyAppHost.Authenticate, MyAppHost"/>
            <serviceCertificate findValue="MyApp_MESSAGE" storeLocation="LocalMachine"
                                storeName="My" x509FindType="FindBySubjectName" />            
            <clientCertificate>
              <authentication 
                certificateValidationMode="PeerTrust" 
                trustedStoreLocation="LocalMachine" />
            </clientCertificate>
          </serviceCredentials>         
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="OverAllEndPointBehavior" />
      </endpointBehaviors>
    </behaviors>

    <bindings>
      <basicHttpBinding>
        <binding name="ServiceBasicHttpEndPointBinding" closeTimeout="00:00:59"
                 openTimeout="00:00:59"

                 messageEncoding="Text"

          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"

                        maxNameTableCharCount="2147483647" />
          <security mode="Message">
            <message clientCredentialType="Certificate"/>
          </security>
        </binding>       
      </basicHttpBinding>
      <wsHttpBinding>
        <binding name="ServiceWSHttpEndPointBinding" closeTimeout="00:00:59"
                 openTimeout="00:00:59"


          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"

                        maxNameTableCharCount="2147483647" />
          <security mode="TransportWithMessageCredential">
            <transport clientCredentialType="None" />
            <message clientCredentialType="Certificate"/>
          </security>          
        </binding>
      </wsHttpBinding>
      <netTcpBinding>
        <binding name="ServiceTCPEndPointBinding" maxBufferSize="2147483647"

          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"

                        maxNameTableCharCount="2147483647" />
          <security mode="TransportWithMessageCredential">
            <transport 
              clientCredentialType="Certificate" 
              protectionLevel="EncryptAndSign" />
            <message clientCredentialType="UserName" algorithmSuite="TripleDes"/>
          </security>
        </binding>
      </netTcpBinding>
    </bindings>

    <services>
      <service behaviorConfiguration="OverAllServiceBehavior"
               name="MiddleWare.ServiceClasses.ServiceClass">

        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://127.0.0.1:15010/ServiceTCPEndPointMEX"/>
            <add baseAddress="http://127.0.0.1:15020/ServiceHttpEndPointMEX"/>
            <add baseAddress="https://127.0.0.1:15030/ServiceWSHttpEndPointMEX"/>            
          </baseAddresses>
        </host>

        <endpoint address="net.tcp://127.0.0.1:15040/ServiceTCPEndPoint"


                  contract="MiddleWare.ServiceContracts.IServiceContract" />

        <endpoint address="http://127.0.0.1:15050/ServiceBasicHttpEndPoint"


                  contract="MiddleWare.ServiceContracts.IServiceContract"/>

        <endpoint address="https://127.0.0.1:15060/ServiceWSHttpEndPoint"


                  contract="MiddleWare.ServiceContracts.IServiceContract"/>

        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

        <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />

      </service>
    </services>
  </system.serviceModel>
  <appSettings>
    <add key="UserName" value="USER"/>
    <add key="Password" value="PASSWORD"/>
  </appSettings>
</configuration>

暫無
暫無

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

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