在 WCF 元数据发布中自动解析主机名

发布于 2024-10-09 21:55:06 字数 730 浏览 0 评论 0原文

我正在运行自托管 WCF 服务。在服务配置中,我在连接端点的 BaseAddresses 中使用 localhost。当尝试使用 WCF 测试客户端连接到端点时,我可以毫无问题地连接到端点并使用计算机名称获取元数据。我遇到的问题是,从元数据生成的客户端在它想要连接的端点 URL 中使用 localhost。我假设这是因为 localhost 是元数据发布的端点 URL。因此,对服务上的方法的任何调用都将失败,因为调用计算机上的 localhost 未运行该服务。

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

这只是默认 IMetadataExchange 合约中的一个缺陷吗?元数据需要以非上下文方式发布信息是否有某种原因?我是否应该通过另一种方式配置我的 BaseAddresses 以获得我想要的功能?

谢谢,

迈克

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

财迷小姐 2024-10-16 21:55:06

您使用什么 .NET 版本?如果您使用的是 .NET 4.0,请将 UseRequestHeadersForMetadataAddressBehavior 添加到您的服务主机:

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

显然,这需要在打开服务主机之前完成。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文