15 days Proficient wcf--second day farewell to the trouble config configuration

Source: Internet
Author: User

The regular WCF-based friends will surely know that there is a crazy feeling when your application has a lot of "service references" ... Migrating from one environment to another, you need to change the

Endpoint will be super TMD more, is simply to kill people ... Well, let's take a look at how to minimize the configuration.

One: Streamlined config configuration for service

Just like the code in the previous article, the Config configuration for my service side is as follows:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Configuration>3 <System.ServiceModel>4 <Behaviors>5 <servicebehaviors>6 <Behaviorname= "Mxbehavior">7 <Servicemetadatahttpgetenabled= "true" />8 <ServicedebugIncludeexceptiondetailinfaults= "true" />9 </Behavior>Ten </servicebehaviors> One </Behaviors> A <Services> - <Servicename= "Myservice.homeservice"behaviorconfiguration= "Mxbehavior"> - <EndpointAddress= "Net.tcp://localhost:1920/homeservice"binding= "NetTcpBinding"Contract= "Myservice.ihomeservice"> the <Identity> - <DNSvalue= "localhost" /> - </Identity> - </Endpoint> + <EndpointAddress= "Mex"binding= "mexHttpBinding"Contract= "IMetadataExchange" /> - <Host> + <baseaddresses> A <Addbaseaddress= "Http://localhost:19200/homeservice"/> at </baseaddresses> - </Host> - </Service> - </Services> - </System.ServiceModel> - </Configuration>

Through the above code, you should know that all nodes under System.ServiceModel are WCF-exclusive nodes, all node data will be opened ServiceHost this listener capture, below I can

Find the relevant code to read the config node by ServiceHost the source of the listener.

From above, do you have a feeling that the bottom of the service is also the code dynamically read the node below the config to get the data, that means I can directly write code in the.

Right, so I can put what I think the configuration of things configured, should not be configured to put everything in the code, so my flexibility is not very strong .... Cool, say dry ...

1     classProgram12     {3         Static voidMain (string[] args)4         {5ServiceHost host =NewServiceHost (typeof(Homeservice),NewUri ("Http://localhost:19200/HomeService"));6 7Host. AddServiceEndpoint (typeof(Ihomeservice),NewNetTcpBinding (),"Net.tcp://localhost:1920/homeservice");8 9             //Publish MetadataTenHost. DESCRIPTION.BEHAVIORS.ADD (NewServiceMetadataBehavior () {httpgetenabled =true }); OneHost. AddServiceEndpoint (typeof(IMetadataExchange), metadataexchangebindings.createmexhttpbinding (),"Mex"); A  - host. Open (); -  theConsole.WriteLine ("Service is open ... "); -  - Console.read (); -         } +}

Some people will say, address words must not be written dead, must live, simple ah, I just put the IP address config inside to do not just finished, right.

<Configuration>  <appSettings>    <AddKey= "BaseURL"value= "Http://localhost:19200/HomeService"/>    <AddKey= "Endpoindurl"value= "Net.tcp://localhost:1920/homeservice"/>  </appSettings>
1   classProgram12     {3         Static voidMain (string[] args)4         {5ServiceHost host =NewServiceHost (typeof(Homeservice),NewUri (configurationmanager.appsettings["BaseURL"]));6 7Host. AddServiceEndpoint (typeof(Ihomeservice),NewNetTcpBinding (), configurationmanager.appsettings["Endpoindurl"]);8 9             //Publish MetadataTenHost. DESCRIPTION.BEHAVIORS.ADD (NewServiceMetadataBehavior () {httpgetenabled =true }); OneHost. AddServiceEndpoint (typeof(IMetadataExchange), metadataexchangebindings.createmexhttpbinding (),"Mex"); A  - host. Open (); -  theConsole.WriteLine ("Service is open ... "); -  - Console.read (); -         } +}

Now look at the words, is not much clearer, if you think my code is cumbersome, you can encapsulate into a method, then you can dynamically configure nettcp,basic,ws* and so on ... All right, finish the costume.

Server, let's look at how the client side avoids it.

Second: Streamline the config configuration of the client


As in the previous section, if I use a "service reference", VS will secretly use svcutil.exe to generate a proxy class and a config file, the proxy class is what you see xxxclient ...

The horrible thing is that config will give me some messy things like:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Configuration>3 <System.ServiceModel>4 <Bindings>5 <nettcpbinding>6 <bindingname= "Nettcpbinding_ihomeservice" />7 </nettcpbinding>8 </Bindings>9 <Client>Ten <EndpointAddress= "Net.tcp://localhost:1920/homeservice"binding= "NetTcpBinding" One bindingconfiguration= "Nettcpbinding_ihomeservice"Contract= "Homeservicereference.ihomeservice" A name= "Nettcpbinding_ihomeservice"> - <Identity> - <DNSvalue= "localhost" /> the </Identity> - </Endpoint> - </Client> - </System.ServiceModel> + </Configuration>

As with the server side, if I use code to do away, is not very good??? Can we do that? We have to look at the source of proxy, first you will see that the so-called Proxy is just a succession

A class of self-clientbase, such as.

Above the two pictures, you will find that the final proxy class is through the Channelfactory<tchannel> class to complete the assists, then the words back, since the bottom with the channelfactory<tchannel>

Then why don't I use channelfactory<tchannel> in the code is not better??? So the config is also saved, right, say dry ...

1     Static voidMain (string[] args)2         {3Channelfactory<ihomeservice> factory =NewChannelfactory<ihomeservice> (NewNetTcpBinding (),"Net.tcp://localhost:1920/homeservice");4 5             varChannel =Factory. CreateChannel ();6 7             varresult = Channel. GetLength ("12345");8}

OK, the code is so simple, now is not feel the big Meng ~ ~ ~

15 days Proficient wcf--second day farewell to the trouble config configuration

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.