15 days proficient in WCF-three tips you need to know on the fifth day

Source: Internet
Author: User

15 days proficient in WCF-three tips you need to know on the fifth day

1. A service is a set of endpoints.

When developing wcf, you may have noticed that a service can publish multiple endpoints. This is indeed the case, it is called "a service is a collection of endpoints ".

For example, a Common Service publishes a service endpoint and a metadata endpoint, right...

After careful consideration, this issue is quite interesting. Since a service can publish multiple endpoints, and I also know that there are many bindings in wcf, these bindings correspond to many transmission methods, isn't it?

I can publish a service using multiple Protocol methods, such as nettcp, basic, msmqbinding, and udp, right? Isn't that fun?

Non-. net program, you can call my basic. If the other party is a. net Program, can it call my nettcp, right... Of course, wcf is omnipotent. This is a powerful cow in history.

Forced framework, awesome to death, has forced programmers to simply modify a few configurations to achieve completely different results... Now, I will use both nettcp and basic to publish services at the same time.

Let's witness the miracle...

Service:

1 using System; 2 using System. runtime. serialization; 3 using System. serviceModel; 4 using System. serviceModel. channels; 5 using System. threading; 6 7 namespace MyService 8 {9 public class HomeService: IHomeService10 {11 public Student Update (Student message) 12 {13 return new Student () {Name = ""}; 14} 15} 16 17 [DataContract] 18 public class Student19 {20 [DataMember] 21 public string Name {get; set ;} 22 23 [DataMember] 24 public int Age {get; set ;}25} 26}

Host:

 1     class Program1 2     { 3         static void Main(string[] args) 4         { 5             ServiceHost host = new ServiceHost(typeof(HomeService), new Uri("http://192.168.1.105:1920")); 6  7             host.AddServiceEndpoint(typeof(IHomeService), new BasicHttpBinding(), "HomeServie"); 8  9             host.AddServiceEndpoint(typeof(IHomeService), new NetTcpBinding(), "net.tcp://192.168.1.105:1921/HomeServieTcp");10 11             host.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled = true });12 13             host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");14 15             host.Open();16 17             Console.Read();18         }19     }

Client:

1 class Program 2 {3 static void Main (string [] args) 4 {5 // basic mode 6 ChannelFactory <IHomeService> factory = new ChannelFactory <IHomeService> (new BasicHttpBinding (), 7 new EndpointAddress ("http: // 192.168.1.105: 1920/HomeServie"); 8 9 var client = factory. createChannel (); 10 11 var result = client. update (new Student () {}); 12 13 14 // nettcp mode 15 factory = new ChannelFactory <IHomeService> (new NetTcpBinding (), 16 new EndpointAddress ("net. tcp: // 192.168.1.105: 1921/HomeServieTcp "); 17 18 client = factory. createChannel (); 19 20 result = client. update (new Student () {}); 21} 22}

 

Through the above Code, have you found that on the client side, I can call both the basic method and the nettcp method, does this technique feel that wcf is incredibly powerful ???

 

Ii. Host hosts with multiple services

We know that there are many methods of hosting in wcf, including iis, windowservice, and simple and convenient console. By default, the most common method is a service and a host,

But what about it ??? In fact, a host can carry multiple services, and it looks like it is fun. If you have 10 servcie servers, now you only need to use one console host to host them.

I will show it to you.

Service:

 1     namespace MyService 2     { 3         [ServiceContract] 4         public interface IHomeService 5         { 6             [OperationContract] 7             Student Update(Student message); 8         } 9 10         [ServiceContract]11         public interface IFlyService12         {13             [OperationContract]14             Student Fly(Student stu);15         }16     }

Host:

1 class Program1 2 {3 static void Main (string [] args) 4 {5 // first: This is Home service 6 ServiceHost host = new ServiceHost (typeof (HomeService ), new Uri (" http://192.168.1.105:1920 "); 7 host. addServiceEndpoint (typeof (IHomeService), new BasicHttpBinding (), "HomeServie"); 8 host. description. behaviors. add (new ServiceMetadataBehavior () {HttpGetEnabled = true}); 9 host. addServiceEndpoint (typeof (IMetadataExchange), MetadataExchangeBindings. createMexHttpBinding (), "mex"); 10 host. open (); 11 12 Console. writeLine ("Home Service enabled .... "); 13 14 // first: This is the Fly service 15 var host2 = new ServiceHost (typeof (FlyService), new Uri (" http://192.168.1.105:1930 "); 16 host2.AddServiceEndpoint (typeof (IFlyService), new BasicHttpBinding ()," FlyServie "); 17 host2.Description. behaviors. add (new ServiceMetadataBehavior () {HttpGetEnabled = true}); 18 host2.AddServiceEndpoint (typeof (IMetadataExchange), MetadataExchangeBindings. createMexHttpBinding (), "mex"); 19 host2.Open (); 20 21 Console. writeLine ("Fly service enabled .... "); 22 23 Console. Read (); 24} 25}

Have you seen that the two services are now enabled? It seems that this method is quite cool. Otherwise, you need to enable two hosts. In this case, my procedures will be streamlined... Right ..

 

3. Tcp Port Sharing

It sounds like everyone knows that Port Sharing doesn't mean that two programs share a port, right? Normally, we will definitely think this is not possible. Actually? In Wcf, we can still play

Is a PortSharingEnabled thing !!! If the ports can be shared, can we open up fewer ports for our service? This is also convenient for us to manage the service

... It's fun.

 

As you can see, both of my hosts use port 1920, and now I really turn it on .... Okay, I have mentioned all three techniques. I think you can connect to them more or less in actual wcf development.

I hope it will be useful to you ~~~~

 

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.