本文介绍了在WCF元数据发布中自动解析主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个自托管的WCF服务.在服务配置中,我在将端点连接到的BaseAddress中使用了 localhost .尝试使用WCF测试客户端连接到端点时,我没有问题可以连接到端点并使用计算机的名称获取元数据.我遇到的问题是,从元数据生成的客户端在要连接的端点URL中使用localhost.我假设这是因为 localhost 是元数据发布的终结点URL.结果,由于调用计算机上的 localhost 没有运行服务,因此对该服务上的方法的任何调用都将失败.

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.

我想弄清楚的是,服务元数据是否有可能根据调用它的客户端向客户端发布正确的URL.例如,如果我从与服务器位于同一网络上的机器请求服务元数据,则端点应为 net.tcp://MYSERVER:1234/MyEndpoint .如果我是从网络外部的计算机上请求的,则URL应该为 net.tcp://MYSERVER.mydomain.com:1234/MyEndpoint .显然,如果客户端在同一台计算机上,则URL可能是 net.tcp://localhost:1234/MyEndpoint .

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 requesting 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.

这仅仅是默认IMetadataExchange合同中的缺陷吗?元数据是否需要某种原因以非上下文方式发布信息?为了获得我想要的功能,还有另一种方法应该配置我的BaseAddresses吗?

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?

谢谢

迈克

推荐答案

您使用的是哪个.NET版本?如果您使用的是.NET 4.0,则将UseRequestHeadersForMetadataAddressBehavior添加到您的服务主机中:

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.

如果您使用的是.NET 3.5,则有一个添加此行为的修补程序:support.microsoft.com/kb/971842.

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

这篇关于在WCF元数据发布中自动解析主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 23:33