15 days proficient in WCF-the next day farewell to the worried config configuration, wcfconfig

Source: Internet
Author: User

15 days proficient in WCF-the next day farewell to the worried config configuration, wcfconfig

 

Those who often engage in wcf will surely know if there is a crazy feeling when your application has many "service references... You need to change

The endpoint will be much more than tmd, and it's just killing people... Okay. Let's take a look at how to minimize the configuration.

 

1. Simplify the config configuration of the service

Like the code in the previous article, the config configuration of my service side is as follows:

 1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <system.servicemodel> 4 <behaviors> 5 <servicebehaviors> 6 <behavior name="mxbehavior"> 7 <servicemetadata httpgetenabled="true" /> 8 <servicedebug includeexceptiondetailinfaults="true" /> 9 </behavior>10 </servicebehaviors>11 </behaviors>12 <services>13 <service name="myservice.homeservice" behaviorconfiguration="mxbehavior">14 <endpoint address="net.tcp://localhost:1920/homeservice" binding="nettcpbinding" contract="myservice.ihomeservice">15 <identity>16 <dns value="localhost" />17 </identity>18 </endpoint>19 <endpoint address="mex" binding="mexhttpbinding" contract="imetadataexchange" />20 

 

Through the above Code, you should know that all nodes under system. servicemodel are dedicated nodes of wcf, and all node data will be captured when the servicehost listener is enabled. Below I can

Use the source code of the servicehost listener to find the relevant code for reading the config node.

 

 

Through the above, do you have a feeling that the bottom layer of the service also dynamically reads the node below config through code to obtain data, that means I can directly write the code into the code,

Right, so that I can configure what I think is the configuration, and put all the things that should not be configured in the Code, so that my flexibility is not very powerful .... Let's just do it...

1 class Program1 2 {3 static void Main (string [] args) 4 {5 ServiceHost host = new ServiceHost (typeof (HomeService), new Uri ("http: // localhost: 19200/HomeService "); 6 7 host. addServiceEndpoint (typeof (IHomeService), new NetTcpBinding (), "net. tcp: // localhost: 1920/HomeService "); 8 9 // publish metadata 10 host. description. behaviors. add (new ServiceMetadataBehavior () {HttpGetEnabled = true}); 11 host. addServiceEndp Oint (typeof (IMetadataExchange), MetadataExchangeBindings. CreateMexHttpBinding (), "mex"); 12 13 host. Open (); 14 15 Console. WriteLine ("the service is enabled... "); 16 17 Console. Read (); 18} 19}

 

Some people will say that the address cannot be written to death. It must be changed to live. It's easy. I just need to configure the IP address in config, right.

<configuration>  <appSettings>    <add key ="baseurl" value="http://localhost:19200/HomeService"/>    <add key ="endpoindurl" value="net.tcp://localhost:1920/HomeService"/>  </appSettings>
1 class Program1 2 {3 static void Main (string [] args) 4 {5 ServiceHost host = new ServiceHost (typeof (HomeService), new Uri (ConfigurationManager. appSettings ["baseurl"]); 6 7 host. addServiceEndpoint (typeof (IHomeService), new NetTcpBinding (), ConfigurationManager. appSettings ["endpoindurl"]); 8 9 // publish metadata 10 host. description. behaviors. add (new ServiceMetadataBehavior () {HttpGetEnabled = true}); 11 hos T. addServiceEndpoint (typeof (IMetadataExchange), MetadataExchangeBindings. createMexHttpBinding (), "mex"); 12 13 host. open (); 14 15 Console. writeLine ("service enabled... "); 16 17 Console. Read (); 18} 19}

 

If you think my code is cumbersome, You can encapsulate it into a method, and then you can dynamically configure nettcp, basic, ws *, right... Now, finish the service.

Service end. Let's take a look at how the client can avoid it.

 

Ii. Streamline the config configuration of the client


In the example below, if I use the movie service to introduce the movie, vswill use svcutil.exe to generate a proxy class and a config file for us. The proxy class is the xxxclient you see...

The bad thing is that the config will generate some messy things for me, such:

 1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <system.serviceModel> 4 <bindings> 5 <netTcpBinding> 6 <binding name="NetTcpBinding_IHomeService" /> 7 </netTcpBinding> 8 </bindings> 9 <client>10 <endpoint address="net.tcp://localhost:1920/HomeService" binding="netTcpBinding"11 bindingConfiguration="NetTcpBinding_IHomeService" contract="HomeServiceReference.IHomeService"12 name="NetTcpBinding_IHomeService">13 <identity>14 <dns value="localhost" />15 </identity>16 </endpoint>17 </client>18 </system.serviceModel>19 </configuration>

 

Like on the server side, is it really nice if I use code ??? Can it be done? Let's take a look at the proxy source code. First, you will see that the so-called proxy is just an inheritance

A class from clientbase, such.

 

 

In the above two figures, you will find that the final proxy class is assists through the ChannelFactory <TChannel> class. Then again, since the bottom layer uses ChannelFactory <TChannel>,

Why don't I use ChannelFactory <TChannel> In the code ??? In this way, config is saved, right, just do it...

1     static void Main(string[] args)2         {3             ChannelFactory<IHomeService> factory = new ChannelFactory<IHomeService>(new NetTcpBinding(), "net.tcp://localhost:1920/homeservice");4 5             var channel = factory.CreateChannel();6 7             var result = channel.GetLength("12345");8         }

 

Okay, the Code is that simple. Do you feel like you are cute now ~~~

 

Related Article

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.