简体   繁体   English

以编程方式访问 WCF 端点 URL

[英]Programmatically access WCF EndPoint 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:我有一个 IIS 托管 WCF 服务(配置如本博文中所述......我需要知道配置的端点的 URL 是什么。例如,给定这个配置:

  <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>

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.我希望能够将值“ http://localhost:9000/MessageProxy.svc ”转换为 web 应用程序的启动事件中的字符串变量。

OK so the short answer is "you can't".好的,所以简短的回答是“你不能”。 The longer answer is you can, kind of, with a bit of work.更长的答案是你可以,有点工作。

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:如果您将其托管在 IIS 之外,则加载配置部分本身并对其进行解析实际上是合理的,因为您可以将其转换为 WCF 配置 class:

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

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 - IIS 托管服务从 IIS 继承其地址,并将忽略任何配置文件中的完全限定地址。

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.这确实意味着在 IIS 的代码或 .svc 文件中更改您的服务启动代码。 A custom service host factory deriveds from ServiceHostFactory and overrides派生自ServiceHostFactory并覆盖的自定义服务主机工厂

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

As you can see you're getting one or more URI objects containing the (potential) addresses of your service.如您所见,您将获得一个或多个包含您服务的(潜在)地址的 URI 对象。 At this point you can store them somewhere (singleton lookup table perhaps) against the service type and then query that elsewhere.此时,您可以根据服务类型将它们存储在某个地方(也许是单个查找表),然后在其他地方查询。

Then in your.svc file you need to change it just a little;然后在您的 .svc 文件中,您只需稍微更改一下即可; for example例如

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

If you are already have an instance of your service proxy created, you can use the following:如果您已经创建了服务代理的实例,则可以使用以下内容:

    public static Uri GetServiceUri(this IMyService proxy)
    {
        var channel = proxy as IContextChannel;
        return 
              channel != null && channel.RemoteAddress != null ? 
                 channel.RemoteAddress.Uri : null;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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