服务元素在WCF的web.config文件中的确切作用是什么?我已经看到了WCF服务在没有服务元素的情况下完美运行的实例。
这是一个示例配置文件,我的服务可以从后面的代码调用&脚本(相同/不同的域)
<?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>
答案 0 :(得分:1)
从WCF 4.0开始,该框架引入了默认端点,行为和绑定的概念。这是为了使WCF配置更容易。在您发布的配置文件中,没有已定义的端点或绑定,因此服务将在服务文件的位置创建一个默认端点(即,如果您的服务文件位于C:\inetpub\wwwroot\MyService\MyService.svc
且IIS应用程序是名为MyService
,它将是http://<servername>\MyService\MyService.svc
)。
basicHttpBinding
的开箱即用默认绑定为http
。因此,这为您提供了basicHttpBinding
的默认端点。您仍然可以显式定义端点和绑定,并且您可以定义绑定并将其设置为该配置中使用该绑定的所有服务的默认设置(通过省略name
属性),您还可以更改用于<protocolMapping>
部分<system.serviceModel>
部分中给定传输的绑定。例如,如果您希望默认情况下对所有wsHttpBinding
请求使用http
,则可以执行以下操作:
<protocolMapping>
<add binding="wsHttpBinding" scheme="http"/>
</protocolMapping>
这里有一篇非常好的文章 - A Developer's Introduction to Windows Communication Foundation 4。