如何使用类库的应用程序设置?

发布于 2024-10-20 08:10:12 字数 508 浏览 1 评论 0原文

我有两个项目。

一种是引用 Web 服务的 WCF 代理项目。我希望这个项目包含与 Web 服务交互的所有逻辑。

我还有一个测试项目,我想将其称为代理项目。

问题是我的所有配置设置(例如端点等)都在 WCF 代理项目中。当我运行它时,我收到此错误,表示找不到端点。

找不到默认端点 引用合同的元素 'AlumniWebService.IAlumniWebService' 在 ServiceModel 客户端中 配置部分。这可能是 因为没有配置文件 为您的应用程序找到,或者因为 没有端点元素与此匹配 合同可以在客户端找到 元素。

我必须将所有 WCF Web 服务配置放入测试项目中才能调用 Web 服务。

该 Web 服务可能不仅仅被这个测试项目使用,还可能被其他几个 Web 应用程序使用,因此我希望将我的配置放在代理项目中,以便不必有多个版本。

如何获取要引用的代理项目的 Web 配置?

I have two projects.

One is a WCF proxy project which references a web service. I want this project to contain all my logic for interacting with the web service.

I also have a test project which I want to call this proxy project.

The problem is that I have all my config settings like end points etc. in the WCF proxy project. When I run it, I get this error saying it can't find the endpoints.

Could not find default endpoint
element that references contract
'AlumniWebService.IAlumniWebService'
in the ServiceModel client
configuration section. This might be
because no configuration file was
found for your application, or because
no endpoint element matching this
contract could be found in the client
element.

I have to put all of my WCF web service configuration in the test project in order for me to call the web service.

This web service will likely be consumed by more than just this test project, and probably by several more web applications so I would like to have my configuration IN the proxy project so as to not have to have several versions.

How do I get the web config for the proxy project to be referenced?

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

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

发布评论

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

评论(2

东京女 2024-10-27 08:10:12

默认情况下,.NET 配置系统不查看类库配置文件。它们被创建为样本、助手 - 但它们并没有被使用。您需要将配置放入将使用您的类库的主应用程序中,以便“激活”和使用它。

您可以通过一些手动工作完成的一件事是:

  • 的每个子元素放入一个单独的文件
  • 引用主配置中的子元素:

    ;
       <行为 configSource="behaviors.config" />
       <绑定configSource =“绑定.config”/>
       
    
    

这样您就可以拥有一组“外部化”配置文件,并从任意数量的其他项目中引用它们。

Visual Studio 无法正确了解这一点,并将其突出显示为错误 - 但相信我 - 它确实有效! (这只是 VS 的一个缺陷,它不理解任何配置部分的 configSource= 属性)

这些外部配置文件将只包含该特定部分的位,因此你的绑定。配置看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<bindings>
    <basicHttpBinding>
         <binding name="YourBindingName" 
                  receiveTimeout="00:02:00" sendTimeout="00:02:00"
                  maxBufferSize="512000" maxBufferPoolSize="524288" 
                  maxReceivedMessageSize="512000"
                  useDefaultWebProxy="false">
              <security mode="None" />
         </binding>
    </basicHttpBinding>
</bindings>

By default, the .NET configuration system does not look at class library config files. They're being created as a sample, as a helper - but they're not being used. You need to put your configuration into the main app that will be using your class library for it to be "activated" and used.

The one thing you could do with a bit of manual work is this:

  • put each of the sub-elements of <system.serviceModel> into a separate file
  • reference those from your main config:

    <system.serviceModel>
       <behaviors configSource="behaviors.config" />
       <bindings configSource="bindings.config" />
       <client configSource="client.config" />
    </system.serviceModel>
    

That way you can have one set of "externalized" config files, and reference them from any number of other projects.

Visual Studio doesn't properly know about this and will highlight this as errors - but trust me - it does work! (it's just a VS deficiency that it doesn't understand the configSource= attribute on any config section)

Those external config files would then contain just the bits for that specific section, so your bindings.config would look like this:

<?xml version="1.0" encoding="utf-8"?>
<bindings>
    <basicHttpBinding>
         <binding name="YourBindingName" 
                  receiveTimeout="00:02:00" sendTimeout="00:02:00"
                  maxBufferSize="512000" maxBufferPoolSize="524288" 
                  maxReceivedMessageSize="512000"
                  useDefaultWebProxy="false">
              <security mode="None" />
         </binding>
    </basicHttpBinding>
</bindings>
俯瞰星空 2024-10-27 08:10:12

可以将自定义 web.config(支持具有一些应用程序设置的 web.config)导入到主 web.config 中,但我不认为您可以将服务端点配置从支持 web.config 导入到主 web.config 中。

如果您只关心在测试应用程序中配置服务,则只需将服务引用添加到测试应用程序中,Visual Studio 将根据主机应用程序中的配置自动在测试应用程序中配置您的设置。

It is possible to import custom web.config(support web.config having some settings for application) into main web.config but I dont think that you can import service endpoints configuration from support web.config to main web.config.

If you are only concerned with configuring the service at your test application , then just add service reference into your test application and Visual Studio will automatically configure your settings at test application depending upon the configuration at your host application.

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