简体   繁体   中英

role of service element in web.config file for a WCF

what's the exact role of service element in web.config file for a WCF? I have seen instances where WCF services work perfectly without service element.

Here's a sample config file, whose service I can call from code behind & script (same/different domain)

 <?xml version="1.0"?>
 <configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
 </system.web>

 <system.serviceModel>    

<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/>

<!--Calling from different domain -->
  <standardEndpoints>
  <webScriptEndpoint>
    <standardEndpoint name="" crossDomainScriptAccessEnabled="true">
  </standardEndpoint>
  </webScriptEndpoint>
</standardEndpoints>

<behaviors>
  <endpointBehaviors>
    <behavior name="EndPointBehavior">
      <enableWebScript />
    </behavior>
  </endpointBehaviors>
  </behaviors>   

 </system.serviceModel>

 <system.webServer>
 <modules runAllManagedModulesForAllRequests="true"/>
 <directoryBrowse enabled="true"/>
 </system.webServer>

 </configuration>

Beginning with WCF 4.0, the framework introduced the concepts of default endpoints, behaviors and bindings. This was to make WCF configuration easier. In the config file you posted, there are no defined endpoints or bindings, so the service will create a default endpoint at the location of the service file (ie, if you have the service file at C:\\inetpub\\wwwroot\\MyService\\MyService.svc and the IIS Application is named MyService , it would be http://<servername>\\MyService\\MyService.svc ).

The out of the box default binding is basicHttpBinding for http . So this gives you a default endpoint with basicHttpBinding . You can still explicitly define endpoints and bindings, and you can define a binding and set it to be the default for all services in that config that use that binding (by omitting the name attribute), and you can also change the binding used for a given transport in <protocolMapping> section in the <system.serviceModel> section. For example, if you wanted to use wsHttpBinding for all http requests by default, you could do this:

<protocolMapping>
  <add binding="wsHttpBinding" scheme="http"/>
</protocolMapping>

There's a very good article that covers this here - A Developer's Introduction to Windows Communication Foundation 4 .

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