C# WCF发现:System.UriFormatException使用带有通配符(*)的URI创建ServiceHost
我的目的是: 运行自托管WCF服务,其基本地址在运行时根据可用网卡设置;客户端发现服务并获取地址 先前的答复: 我找到了同样的目的和建议的解决办法 我的问题是: 当我运行主机控制台应用程序时,会在此处引发异常:C# WCF发现:System.UriFormatException使用带有通配符(*)的URI创建ServiceHost,c#,wcf,uri,wildcard,discovery,C#,Wcf,Uri,Wildcard,Discovery,我的目的是: 运行自托管WCF服务,其基本地址在运行时根据可用网卡设置;客户端发现服务并获取地址 先前的答复: 我找到了同样的目的和建议的解决办法 我的问题是: 当我运行主机控制台应用程序时,会在此处引发异常: var host = new ServiceHost(typeof(WcfPingTest), new Uri("http://*:7400/WcfPing")); 异常详细信息:System.UriFormatException:无效URI:无法分析主机名 如果我将*替换为local
var host = new ServiceHost(typeof(WcfPingTest), new Uri("http://*:7400/WcfPing"));
异常详细信息:System.UriFormatException:无效URI:无法分析主机名
如果我将*替换为localhost,它可以工作,但不能满足我的任务,即:从客户端获取真实的ip地址,而不是硬编码的ip地址
我也尝试在app.config文件中执行此操作,但例外情况相同:
我哪里出错了
附加信息:C#代码.NET Framework 4.5
更新: 通配符使用在app.config文件中起作用:
<baseAddresses><add baseAddress="http://*:7400/WcfPing" /></baseAddresses>
另一方面,Uri
构造函数不接受内部带有*char的uriString参数
下一个主题:使用上述源代码,我的客户机在运行时获得uri,但此uri包含我的计算机名称,如
http://MY_COMPUTER_NAME:7400/WcfPing
,国际互联网
http://MY_IP_ADDRESS:7400/WcfPing
是否有任何方法获取实际IP地址?(我有多个NIC用于不同的本地网络)出现异常,因为Uri字符串对Uri无效。 关于配置文件中的通配符,请检查您的配置文件是否类似
<system.serviceModel>
<services>
<service name="Nelibur.ServiceModel.Services.JsonServicePerCall">
<host>
<baseAddresses>
<add baseAddress="http://*:9095/feedback" />
</baseAddresses>
</host>
<endpoint binding="webHttpBinding"
contract="Nelibur.ServiceModel.Contracts.IJsonService" />
</service>
</services>
</system.serviceModel>
你是对的。在配置文件中使用
和var h=new ServiceHost(typeof(WcfPingTest.WcfPingTest))
有效!但是,var-host=new-ServiceHost(typeof(WcfPingTest)、新Uri(“http://*:9095/feedback”)
不起作用。可能uriString不允许通配符。通过以上代码,我的客户可以得到:http://MY_COMPUTER_NAME:9095/feedback
而不是像http://MY_NIC_IP_ADDRESS:9095/feedback
<system.serviceModel>
<services>
<service name="Nelibur.ServiceModel.Services.JsonServicePerCall">
<host>
<baseAddresses>
<add baseAddress="http://*:9095/feedback" />
</baseAddresses>
</host>
<endpoint binding="webHttpBinding"
contract="Nelibur.ServiceModel.Contracts.IJsonService" />
</service>
</services>
</system.serviceModel>
var host = new ServiceHost(typeof(WcfPingTest)