Why do WCF endpoints ignore the hosts file? - wcf

I have endpoints defined in a config file with a "fake" domain name. The idea being that this would make deployment easier, since instead of modifying the config files, we simply set the ip address of the "fake" domain name on the enviroments' hosts files. So for example, on my local dev box, I would have an entry in the hosts file
127.0.0.1 fake.domain
and on a user acceptance environment I might have
192.45.34.31 fake.domain
but in my config file I would just have the endpoint
<endpoint address="http://fake.domain/someServiceBase/SomeService.svc" ... />
However, WCF seems to completely ignore the hosts file. I can copy past the address into a browser, and it will come right up, but the WCF client (which is on the local dev box) will give a "host not found" error.
Why does WCF ignore the hosts file?
Edit:
Additional note, everything works if I replace the endpoint address to use the actual ip address directly in the config file, e.g.
<endpoint address="http://127.0.0.1/someServiceBase/SomeService.svc" ... />

Why does WCF ignore the hosts file?
Hmm, I suspect that it isn't. I'd guess that your client is making a connection, but being redirected by the service.
There's an article here that goes into great detail on endpoints:
The Windows Communication Foundation channel infrastructure revolves around the physical address since it’s responsible for receiving incoming messages using a particular transport protocol at a specific location. The Windows Communication Foundation dispatcher, on the other hand, is shielded from such networking details and instead focuses on mapping the incoming message to an endpoint, and ultimately to a method call.

Related

WCF DiscoveryClient returns references to localhost from remote machines

I have an app with a self-hosted WCF service.
My WCF service gets published under the URI "net.tcp://localhost:8004/DocumentService". When I run the service on a remote machine and try to discover the service with the new .NET 4 class DiscoveryClient, the found services all have the URI "net.tcp://localhost:8004/DocumentService" too without any information about the actual machine where the service is hosted.
Obviously this is useless if I want to access the service on the remote machine. But I can't find any reference to the actual remote machine (IP address or server name) in the arguments passed to FindProgressChanged.
Is there a way to get the information about the remote machine or do I have to publish my service with the machine name of the remote machine? Or is DiscoveryClient just broken?
I hope this make sense.
I spent a lot of time investigating this problem. Building base addresses in the code was not acceptable for me, as it implies hardcoding transport scheme and port (the latter, of course, can be stored in a separate config section, but then why not just to use the existing section?). I wanted to have an ability to just configure the base address in config as usual. And it turns out that a base address like <add baseAddress="net.tcp://*:8731/"/> will perfectly work. I think the same is true for programmatic configuration.

Multiple endpoints for single contract in WAS-hosted WCF service?

If one needs to expose multiple endpoints (e.g., one with basicHttpBinding, and another with netTcpBinding) for a single contract in a heterogeneous environment, on a WAS-hosted service, how does one do it?
Everything I've read about WAS configuration of endpoints indicates that endpoint addresses and host base addresses should be left blank, because WAS is supposed to automatically resolve addresses via the path to the .svc file and the protocol.
However, it seems impossible to define an HTTP endpoint and a TCP endpoint for the same contract without explicating the addresses. Every attempt of mine thus far has netted me the coveted "a binding instance has already been associated to listen uri" error.
Ideas?
Thanks
See if this works for you
http://knowledgebaseworld.blogspot.com/2010/06/domain-name-replaced-with-machine-name.html
I was hainvg same issue of "a binding instance has already been associated to listen uri" which get fixed by adding httpGetUrl along with binding address
As far as I understand, the *.svc file is only viable for HTTP protocols, e.g. you can use it for your basicHttpBinding connection. In that case, you're absolutely right - the service's base address and the endpoint's address attribute are ignored - the service URI is defined by server name, optionally port, the virtual directory where the SVC file resides, and the name and extension of the SVC file itself.
So as long as you don't need multiple different HTTP-like protocols and endpoints, that one SVC file should take care of the HTTP traffic.
However, these options do not apply to non-http protocols, like netTcpBinding. In that case, you need to define an endpoint address (possibly as a relative path off an appropriate base address) in your web.config.

WCF host address question

when I setup the wcf service on a web server, I set the end point address as
<endpoint address="http://www.mydomin.com/clientname/happy.svc"
binding="basicHttpBinding"
name="happysvcbasic"
contract="happysvc.Ihappysvc">
</endpoint>
but when type in above address on a browser, I get a different host name, which is the internal server name, such as,
To test this service, you will need to create a client and use it to call the service. You can do this using the svcutil.exe tool from the command line with the following syntax:
svcutil.exe http://internalservername.domain/clientname/happy.svc?wsdl
I tried to add the host/baseaddress tag, but make no difference, what I missed? thanks for help.
When you host via IIS your service in IIS the address is always relative. Assuming you want to achieve something with your endpoint remove the http://
<endpoint address="clientname"
And then your endpoint would be
http://localhost/virtualdirectory/happy.svc/clientname
The use for this is when you are exposing multiple endpoints, as each endpoint has to have a unique address.
See or This for more information.
If you are trying to setup a different dns address for your service you need to change the way you have hosted your website and use host headers.
When you host your WCF service in IIS, you do not get to choose the address, so setting an address= in your <endpoint> is absolutely useless, as is setting base addresses.
When hosting in IIS, the only things that determine your WCF service address are:
the IIS server machine name/IP address, plus possibly a port number
the virtual directory and possibly any subdirectories where your happy.svc file is located
the name of your *.svc file itself, including the .svc extension
So your WCF service address will be something like:
http://yourserver:80/VirtualDirectory/SubDirectory/happy.svc
That's all there is, and you can't change that (at least not now, in WCF 3.5 - it might be different in WCF in .NET 4).
So now: what is your question, really?

