.net WCF 正在使用计算机名而不是 IP 地址并且无法解析
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6863464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WCF is using the computer name instead of the IP address and cannot be resolved
提问by baileyswalk
I have a WCF service that works fine on the LAN but when trying to access it from outside the service reference fails.
我有一个 WCF 服务在 LAN 上运行良好,但是当尝试从服务引用外部访问它时失败。
My WCF service is hosted on a win2k3 box that is using a static IP no domain.
我的 WCF 服务托管在使用静态 IP 无域的 win2k3 机器上。
采纳答案by baileyswalk
I found an answer to this after some digging - here is what I have found hopefully it can save someone else some time and bother.
经过一番挖掘,我找到了一个答案 - 这是我发现的,希望它可以为其他人节省一些时间和麻烦。
1.) Add the IP to the endpoint address & add a host name with the base IP address like so:
1.) 将 IP 添加到端点地址并添加带有基本 IP 地址的主机名,如下所示:
<endpoint
address="http://xx.xx.xx.xx/ServiceApp/Service.svc"
binding="basicHttpBinding" contract="IService">
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://xx.xx.xx.xx/ServiceApp/" />
</baseAddresses>
</host>
This used to be enough to make my service reference work but the disco file started being returned with the computer name instead of the ip (I think this was after updating to .NET 4.0).
这曾经足以使我的服务参考工作,但迪斯科文件开始返回计算机名称而不是 ip(我认为这是在更新到 .NET 4.0 之后)。
2.) If you have a domain name (www.myDomain.com) then add this to the host header in IIS.
2.) 如果您有域名 (www.myDomain.com),则将其添加到 IIS 中的主机标头。
3.) Add the IP address & computer name to the clients hosts file (easy fix not always possible to get all of your clients to add this to their host file however)
3.) 将 IP 地址和计算机名称添加到客户端主机文件中(简单的修复并非总是可以让所有客户端将其添加到他们的主机文件中)
4.) The BEST SOLUTION I found was to implement the ServiceHosts Factory Attribute as per "Timetheos" post here: http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/c7fd51a2-773e-41d4-95a0-244e925597fe
4.) 我发现的最佳解决方案是按照此处的“Timetheos”帖子实现 ServiceHosts 工厂属性:http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/c7fd51a2-773e-41d4 -95a0-244e925597fe
This worked well for me as I could test develop & debug my service library locally and then use a service app to deploy the service to my dev server and didn't have to change any configuration files after publishing it.
这对我来说很有效,因为我可以在本地测试开发和调试我的服务库,然后使用服务应用程序将服务部署到我的开发服务器,并且在发布后不必更改任何配置文件。
This whole process was a total nightmare, and I wouldnt wish it upon anyone so if you are in the same situation and need anymore info on the above points just get in touch!
整个过程完全是一场噩梦,我不希望任何人发生这种情况,所以如果您处于相同的情况并且需要有关上述几点的更多信息,请联系!
回答by Jorge Barrera
This is what worked for me. In config file
这对我有用。在配置文件中
< serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
< / system.serviceModel >
If it is set to false, I was getting that crazy computername substitution.
如果它设置为 false,我就会得到那个疯狂的计算机名替换。
multipleSiteBindingsEnabled="true"seems to be all that I have to do for this to work as it should.
multipleSiteBindingsEnabled="true"似乎是我必须做的一切才能使其正常工作。
回答by Dan Vulpe
I was looking into an approach to reuse the Host header from HTTP request. In my opinion this should work in development as in production.
我正在研究一种从 HTTP 请求中重用 Host 标头的方法。在我看来,这应该在开发中和生产中一样有效。
It turns out is as easy as:
事实证明,这很简单:
<behaviors>
<serviceBehaviors>
<behavior name="...">
...
<useRequestHeadersForMetadataAddress />
</behavior>
</serviceBehaviors>
</behaviors>
This way if the WSDL endpoint is accessible by a client this ensures that all the associated wsdl/xsd resources will be accessible with the same base url.
这样,如果客户端可以访问 WSDL 端点,就可以确保所有关联的 wsdl/xsd 资源都可以使用相同的基本 URL 访问。
回答by RiverTrader
You can use an asterisk * (wildcard) in the place of LocalHost or machine name in the base url like so:
您可以在基本 url 中使用星号 *(通配符)代替 LocalHost 或机器名称,如下所示:
<add baseAddress="net.tcp://*:4502/WxWcfService_01" />
回答by Dipak
Set service end point and httpgeturl like this.
像这样设置服务端点和 httpgeturl。
<services>
<service behaviorConfiguration="serviceBehaviour" name="Demo.Service.MultiEndPointsService">
<endpoint address="http://192.168.1.2/Demo.Service/MultiEndPointsService.svc/basic" binding="basicHttpBinding" bindingConfiguration="basicBinding" contract="Demo.Service.MultiEndPointsService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehaviour">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://192.168.1.2/Demo.Service/MultiEndPointsService.svc/basic"/>
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>

