以编程方式访问WCF EndPoint URL [英] Programmatically access WCF EndPoint URL

查看:117
本文介绍了以编程方式访问WCF EndPoint URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个IIS托管的WCF服务(配置如这篇博客文章 ...我需要知道配置端点的URL是什么。例如,给定此配置:

I have an IIS hosted WCF service (configured as described in this blog post ... I need to know what the configured endpoint's URL is. For example, given this configuration:

  <system.serviceModel>
    <services>
      <service behaviorConfiguration="mexBehavior" name="Sc.Neo.Bus.Server.MessageProxy">
        <endpoint address="http://localhost:9000/MessageProxy.svc" binding="basicHttpBinding"
          contract="Sc.Neo.Bus.IMessageProxy" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="BusWeb.Service1Behavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
        <behavior name="mexBehavior">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

我希望能够获得价值' http:/ /lhosthost:9000/MessageProxy.svc '进入Web应用程序的onstart事件中的字符串变量。

I'd like to be able to get the value 'http://localhost:9000/MessageProxy.svc' into a string variable in the web application's onstart event.

推荐答案

好的,所以简短的回答是你不能。更长的答案是,你可以,有点工作。

OK so the short answer is "you can't". The longer answer is you can, kind of, with a bit of work.

如果你在IIS之外托管这个,那么加载配置部分本身和解析它,因为您可以将其转换为WCF配置类:

If you're hosting this outside of IIS it's actually reasonable to load the configuration section itself and parse it, as you can cast it to the WCF configuration class:

ServiceModelSectionGroup serviceModelGroup =
      cfg.GetSectionGroup("system.serviceModel") 
            as ServiceModelSectionGroup;

有点乱,但确实有效。问题来自IIS - IIS托管服务从IIS继承其地址,并将忽略任何配置文件中的完全限定地址。

Somewhat messy but it does work. The problem comes with IIS - IIS hosted services inherit their address from IIS and will ignore fully qualified addresses in any configuration file.

但是你可以作弊,你可以使用自定义服务主机厂。这意味着在代码或IIS的.svc文件中更改服务启动代码。自定义服务主机工厂派生自 ServiceHostFactory 和覆盖

But you can cheat, you could use a custom service host factory. This does mean changing your service startup code, in either code or the .svc file for IIS. A custom service host factory deriveds from ServiceHostFactory and overrides

protected override ServiceHost CreateServiceHost(Type serviceType, 
                                                 Uri[] baseAddresses)

如您所见,您将获得一个或多个包含服务(潜在)地址的URI对象。此时,您可以将它们存储在某个位置(可能是单一查找表),然后在其他地方查询。

As you can see you're getting one or more URI objects containing the (potential) addresses of your service. At this point you can store them somewhere (singleton lookup table perhaps) against the service type and then query that elsewhere.

然后在.svc文件中,您需要更改它只是一点点;例如

Then in your .svc file you need to change it just a little; for example

<%@ServiceHost Service="MyService.ServiceName" 
               Factory="MyService.ServiceHostFactory" %>
<%@Assembly Name="MyService" %>

这篇关于以编程方式访问WCF EndPoint URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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