[Solution] WCF (1) Quick Start, solutionwcf
Windows Communication Foundation (WCF) is a series of application frameworks developed by Microsoft to support data Communication. It can be translated as a Windows Communication development platform. Integrates the existing. net Remoting, WebService, and Socket mechanisms for windows communications, and integrates HTTP and FTP-related technologies. Is the best practice for developing distributed applications on Windows.
Install: the. net framework 3.0 has built-in WCF.
Since the WCF Service cannot exist in isolation, it must be hosted in a running process. We call the process that carries the WCF Service as the host. This section describes two types of service hosting services, and bind an endpoint to the WCF Service by Code and Config:
- Self-hosting (*. exe)
- Iisboarding (w3wp.exe)
- Config
Self-boarding
1. Create a project
- Service:A console application is used to host the wcf Service. This project references the System. ServiceMode assembly. (Most of the implementations of the WCF framework and API definitions are in this Assembly)
- Client:A console application that calls services. This project references the System. ServiceMode assembly. (The generated proxy class inherits from this Assembly)
2. Create a contract)
WCF includes four types of contracts: service contract, data contract, message contract, and error contract. The service contract is used to declare all operations of the Service.
To make an interface a contract, you must add a feature label to it.
using System.ServiceModel;namespace Service{ [ServiceContract] public interface ICar { [OperationContract] string Run(int distance); }}
3. Implement services
Implement the interface method.
Namespace Service {public class CarService: ICar {public string Run (int distance) {return "traveled" + distance ;}}}
4. start boarding
WCF uses the endpoint communication mode. Endpoints Include Address, Binding, Contract, or ABC.
Address: specify the location of the wcf
Binging: Specifies the transmission protocol. (Tcp http msmq, etc)
Contract: Specifies the Behavior of the service.
Using System; using System. serviceModel; namespace Service {class Program {static void Main (string [] args) {using (var host = new ServiceHost (typeof (CarService ))) {// specify the endpoint of the service, respectively specify Contract, Binding, Address host. addServiceEndpoint (typeof (ICar), new BasicHttpBinding (), "http: // localhost: 10000/Car"); host. open (); Console. writeLine ("service started successfully"); Console. read ();}}}}
(Managed employee ID starts service.exe)
The service and boarding are successful. How can we call it. In fact, this method cannot be called. The address referenced by the client is the metadata of the service. Therefore, we need to expose the metadata of the service.
Using System; using System. serviceModel; using System. serviceModel. description; namespace Service {class Program {static void Main (string [] args) {using (var host = new ServiceHost (typeof (CarService) {if (host. description. behaviors. find <ServiceMetadataBehavior> () = null) {// metadata of the service allows the Get method to obtain var behavior = new ServiceMetadataBehavior {HttpGetEnabled = true from the Url below, httpGetUrl = new Uri (" http://localhost:10000/Car /Metadata ")}; host. description. behaviors. add (behavior);} // specify the endpoint of the service, respectively specifying Contract, Binding, Address host. addServiceEndpoint (typeof (ICar), new BasicHttpBinding ()," http://localhost:10000/Car "); Host. opened + = (o, e) => Console. writeLine ("service started successfully"); host. open (); Console. read ();}}}}
5. service call
There are two methods to call a service. VS references the service to generate a proxy class, and the Code defines the proxy class (ensure that the Endpoint is completely consistent ).
Code custom proxy class:
Using System; using System. serviceModel; namespace Client {class Program {static void Main (string [] args) {// consistent endpoint using (var channelFactory = new ChannelFactory <ICar> (new BasicHttpBinding (), "http: // localhost: 10000/Car") {// manually create a proxy class var proxy = channelFactory. createChannel (); Console. writeLine (proxy. run (3);} Console. read ();}}}
Here, the ICar (Contract) is not necessarily the same as the ICar of the wcf Service (the namespace, the Assembly name). You only need to use the same method signature. (Because Contract still requires corresponding features)
using System.ServiceModel;namespace Client{ [ServiceContract] interface ICar { [OperationContract] string Run(int distance); }}
Run client.exe on the service.exebase before the Administrator runs.
IIS boarding
1. Create a project
2. File structure
3. svc File
The svc file is equivalent to an asmx file. It specifies a specific Service through a mandatory Command Service. For example, Service = "WcfService. Service1"
In vs, the browser views the svc file. The svc address is the service address. Svc address +? Wsdl is the address of the service description.
4. Service call
Similar to self-Hosted Services, you only need to change the Address to the svc Address.
Note: When you create an svc file, vs automatically modifies the Web. config file. (Used to specify the metadata exposure behavior of the Service)
<System. serviceModel> <behaviors> <serviceBehaviors> <behavior name = ""> // specify the default behavior <serviceMetadata httpGetEnabled = "true" httpsGetEnabled = "true"/> </behavior> </serviceBehaviors> </behaviors> </system. serviceModel>
Config
Basically, the above Code is used to implement WCF. In fact, WCF is very flexible and generally uses the Config method. Or Config + Code (will take effect ).
Two types of wcf configurations are involved: service publishing and service calling.
Release:
<system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="meta"> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:10001/Car/Metadata"/> </behavior> </serviceBehaviors> </behaviors> <services> <service name="Service.CarService" behaviorConfiguration="meta"> <endpoint address="http://localhost:10001/Car" binding="wsHttpBinding" contract="Service.ICar"></endpoint> <endpoint address="http://localhost:10001/Car2" binding="wsHttpBinding" contract="Service.ICar"></endpoint> </service> </services> </system.serviceModel>
Call:
<system.serviceModel> <client> <endpoint name="car" address="http://localhost:10001/Car" binding="wsHttpBinding" contract="Client.ICar"></endpoint> </client> </system.serviceModel>
When the caller uses the configuration method, the code is changed to the following method. (In the configuration, the name is used to distinguish between different endpoints, and the corresponding name is used in the code to create a Channel stack Channel)
using System;using System.ServiceModel;namespace Client{ class Program { static void Main(string[] args) { using (var cf = new ChannelFactory<ICar>("car")) { Console.WriteLine(cf.CreateChannel().Run(3)); Console.Read(); } } }}
The above content outlines the two methods of wcf Code and config. In actual development, you can generate a proxy class through the reference service of vs, and configure the wcf Service tool to modify *. config. This article will not go into detail.
Code download: Waiting for sorting
Author: Never, C
Link: http://www.cnblogs.com/neverc/p/4682394.html