开发者

Auto-resolving a hostname in WCF Metadata Publishing

开发者 https://www.devze.com 2023-02-02 07:29 出处:网络
I am running a self-hosted WCF service.In the service configuration, I am using localhost in my BaseAddresses that I hook my endpoints to.When trying to connect to an endpoint using the WCF test clien

I am running a self-hosted WCF service. In the service configuration, I am using localhost in my BaseAddresses that I hook my endpoints to. When trying to connect to an endpoint using the WCF test client, I have no problem connecting to the endpoint and getting the metadata using the machine's name. The problem that I run into is that the client that is generated from metadata uses localhost in the endpoint URLs it wants to connect to. I'm assuming that this is because localhost is the endpoint URL published by metadata. As a result, any calls to the methods on the service will fail since localhost on the calling machine isn't running the service.

What I would like to figure out is if it is possible for the service metadata to publish the proper URL to a client depending on the client who is calling it. For example, if I was requesting the service metadata from a machine on the same network as the server the endpoint should be net.tcp://MYSERVER:1234/MyEndpoint. If I was requesti开发者_运维百科ng it from a machine outside the network, the URL should be net.tcp://MYSERVER.mydomain.com:1234/MyEndpoint. And obviously if the client was on the same machine, THEN the URL could be net.tcp://localhost:1234/MyEndpoint.

Is this just a flaw in the default IMetadataExchange contract? Is there some reason the metadata needs to publish the information in a non-contextual way? Is there another way I should be configuring my BaseAddresses in order to get the functionality I want?

Thanks,

Mike


What .NET version are you using? If you're using .NET 4.0, add the UseRequestHeadersForMetadataAddressBehavior to your service host:

UseRequestHeadersForMetadataAddressBehavior urh = 
    new UseRequestHeadersForMetadataAddressBehavior();
serviceHost.Description.Behaviors.Add(urh);

Obviously, this needs to be done prior to opening the service host.

If you're using .NET 3.5, there's a hotfix that adds this behavior: support.microsoft.com/kb/971842.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号