WCF Technical Profiling 29: Invoking WCF services in different ways

Source: Internet
Author: User

We have two typical WCF invocation methods: Import the published service metadata generation service proxy related code and configuration via SvcUtil.exe (or add a Web reference), and create service proxy objects by channelfactory<tchannel>. In this article, we use a unique way to invoke the service. Essentially, we can achieve normal service invocation as long as we are able to create a matching endpoint for service connected. In the WCF client metadata architecture system, the metadataexchangeclient is used to get the metadata of the service, and the metadataimporter is used to import the acquired metadata into the ServiceEndpoint object. In this case, we will use these two components to define a simple example of a unique service invocation that can help readers to further deepen their understanding of the WCF metadata framework system.

This article supporting source code: http://www.bianceng.net/dotnet/201212/656.htm

We still use the familiar example of computing services, which is the corresponding service contract for the service, the definition of the service type, and the configuration used to host the service.

   1:using System.ServiceModel;
2:namespace Artech.ServiceInvocationViaMetadata.Contracts
3: {
4: [ServiceContract (Namespace = "http://www.artech.com/")]
5: Public interface ICalculator
6: {
7: [OperationContract]
8: Double Add (double x, double y);
9: }
10:}

Service Type:

   1:using System.ServiceModel;
2:using Artech.ServiceInvocationViaMetadata.Contracts;
3:
4:namespace Artech.ServiceInvocationViaMetadata.Services
5: {
6: Public class Calculatorservice:icalculator
7: {
8: Public double Add (double x, double y)
9: {
Ten: Return x + y;
One: }
: }
13:}

Configuration:

1: <?xml version= "1.0" encoding= "Utf-8"?>
2: <configuration>
3: <system.serviceModel>
4: <behaviors>
5: <serviceBehaviors>
6: <behavior name= "Mexbehavior" >
7: <servicemetadata/>
8: </behavior>
9: </serviceBehaviors>
: </behaviors>
One: <services>
<service behaviorconfiguration= "Mexbehavior" Name= "Artech.ServiceInvocationViaMetadata.Services.Calcula Torservice ">
<endpoint address= "Http://127.0.0.1:3721/calculatorservice" binding= "ws2007httpbinding" A Rtech. ServiceInvocationViaMetadata.Contracts.ICalculator "/>
<endpoint address= "Http://127.0.0.1:3721/calculatorservice/mex" binding= "mexHttpBinding" IMetadataExchange "/>
: </service>
: </services>
: </system.serviceModel>
: </configuration>

From the configuration above, we can see that the service metadata is released through the Ws-mex mode, and the address of the post and the Mex binding are: Http://127.0.0.1:3721/calculatorservice/mex and mexhttpbinding respectively.

Next, we can invoke the service in the following way. We first create the MetadataExchangeClient object and use it to get the Metadataset object that contains the metadata, and use that object to create the Wsdlimporter object. Next, we add the service contract based on the ICalculator interface to the Wsdlimporter list of known contracts, and invoke the Importallendpoints method to get the imported ServiceEndpoint list. Finally, the Channelfactory<icalculator> object is created based on the exported ServiceEndpoint object and a service proxy is created to make the service invocation.

1:sing System;
2:using System.ServiceModel;
3:using System.ServiceModel.Description;
4:using System.Xml;
5:using Artech.ServiceInvocationViaMetadata.Contracts;
6:namespace Artech.ServiceInvocationViaMetadata.Client
7: {
8:class Program
9: {
10:static void Main (string[] args)
11: {
12:metadataexchangeclient metadataexchangeclient = new MetadataExchangeClient (metadataexchangebindings.create mexHttpBinding ());
13:metadataset metadata = Metadataexchangeclient.getmetadata (new EndpointAddress ("Http://127.0.0.1:3721/calcu Latorservice/mex "));
14:wsdlimporter wsdlimporter = new Wsdlimporter (metadata);
15://Add known contract type
16:contractdescription contract = Contractdescription.getcontract (typeof (ICalculator));
17:wsdlimporter.knowncontracts.add (new XmlQualifiedName (contract). Name, contract. Namespace), contract);
18:serviceendpointcollection endpoints = wsdlimporter.importallendpoints ();
19:using (channelfactory<icalculator> ChannelFactory = new Channelfactory<icalculator> (endpoints[ 0]))
20: {
21:icalculator calculator = Channelfactory.createchannel ();
22:using (Calculator as IDisposable)
23: {
24:try
25: {
26:console.writeline ("x + y = {2} when x = {0} and y = {1}", 1, 2, calculator.) ADD (1, 2));
27:}
28:catch (TimeoutException)
29: {
(Calculator as Icommunicationobject). Abort ();
31:throw;
32:}
33:catch (communicationexception)
34: {
: (Calculator as Icommunicationobject). Abort ();
36:throw;
37:}
38:}
39:}
40:console.read ();
41:}
42:}
43:}

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.