Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize WCF Service metadata Exchange programming

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "WCF service metadata exchange programming how to achieve", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "WCF service metadata exchange programming how to achieve" it!

The former configuration is simple and fast, while the latter is relatively complex. But programming allows the code to control or set the information for metadata exchange at run time. So it's more flexible. Let's take a look at how to implement the configuration of the service original data exchange through code.

HTTP-GET programming implementation of WCF service metadata exchange:

A reference to the namespace must be added. Using System.ServiceModel.Description; the class and interface information we manipulate on the service metadata is defined in this namespace. The specific code for implementing HTTP-GET is as follows:

ServiceMetadataBehavior metadataBehavior; / / defines the service behavior variable, metadataBehavior = host.Description.Behaviors.Find (); / / gets the host behavior list if (metadataBehavior = = null) / / if there is no service original data exchange behavior, instantiate add the service original data exchange behavior {metadataBehavior = new ServiceMetadataBehavior (); Uri httpAddress = new Uri ("http://localhost:8001/"); metadataBehavior.HttpGetUrl = httpAddress; metadataBehavior.HttpGetEnabled = true / / set HTTP mode host.Description.Behaviors.Add (metadataBehavior);}

The first step is to get the list information of the service behavior. If it is not set, we instantiate the service original data exchange behavior and set the http mode to be available. Host.Description.Behaviors.Add (metadataBehavior); the act of adding a hosting service.

WS-* programming implementation of WCF service metadata exchange:

Here, the code of metadata exchange of HTTP, TCP and IPC is realized respectively. Slightly different from the http-get approach, we need to instantiate our own binding elements and bindings, passing * * as parameters to the host host instance. The specific implementation code is as follows:

/ / 2 programmatically implement ws* original data exchange / / Life three binding node classes BindingElement tcpBindingElement = new TcpTransportBindingElement (); BindingElement httpBindingElement = new HttpsTransportBindingElement (); BindingElement pipeBindingElement = new NamedPipeTransportBindingElement (); / / instantiate instances of generic binding classes Binding tcpBinding = new CustomBinding (tcpBindingElement); Binding httpBinding = new CustomBinding (httpBindingElement); Binding pipeBinding = new CustomBinding (pipeBindingElement); / / Uri tcpBaseAddress = new Uri ("net.tcp://localhost:9001/") Uri httpBaseAddress = new Uri ("http://localhost:9002/"); Uri pipeBaseAddress = new Uri (" net.pipe://localhost/ "); host.AddServiceEndpoint (typeof (WCFService.IWCFService), new NetTcpBinding (), tcpBaseAddress); host.AddServiceEndpoint (typeof (WCFService.IWCFService), new WSHttpBinding (), httpBaseAddress); host.AddServiceEndpoint (typeof (WCFService.IWCFService), new NetNamedPipeBinding (), pipeBaseAddress); / / ServiceMetadataBehavior metadataBehavior;// defines service behavior variables, metadataBehavior = host.Description.Behaviors.Find () / / get host behavior list if (metadataBehavior = = null) / / if there is no service original data exchange behavior, instantiate add service original data exchange behavior {metadataBehavior = new ServiceMetadataBehavior (); host.Description.Behaviors.Add (metadataBehavior);} / / if no mex node is available, you can use the following code to determine and add the mex node host.AddServiceEndpoint (typeof (IMetadataExchange), tcpBinding, "mex") Host.AddServiceEndpoint (typeof (IMetadataExchange), httpBinding, "mex"); host.AddServiceEndpoint (typeof (IMetadataExchange), pipeBinding, "mex"); Thank you for reading, the above is the content of "how to achieve WCF service metadata exchange programming". After the study of this article, I believe you have a deeper understanding of how to achieve WCF service metadata exchange programming, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report