【问题标题】:Get Hostname in WCF on IIS在 IIS 上的 WCF 中获取主机名
【发布时间】:2017-12-25 12:05:04
【问题描述】:

根据下图,IIS 上可能有多个 WebSite 和多个服务,

所以我唯一将它们分开的东西是 Hostname ,在其他站点中,兄弟服务可能会一起调用,所以我决定更改 hostname 如果它们不在本地主机上,所以在服务中我尝试了类似的东西这个:

HostName = OperationContext.Current.Channel.LocalAddress.Uri.Host.ToString();

当我通过它的代理调用另一个服务时,我在服务中我 Rehome

 public void ReHome(string hostName)
    {
        if (!string.IsNullOrEmpty(hostName))
        {
            if (this.Endpoint.Address.Uri.DnsSafeHost.ToLower().Equals("localhost"))
            {
                string newAddress = string.Format("{0}://{1}{2}/{3}", Endpoint.Address.Uri.Scheme
                    , hostName, string.IsNullOrEmpty(Endpoint.Address.Uri.Port.ToString()) ? string.Empty : ":" + Endpoint.Address.Uri.Port.ToString()
                    , Endpoint.Address.Uri.AbsolutePath);
                this.Endpoint.Address = new EndpointAddress(newAddress);
            }
        }
    }

在服务中调用示例:

 using (var hisProxy = new HISIntegrationClient("hisIntegrationEndPoint", Internals.SYSTEM))
        {
            hisProxy.ReHome(HostName);
            ....
        }

OperationContext.Current.Channel.LocalAddress.Uri.Host 也给我上面图片中提到的我想要的东西吗?

【问题讨论】:

    标签: c# wcf iis


    【解决方案1】:

    您使用以下代码 sn-p 获取服务器的当前基地址(主机名和端口)

    var baseAddress = OperationContext.Current.Host.BaseAddresses[0].Authority;
    

    【讨论】:

    • 我来看看,它们有什么区别?
    • 不行我在IIS中设置主机名时遇到一些问题,服务的操作将无法访问,我会告诉反馈...
    • 成功了,它给了我主机名,但我的代码OperationContext.Current.Channel.LocalAddress.Uri.Host.ToString() 也确实给了我主机名。
    • 我当然会接受,但是正如您所看到的,您的代码和我的代码有什么区别,它们都给了我真正的主机名,如果您的代码更好,请添加更多解释,谢谢。
    • @Aria 当然我会添加 :)
    猜你喜欢
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多