简体   繁体   中英

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. I need to make a WCF service. I want to make only one service and then host it either in the IIS or in a Windows service. Is this even possible, making this kind of reuse of a WCF Service? How would I go about doing this? 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.

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.

Hosting a WCF service occurs equally in IIS, Windows service, WinForm application, or a console application. 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. 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. For ease of maintenance on your part, though, I'd pick one or the other - Windows service or 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM