简体   繁体   English

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

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

I am trying to decide on an architecture for a change in my web service.我正在尝试决定更改我的 web 服务的架构。 I need to make a WCF service.我需要做一个 WCF 服务。 I want to make only one service and then host it either in the IIS or in a Windows service.我只想提供一项服务,然后将其托管在 IIS 或 Windows 服务中。 Is this even possible, making this kind of reuse of a WCF Service?这是否可能,使这种 WCF 服务的重用? How would I go about doing this?我将如何 go 这样做? The scenario is that some of our customers do not have access to start a Windows service but can install a WCF in the IIS.场景是我们的一些客户无权启动 Windows 服务,但可以在 IIS 中安装 WCF。

Thank you in advance.先感谢您。

A WCF service is simply an assembly that abides by the WCF hosting interface and then provides a client interface that allows it to be accessed. WCF 服务只是一个遵守 WCF托管接口的程序集,然后提供允许访问它的客户端接口。

Hosting a WCF service occurs equally in IIS, Windows service, WinForm application, or a console application.托管 WCF 服务同样发生在 IIS、Windows 服务、WinForm 应用程序或控制台应用程序中。 It truly doesn't matter.真的没关系。

The client interface remains unchanged, although how the interface is exposed might change depending on hosting scenario.客户端接口保持不变,尽管接口的公开方式可能会根据托管方案而改变。 For example, you'll probably use one the http bindings in the IIS case, but might use TCP binding for Windows services.例如,您可能会在 IIS 案例中使用 http 绑定之一,但可能会使用 TCP 绑定为 ZAEAZ063489CE3AAB9404 服务。 These bindings can be defined in the config file, so the code doesn't necessarily have to change to accommodate being hosted one way or the other.这些绑定可以在配置文件中定义,因此不必更改代码以适应以一种或另一种方式托管。

In short, creating the WCF service should be independent of how it will eventually be hosted.简而言之,创建 WCF 服务应该独立于它最终将如何托管。 For ease of maintenance on your part, though, I'd pick one or the other - Windows service or IIS.不过,为了便于您进行维护,我会选择其中一个 - 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.. 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