How do I host a wcf service on the internet?

This is probably a basic networking issue, but I am new to this stuff and just do not know the answer.
I have written a wcf service and client. I can use one of the http bindings and get the service to work correctly when I put my machine's network IP address as the endpoint address and run the client and server from the same machine. Now, I want to be able to connect to this service from a different machine over the internet. Clearly it does not work when I use my network IP address in this scenario, but simply putting in my router's broadband IP address does not seem to be doing the trick, either. Am I just missing a firewall port that I need to open up, or am I trying to do something that should not be possible?
If you want users from the internet to be able to connect to your service, you'll have to consider a few points:
binding: the lowest common denominator is the basicHttpBinding which is SOAP 1.1 with basically no additional features available - just like ASMX webservices. Just about anyone can connect to that. For more advanced clients, you might also want to expose a wsHttpBinding endpoint on your service
security: how (if at all) do you want to secure access to your web service? Do you have username/password credentials that callers must supply? Check out the WCF Security Guidance for a whole slew of information bits on the various security scenarios
authenticating your service: typically, you should strive to make your service authenticate itself to the rest of the world - this requires a server certificate and enables secured communication (messages signed + encrypted) on the wire
make sure your service endpoint(s) is reachable from the internet, through all firewalls and proxies and everything :-)
Hope that helps a bit!
You need to set up port forwarding on your router. Perhaps someone on ServerFault or SuperUser would be able to help you. Or even a google search now that you know what it's called. The instructions will be different depending on the router. The port you need to forward will be the port you've picked in the WCF config file.
I host WCF services through IIS, but it took me ages to work out how. At the moment I put the files on the webserver and enable websharing on the root folder. Then you can assign them to an appropriate Application Pool in IIS, and add a service reference to any client projects using the URL of the wsdl.
I'm not sure if this is the best way to do it but its the only way I've worked out so far.
Here's the simple solution.
I am assuming that you have made a working WCF application and hosted over the IIS.
The next thing to do is to browse the application from the IIS. It will give you url in the address bar something like:
http://localhost/myservice/service.svc
Next go to www.whatismyip.com. this will give you your system's WAN IP (say, 45.34.56.200).
Replace the URL you got in step 2 with: http://45.34.56.200/myservice/service.svc
Now you can use this URL any where in this world to consume your service.
I found a good Article and it is working fine for me, on the following the Main steps:
1-First you should create WCF Service.
2-add application on IIS and give alias for your virtual directory and set path from your local drive.
3-Make sure your default app pool set to .NET CLR V4.0.
4-test your WCF service is running successfully on localhost.
5-To access the same via LAN (Local Area Network) you must disable Firewall for you Private network.
6- try to use ngrok.com, you will get Temp URL to use via internet to access your LocalHost anywhere.
Then Everything will be fine.
For More Information Check the following Link:
https://www.codeproject.com/Tips/813650/Host-WCF-on-LocalHost-and-access-via-Internet

How can you load balance an IIS 6 hosted WCF Service?

We use BigIP to load balance between our two IIS servers. We recently deployed a WCF service hosted on by IIS 6 onto these two Windows Server 2003R2 servers.
Each server is configured with two host headers: one for the load balancer address, and then a second host header that points only to that server. That way we can reference a specific server in the load balanced group for debugging.
So when we run We immediately got the error:
This collection already contains an address with scheme http. There can be at most one address per scheme in this collection.
Parameter name: item
I did some research and we can implement a filter to tell it to ignore the one of the hosts, but then we cannot access the server from that address.
<serviceHostingEnvironment>
<baseAddressPrefixFilters>
<add prefix="http://domain.com:80"/>
</baseAddressPrefixFilters>
</serviceHostingEnvironment>
What is the best solution in this scenario which would allow us to hit a WCF service via http://domain.com/service.svc and http://server1.domain.com/service.svc?
If we should create our own ServiceFactory as some sites suggest, does anyone have any sample code on this?
Any help is much appreciated.
EDIT: We will need to be able to access the WCF service from either of the two addresses, if at all possible.
Thank you.
On your bigIP Create 2 new virtual servers
http://server1.domain.com/
http://server2.domain.com/
create a pool for each VS with only the specific server in it - so there will be no actual load balancing and access it that way. If you are short on external IP'S you can still use the same IP as your production domain name and just use an irule to direct traffic to the appropriate pool
Hope this helps
The URL it uses is based on the bindings in IIS. Does the website have more than one binding? If it does, or is the WCF service used by multiple sites? If it is, then you are SOL AFAIK. We ran into this issue. Basically, there can be only one IIS binding for HTTP, otherwise it bombs.
Also, here's info on implementing a ServiceHostFactory. That WILL work if it's possible that your WCF service only be accessible through 1 address (unfortunately for us, this was not possible).
When you need to test a specific machine, you could "bypass" the load balancing and ensure the correct host-header is sent to keeep WCF happy by editing the "hosts" file on the machine you're testing from so, for example:
10.0.0.11 through 10.0.0.16 are the six hosts that are in the cluster "cluster.mycompany.local", with a load balanced IP address of 10.0.0.10. When testing you could add a line to the machines hosts file that says "10.0.0.13 cluster.mycompany.local" to be able to hit the third machine in the cluster directly.