简化 WCF 配置

发布于 2024-08-28 06:08:25 字数 1853 浏览 3 评论 0原文

我有很多自托管的 WCF 服务。一切工作正常,但我正在寻找标准化/简化结果配置的方法。我已经尽可能地简化了它,但我仍然不满意。目前,我的配置如下所示:

<system.serviceModel>
    <bindings>
        <netTcpBinding>
            <binding name="BindingConfiguration" ...>
                ...
            </binding>
        </netTcpBinding>
    </bindings>

    <behaviors>
        <serviceBehaviors>
            <behavior name="ServiceBehavior">
                 ...
            </behavior>
        </serviceBehaviors>
    </behaviors>

    <services>
        <service behaviorConfiguration="ServiceBehavior" name="Name1">
            <endpoint address="net.tcp://localhost:8080/name1" binding="netTcpBinding" bindingConfiguration="BindingConfiguration" contract="Contract1">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </service>
        <service behaviorConfiguration="ServiceBehavior" name="Name2">
            <endpoint address="net.tcp://localhost:8080/name2" binding="netTcpBinding" bindingConfiguration="BindingConfiguration" contract="Contract2">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </service>
        ...
    </services>
</system.serviceModel>

总共,我有 6 个服务,因此它是重复的。 ,我想:

  • 仅指定一次“localhost:8080”并在所有服务之间共享,并且仅指定差异(“name1”或“name2”)
  • 仅指定身份信息一次并在所有服务定义之间共享

理想情况下 第一点,我知道基地址,但这只是在服务级别工作,而不是跨单独的服务。对于我的第二点,我尝试将身份信息转移到端点行为中,但这似乎不受支持。

我可以做些什么来简化此配置吗?或者是我切换到基于代码的配置方法的唯一选择?

I have a bunch of self-hosted WCF services. Everything's working fine but I'm look for ways to normalize/simplify the resultant config. I've simplified it as much as possible, but I'm still not happy. Currently, my config looks like this:

<system.serviceModel>
    <bindings>
        <netTcpBinding>
            <binding name="BindingConfiguration" ...>
                ...
            </binding>
        </netTcpBinding>
    </bindings>

    <behaviors>
        <serviceBehaviors>
            <behavior name="ServiceBehavior">
                 ...
            </behavior>
        </serviceBehaviors>
    </behaviors>

    <services>
        <service behaviorConfiguration="ServiceBehavior" name="Name1">
            <endpoint address="net.tcp://localhost:8080/name1" binding="netTcpBinding" bindingConfiguration="BindingConfiguration" contract="Contract1">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </service>
        <service behaviorConfiguration="ServiceBehavior" name="Name2">
            <endpoint address="net.tcp://localhost:8080/name2" binding="netTcpBinding" bindingConfiguration="BindingConfiguration" contract="Contract2">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </service>
        ...
    </services>
</system.serviceModel>

In all, I have 6 services so it's repetitive. Ideally, I'd like:

  • To specify "localhost:8080" only once and share between all services, and only specify the difference ("name1" or "name2")
  • Specify the identity information only once and share between all service definitions

To my first point, I've aware of base addresses, but that only works at the service level, not across separate services. To my second point, I've tried moving the identity information into an endpoint behavior, but that doesn't appear to be supported.

Is there anything I can do to simplify this config? Or is my only option to switch to a code-based configuration approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪冰清 2024-09-04 06:08:25

不幸的是,正如您自己注意到的那样 - 基地址概念仅在每个服务级别上 - 因此,如果给定服务有很多端点,那么您可以使用它。

您真正拥有的唯一选择是使用其他一些方法来配置基地址,然后使用它在代码中创建服务端点。如果您自行托管服务,则可以在调用 ServiceHost.Open() 之前在主机代码中执行此操作 - 如果您在 IIS 中托管,则必须创建自己的自定义 ServiceHostFactory 完成所有这些设置,然后使用该自定义服务主机工厂在 IIS 中创建服务主机。

两者都可以通过可管理的努力来实现 - 问题是这是否值得您为此烦恼,但我们无法代替您做出决定......

Unfortunately, as you noticed yourself - the base address concept is only on a per-service level - so if you have lots of endpoints for a given service, then you can use it.

The only option you really would have is to use some other means of configuring your base address, and then use that to create the service endpoints in code. If you self-host your service, you could do this in your host code, before you call ServiceHost.Open() - if you host in IIS, you would have to create your own custom ServiceHostFactory which does all this setup, and then use that custom service host factory to create your service hosts in IIS.

Both is doable with a manageable effort - question is whether that's worth the trouble for you, but we can't decide that in your place ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文