简体   繁体   中英

WCF, replacing 'localhost' with my IP or should I use NetTcpBinding?

while continuing to learn WCF I've completes the Getting Started Tutorial from http://msdn.microsoft.com/en-us/library/ms734712.aspx and created my first server/client application. :)

Yet now I'm wondering, what should I do if I want this application to run on different machines?

from the server app:

Uri baseAddress = new Uri("http://localhost:8000/Server");
ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);

With what should I replace "localhost" if I want this service to be available from outside? I tried replacing it with my IP, but that didn't work.

Any ideas?

Also I heard that HttpBinding is kinda outdated and used mostly when a wcf application needs to communicate with a non-wcf app. while in a wcf-to-wcf communication NetTcpBinding should be used, is that so?

Thanks! :)

You replace it with your public facing IP if you want it to be accessible to the outside world.

WhatIsMyIp.com will give you your public IP.

If you only want it on your network, then you replace it with the IP or host name of your computer on your home network. Usually starts with 192.168.xx

Remember to open ports in your firewall on the host computer. Also, if you are having a computer on the internet trying to connect to your public IP, you will have to learn how to do port forwarding in your router to send the request to the appropriate computer on your local network.

Edit

To better help you visualize how the network structure works you need to understand that your public IP (the one from whatismyip.com) is the IP your ISP assigns to your router (assuming you are behind a router). Your router then assigns the computers on your network their own local IPs, usually starting with 192.168.xx If your client application tries to connect to your public IP, your router needs to know what to do with the request. It has to have a rule that says requests on that port need to forward to a specific computer on your network (the one running your host app).

If you are not behind a router then all you need to worry about is your firewall.

Edit 2

You are new to networking so something you must understand is that you cannot get around port forwarding for applications listening on a server behind a router. Study the following diagram:


(source: clear-cloud.com )

This is the common structure of a household network. Now study this diagram with IP addresses.

http://www.codetunnel.com/networkdiagram.jpg

These are made up IPs, but you get the idea. Your router is assigned an IP by your Internet Service Provider. Your router then assigns computers on your network their own IPs. When a request comes into your public IP, it hits your router. Your router receives the request from your client app and does nothing with it because it does not have any software waiting for a request. That is why they call routers hardware firewalls, requests from the internet cannot make it to a potentially vulnerable PC on your network.

The only way to make requests find their way to the right PC is to tell the router how to do that. Most routers have a configuration interface with a port forwarding section for you to do this. An example would be this linksys router configuration page:


(source: rhinosoft.com )

Ext Port is the port the router should listen for requests that come in on. You can specify a range of ports, or just one port usually.

Protocol is the type of request to listen for. If you don't know the difference between UDP and TCP then that is something you'll have to research as it's beyond the scope of this answer. I'm sure if you are reading a good WCF text book then you will learn about these protocols soon enough. For now just remember that most of your beginning connections are TCP. It won't hurt if you forward both either.

IP Address is the local IP of the computer that the router should forward the request to. Notice that in this linksys screenshot it already fills in the first 3 segments. This is because it already knows the first part of your network's local IP addresses, since it assigned them in the first place.

Then just check the box for enable to turn on the forwarding rule. Your router will now send connections from the outside world on the port or port range you specify to the corresponding PC on your network. Different brands of routers have different configuration pages, but they should be pretty similar and straight forward.

Making your users who have both the client and server app and want to set it up have to forward ports is a giant pain in the butt. That's why most applications have one central server and only pass out client applications to the users.

Note: Remember that only the host application that is listening for a request needs to have ports forwarded to it if it's behind a router. The client applications won't know the difference, you will just put in a public facing IP.

  1. The first option is to set up a central hosting application somewhere. So basically, you set up one hosting app on a server with all the network configuration (port forwarding) set up the way you need. Then all the client apps connect to that one server and the server relays information back and forth between them.


(source: uel.ac.uk )

HttpBinding is mostly there for asmx compatibility etc (I think it's replaced with WSHttpBinding). The binding depends on the use look here for different bindings.

Also, you should not have to change anything. to call it from outside replace localhost with your ip like you did (as specified by @Chevex), but make sure your firewall is off/ports are unblocked.

As for which binding you should use, it depends on how the service is exposed and what features it will require. Http is nice because it is easily exposed over ports that are usually open in a firewall (80, 443). NetTcp is supposed to be faster, but it really depends on the application.

With regard to what you should change localhost to, the first thing I would mention is that the baseAddress parameter is not required. Thus, you can new up your service host with just the Type:

ServiceHost selfHost = new ServiceHost(typeof(CalculatorService));

The configuration of your services should be in a config file, in the ServiceModel element

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM