简体   繁体   中英

Get Hostname in WCF on IIS

Based on below pic, there may be several WebSite on IIS with several services,

在此处输入图片说明

So the only thing which I have separate them from together is Hostname , in the other site sibling services may call together so I have decided to change hostname if they are not on localhost so in service I tried something like this:

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

and in service when I am calling another service by it's proxy I 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);
            }
        }
    }

call example in a service:

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

so is OperationContext.Current.Channel.LocalAddress.Uri.Host give me what I want that mentioned in above pic?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM