Concept that WCF has to be clear

Source: Internet
Author: User
Tags msmq

1. Address)

In WCF, the address is specified as a uniform resource identifier (URI). It is used to identify the destination of message sending and receiving, andCommunication ProtocolAndLocation PathIt consists of two parts. For example, http: // 192.168.1.1: 8000/indicates that the communication protocol is HTTP and the location path is port 8000 of 192.168.1.1. Location Path is easy to understand, that is, IP and port number. For beginners, they often ignore the communication protocols included in them. In addition to HTTP, they can also be specified:

TCP address
Transmission over TCP, in the form of: net. TCP: // localhost: 8000/

IPC address
Use Net. Pipe for transmission, in the form of: net. Pipe: // localhost/

MSMQ address
Use the Microsoft Message Queue mechanism for transmission, in the form of: net. MSMQ: // localhost/

Peer network address
Use Net. P2P for transmission, in the form of: net. P2P: // localhost/

 

Let's take a look at the actual example in the configuration file:
<Host>
<Baseaddresses>
<Add
Baseaddress = "http: // localhost: 8731 /"
/>
</Baseaddresses>
</Host>
<Endpoint
Address = "http: // localhost: 8731/service" binding = "basichttpbinding" Contract = "wcf_address_config.iservice1"> </Endpoint>

You can also write the relative path:

<Host>

<Baseaddresses>

<Add
Baseaddress = "http: // localhost: 8731 /"
/>

</Baseaddresses>
</Host>
<Endpoint
Address = "service1" binding = "basichttpbinding" Contract = "wcf_address_config.iservice1"> </Endpoint>

 

 

In programming, how do I set the address?


Endpointaddress address =
New endpointaddress ("http: // 127.0.0.1: 2136/service1 ");

Binding binding
= New basichttpbinding ();
WCF. iservice1 Service
= New WCF. service1client (binding, address );

2. Binding)

The binding element represents a specific part of the binding, such as the implementation of transmission protocol, encoding, infrastructure-level protocols (such as WS-reliablemessaging), and any other elements of the communication stack. Binding is also one of the three elements that make up a service endpoint. The WCF base library provides many optional pre-bindings:

Basichttpbinding

A binding is applicable to communication with Web services that comply with WS-basic profile (for example, services based on ASP. NET web services (asmx. This binding uses HTTP as the transmission protocol and text/XML as the default message encoding. The transmission protocol is HTTP/HTTPS encoded in the format of text and MTOM.

Wshttpbinding

A secure and interoperable binding is suitable for non-duplex service conventions. The transmission protocol is HTTP/HTTPS encoded in the format of text and MTOM.

Wsdualhttpbinding

A secure and interoperable binding applies to duplex service protocols or communication through soap media. Transmission Protocol: HTTP encoding format: Text, MTOM

Nettcpbinding

A secure and optimized binding applies to cross-Computer Communication Between WCF applications. The transmission protocol is: TCP encoding format: Binary

Netpeertcpbinding

A binding that supports secure communication between multiple computers. The transfer protocol is: P2P encoding format: Binary

3. Contract)

In WCF, there are four types of contracts. 1) service contracts: a service contract associates multiple coherent operations to form a single function element. A contract can define service-level settings, if the service namespace, the corresponding callback contract and other such settings. 2) Data contract: the data type used by the Service must be described in the metadata so that other parties can interact with the service. The description of the data type is referred to as the data contract, these types can be used in any part of the message. if the service is a simple type, there is no need to display and use the data contract. 4) Data contract: You can associate an error contract with a service operation to indicate that an error indicating a call may be returned. An operation may have zero or multiple associated errors, these errors are SOA errors modeled as exceptions in the programming model. 5) Message contract: Message contract describes the Message format, it declares whether the message element should be included in the message header or in the message body, and what level of security should be applied to any element of the message. A contract is also one of the three elements that make up a service endpoint.

4. endpoint)

An endpoint consists of three elements. An endpoint is used to send or receive messages. An endpoint is equivalent to a public interface such as a service. Each Service can have one or more endpoints, because each service has only one address, all endpoints of a service share one address. The configuration or programming of endpoints does not belong to the programming of business logic, therefore, the WCF design separates the definition of the endpoint and the specific implementation of the contract.

5. Metadata

The metadata of the Service describes the features of the service. external entities need to understand these features for the service to communicate. The metadata published by the service includes the XML schema file and the WSDL document. After metadata is enabled, WCF automatically generates service metadata by checking services and endpoints. To publish service metadata, you must display the startup metadata behavior. In WCF, you can set a dedicated endpoint for metadata.

Configuration: <servicemetadata
Httpgetenabled = "true" httpgeturl = "http: // localhost: 8731/service"/>

6. Host

The service must host an application that controls the Service's survival period in a process. The service can be self-hosted or managed by the existing boarding process. From the perspective of internal implementation, A service host process can contain one or more application domains, and each application domain can theoretically be placed in any service host, each service host can have any context, and each context can have 0 or 1 and service example.

 

Finally, we will provide a WCF instance in msdn: Pay attention to referenceSystem. servicemodel. dll

using System;using System.ServiceModel;using System.ServiceModel.Description;namespace Microsoft.ServiceModel.Samples{    // Define a service contract.    [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]    public interface ICalculator    {        [OperationContract]        double Add(double n1, double n2);        [OperationContract]        double Subtract(double n1, double n2);        [OperationContract]        double Multiply(double n1, double n2);        [OperationContract]        double Divide(double n1, double n2);    }    // Service class that implements the service contract.    // Added code to write output to the console window.    public class CalculatorService : ICalculator    {        public double Add(double n1, double n2)        {            double result = n1 + n2;            Console.WriteLine("Received Add({0},{1})", n1, n2);            Console.WriteLine("Return: {0}", result);            return result;        }        public double Subtract(double n1, double n2)        {            double result = n1 - n2;            Console.WriteLine("Received Subtract({0},{1})", n1, n2);            Console.WriteLine("Return: {0}", result);            return result;        }        public double Multiply(double n1, double n2)        {            double result = n1 * n2;            Console.WriteLine("Received Multiply({0},{1})", n1, n2);            Console.WriteLine("Return: {0}", result);            return result;        }        public double Divide(double n1, double n2)        {            double result = n1 / n2;            Console.WriteLine("Received Divide({0},{1})", n1, n2);            Console.WriteLine("Return: {0}", result);            return result;        }    }    class Program    {        static void Main(string[] args)        {            // Step 1 of the address configuration procedure: Create a URI to serve as the base address.            Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");            // Step 2 of the hosting procedure: Create ServiceHost            ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);            try            {                // Step 3 of the hosting procedure: Add a service endpoint.                selfHost.AddServiceEndpoint(                    typeof(ICalculator),                    new WSHttpBinding(),                    "CalculatorService");                // Step 4 of the hosting procedure: Enable metadata exchange.                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();                smb.HttpGetEnabled = true;                selfHost.Description.Behaviors.Add(smb);                // Step 5 of the hosting procedure: Start (and then stop) the service.                selfHost.Open();                Console.WriteLine("The service is ready.");                Console.WriteLine("Press <ENTER> to terminate service.");                Console.WriteLine();                Console.ReadLine();                // Close the ServiceHostBase to shutdown the service.                selfHost.Close();            }            catch (CommunicationException ce)            {                Console.WriteLine("An exception occurred: {0}", ce.Message);                selfHost.Abort();            }        }    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.