After the service contract and the implementation are written, the relevant services need to be advertised, and host is required to host it for the client to invoke.
There are two ways to host a service, one through configuration files and one for configuration through code. The previous chapter has introduced a code approach to erecting services.
Of course, the format of the configuration file is still very flexible and widely used.
This article specifically describes how WCF is configured through configuration files. First, the configuration file function
You need to configure the endpoint of the service, set up related binding protocols and service contracts, and limit their specific behavior. Second, configuration file structure
The main configuration includes service nodes, binding and behavior nodes, and some other nodes that are not discussed here for the time being.
<?xml version= "1.0" encoding= "Utf-8"?> <configuration> <!--deploy the service library project, the contents of the configuration file must be added to the host's app.config file In System.Configuration does not support configuration files for libraries. --> <system.serviceModel> <!--Configure Service node start--> <services> <!--Configure a service where you can specify a service name--&G
T
<service name= "Secondwcfsample.service1" > </service> </services> <!--Configure Service node end--> <!--Configuring binding nodes start--> <bindings> <basicHttpBinding></basicHttpBinding> <mexhttp Binding></mexhttpbinding> <wsHttpBinding></wsHttpBinding> </bindings> <!--configuration bindings
Node End--> <!--Configuration behavior node start--> <behaviors> <serviceBehaviors> <behavior> </behavior> </serviceBehaviors> </behaviors> <!--Configuration behavior node end--> <!--routing Set start--> <routing> <filters> <filter name= "Filter1 filtertype=" ActIon "/> </filters> </routing> <!--routing Setup end--> </system.serviceModel> </conf Iguration>
iii. Specific configuration instructions
1, service node
Attribute Description:
Name-Specifies the service to which this services configuration is addressed, the fully qualified name (namespace. Type name) of the service class that implements some contract, ServiceHost after loading a service, Go to the bottom of the configuration file. Configuration with the name attribute matching the service
Behaviorconfiguration-Specifies the name of the next, which specifically gives the service a certain set of behaviors, such as whether the services allow identity simulations –>
Case:
<!--property settings, Name: The fully qualified name of the service, namespace + implementation class name, behaviorconfiguration behavior configuration, behavior name of the set, concrete visible behaviors node-->
< Service name= "Secondwcfsample.service1" behaviorconfiguration= "Basicbehavior" >
</service>
2, endpoint node
Define endpoint, specify three elements ABC, address (addresses), bindings (Binding), and agreements (Contract).
Case:
<!--define endpoint, specify address, bindings (binding), and contract (contract)-->
<!--attribute Description:
address-server-side addresses, Relative to the baseaddress relative address, if NULL is baseaddress, can also be set to an absolute address
binding-binding protocol, the system of a protocol, basichttpbinding,mexhttpbinding, Wshttpbinding
Contract-Protocol fully qualified name (namespace. Type name) name
-the Endpointconfigurationname in the constructor method of the client proxy class corresponds to this name
Bindingconfiguration-Specifies the specific settings for the client binding, pointing to the name-->
<endpoint address = "" of the same type binding under the <bindings> element binding= "Wshttpbinding" contract= "Secondwcfsample.iservice1" >
<!--service identification. When deploying, the following identity elements should be deleted or replaced to reflect the identity used to run the deployed service. After the deletion, WCF automatically infers the corresponding identity. -->
<identity>
<dns value= "localhost"/>
</identity>
</endpoint>
3, Binding
Bindings specify the communication mechanism to use when talking to endpoints and indicate how to connect to endpoints.
The binding contains the following elements:
The protocol stack determines the security, reliability, and context flow settings for messages sent to endpoints.
transport determines the underlying transport protocol that is used when sending messages to an endpoint, such as TCP or HTTP.
encoding determines the network encoding used for messages sent to endpoints, for example, text/xml, binary, or message transfer optimization (MTOM).
Specific binding, different functions, set the properties of different, specific reference to the following
binding |
configuration element |
Description |
BasicHttpBinding |
<basicHttpBinding> |
A binding that is suitable for communication with Web services that conform to ws-basic profile, such as services based on ASP.net Web Services (ASMX). This binding uses HTTP as the transport protocol and uses text/xml as the default message encoding. |
Wshttpbinding |
<wsHttpBinding> |
A secure and interoperable binding, suitable for non-duplex service engagements. |
Wsdualhttpbinding |
<wsdualhttpbinding > |
A secure and interoperable binding that applies to duplex service agreements or communication through the SOAP medium. |
WSFederationHttpBinding |
<wsFederationHttpBinding> |
A secure, interoperable binding that supports the Ws-federation protocol, enabling organizations in the federation to efficiently authenticate and authorize users. |
NetTcpBinding |
<netTcpBinding> |
A secure and optimized binding that applies to communication across computers between WCF applications. |
NetNamedPipeBinding |
<netNamedPipeBinding> |
A secure, reliable, and optimized binding that applies to communication on computers between WCF applications. |
NetMsmqBinding |
<netMsmqBinding> |
A queued binding that applies to cross computer communication between WCF applications. |
NetPeerTcpBinding |
<netPeerTcpBinding> |
A binding that supports multiple computer secure communications. |
MsmqIntegrationBinding |
<msmqintegrationbinding > |
A binding that is appropriate for cross computer communication between WCF applications and existing Message Queuing applications. |
Basichttpcontextbinding |
<basichttpcontextbinding > |
A binding that is appropriate for communication with a WEB service that conforms to Ws-basic profile and allows the exchange of contexts using an HTTP Cookie. |
Nettcpcontextbinding |
<netTcpContextBinding> |
A secure and optimized binding for communication across computers between WCF applications that allow the use of SOAP headers to exchange contexts. |
WebHttpBinding |
<webhttpbinding > |
A binding that can be used to configure endpoints for WCF Web services that are exposed through HTTP requests rather than SOAP messages. |
Wshttpcontextbinding |
<wsHttpContextBinding> |
A secure and interoperable binding for non-duplex service contracts that allow the use of SOAP headers to exchange contexts. |
<!--Configure binding node start-->
<bindings>
<basicHttpBinding>
<binding name= "Basic" Transfermode= "streamed" messageencoding= "Text" allowcookies= "false" textencoding= "" ></binding>
</ basichttpbinding>
<netTcpBinding>
</netTcpBinding>
<customBinding>
</ custombinding>
</bindings>
<!--configuring binding nodes End-->
4, Behavior node
Behavior sets the run-time behavior of the service, such as whether to allow clients to obtain metadata for the service, route modifications, Operation access permission settings, service restriction mechanisms, timeout for service requests, and so on.
The specific meaning of behavior is visible:
Introduction to WCF Introductory Tutorial (i)
Http://www.cnblogs.com/yank/p/3653160.html
Case:
<!--Configuration behavior node start-->
<behaviors>
<serviceBehaviors>
<behavior name= " Basicbehavior >
<!--to avoid leaking metadata information, set the
following value to False before deployment and delete the metadata endpoint above -->
<servicemetadata Httpgetenabled= "true"/>
<!--to receive fault exception details for debugging, set the
following value to True. Set to False before deployment to
avoid leaking exception information-->
<servicedebug includeexceptiondetailinfaults= "false"/>
</ behavior>
</serviceBehaviors>
</behaviors>
<!--configuration behavior node end-->
The specific child node features are described below:
element |
Description |
<dataContractSerializer> |
Contains the configuration data for the DataContractSerializer. |
<persistenceProvider> |
Specifies the type of persistence provider implementation to use and the time-out value for the persistence operation. |
<routingExtension> of <serviceBehavior> |
Provides run-time access to the routing service to allow dynamic modification of the route configuration. |
<serviceAuthentication> |
Provides a workflow configuration element that establishes a transmission, message, or initiator validity at the service level. |
<serviceAuthorization> |
element specifies the settings that are used to grant access to a service operation. |
<serviceCredentials> |
Specifies the credentials to use to authenticate the service and the settings related to client credential validation. |
<serviceDebug> |
Specifies the debugging and help information features of the Windows communication Foundation (WCF) service. |
<serviceDiscovery> |
Specifies the discovery of service endpoints. |
<serviceMetadata> |
Specify the publication and related information for the service meta data. |
<serviceSecurityAudit> |
Specifies the settings that are used to enable security event auditing during a service operation. |
<serviceThrottling> |
Specify a restriction mechanism for WCF services. |
<serviceTimeouts> |
Specifies a timeout for the service. |
<workflowRuntime> |
Specifies the settings used to host a WorkflowRuntime instance of a WCF service based on the workflow. |
Detail visible:
http://msdn.microsoft.com/zh-cn/library/aa967282 (v=vs.100). aspx Four, complete instance
<?xml version= "1.0" encoding= "Utf-8"?> <configuration> <system.web> <compilation debug= "true" /> </system.web> <!--when deploying a service library project, the contents of the configuration file must be added to the app.config file of the host. System.Configuration does not support configuration files for libraries. --> <system.serviceModel> <!--Configure Service node start--> <services> <!--Configure a service where you can specify a service name--&G
T <!--property setting, Name: Fully qualified name of service, namespace + implementation class name, behaviorconfiguration behavior configuration, behavior name of set, visible behaviors node--> <service
Name= "Secondwcfsample.service1" behaviorconfiguration= "Basicbehavior" > <!--host address-->
v. Other configurations
For additional configuration items, the more in-depth friends can refer to the MSDN Detail documentation
http://msdn.microsoft.com/zh-cn/library/ms731354 (v=vs.100). aspx