在IIS上的WCF中获取主机名 [英] Get Hostname in WCF on IIS

查看:198
本文介绍了在IIS上的WCF中获取主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据以下图片,IIS上可能有多个WebSite提供多种服务,

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

所以我唯一将它们从一起分开的是主机名,在其他网站兄弟服务可以一起调用所以我有决定更改主机名如果他们不在localhost上,那么在服务中我试过这样的事情:

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();

当我通过它的代理呼叫另一项服务时我在服务中 Rehome

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);
            }
        }
    }

服务中的呼叫示例:

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

所以 OperationContext.Current.Channel.LocalAddress.Uri.Host 给我我想要的上面的照片?

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

推荐答案

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

You get the current base address of server(hostname and port) using following code snippet

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

这篇关于在IIS上的WCF中获取主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