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

时间:2011-06-06 18:43:25

标签: windows wcf iis windows-services

我正在尝试为我的网络服务更改架构。我需要制作一个WCF服务。我只想制作一个服务,然后在IIS或Windows服务中托管它。这是否可能,使这种WCF服务重用?我该怎么做呢?这种情况是我们的一些客户无权启动Windows服务,但可以在IIS中安装WCF。

提前谢谢。

2 个答案:

答案 0 :(得分:4)

WCF服务只是一个遵循WCF 托管接口的程序集,然后提供允许访问它的客户端接口。

在IIS,Windows服务,WinForm应用程序或控制台应用程序中,托管WCF服务同样。这真的没关系。

客户端界面保持不变,但接口的公开方式可能会因托管方案而异。例如,您可能会在IIS情况下使用一个http绑定,但可能会对Windows服务使用TCP绑定。这些绑定可以在配置文件中定义,因此代码不一定必须更改以适应以某种方式托管。

简而言之,创建WCF服务应该独立于最终托管的方式。但是,为了便于维护,我选择了一种或另一种 - Windows服务或IIS。

答案 1 :(得分:0)

你可以让一个Windows服务托管WCF并公开它上面的所有端点..Http,TCP .. Windows服务比IIS更好,因为IIS本身就是一个进程,然后我们在它上面放置一个VD来托管我们的网站/ WCF。对于Windows服务,它将是一个只适用于WCF的专用线程。我正在共享Windows服务的app.config(详细信息已更改)显示我们如何托管WCF ...希望它有所帮助..

<?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>