包括来自类库的服务引用

发布于 2024-07-13 13:38:32 字数 320 浏览 8 评论 0原文

我有一个 C# 类库和一个启动项目(一个控制台应用程序)。 类库包括对 Web 服务的服务引用。 当我尝试运行该项目时,我收到 InvalidOperationException,因为启动项目没有读取类库的 app.config,并且它忽略了服务引用。 为了让它工作,我被迫将相同的服务引用添加到启动项目中。 有什么办法可以避免这种情况吗? 我可以让启动项目识别类库的服务引用和app.config,而不必将其复制到启动项目中吗?

我尝试从类库添加到 app.config 的链接,但这不起作用。 如果类库要求任何使用它的人将该服务引用添加到启动项目中,那么它的可移植性就不是很好。

I have a C# class library and a startup project (a console app). The class library includes a service reference to a web service. When I try to run the project, I get an InvalidOperationException because the startup project isn't reading the class library's app.config, and it's ignoring the service reference. To get it working, I'm forced to add the same service reference to the startup project. Is there any way I can avoid this? Can I make the startup project recognize the class library's service reference and app.config without having to copy it to the startup project?

I've tried adding a link to the app.config from the class library, but that doesn't work. The class library isn't very portable if it requires anyone who uses it to add that service reference to the startup project.

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

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

发布评论

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

评论(5

反目相谮 2024-07-20 13:38:32

想想你想要做什么 - 你有两个正在构建的程序集:

Library
ConsoleApp

这两个程序集都有配置文件 - 我想它们看起来像这样:

Library
    app.config
ConsoleApp
    ConsoleApp.exe.config

当你运行 ConsoleApp 时,它没有办法从您的 Library 程序集中读取或了解 app.config。 它知道或关心的唯一配置文件是ConsoleApp.exe.config。 现在可以让配置文件相互引用,但这不是您想要执行的操作的正确解决方案。

由于您的Library程序集没有入口点,因此它永远不会被加载到AppDomain中。 由于它永远不会被加载到 AppDomain 中,因此它的应用程序配置文件永远不会被使用。

您应该做的是通过项目引用来引用 ConsoleApp 中的Library。 然后将所有相关配置数据从 app.config 移至 ConsoleApp.exe.config,因为这是您的应用程序将使用的配置文件。

这将使您拥有调用 Web 服务上的方法所需的两件事

  1. Library 中可以发送和接收 SOAP 消息的代码。
  2. Library 运行所需的配置元数据。

Think about what you are trying to do - you have two assemblies that you are building:

Library
ConsoleApp

Both of these assemblies have configuration files - I would imagine they look something like this:

Library
    app.config
ConsoleApp
    ConsoleApp.exe.config

When you run ConsoleApp it has no way of reading from or knowing aboout app.config from your Library assembly. The only configuration file that it knows or cares about is ConsoleApp.exe.config. Now it is possible to have configuration files reference each other but this is not the proper solution for what you are trying to do.

Since your Library assembly has no entry point, it will never be loaded into an AppDomain. Since it will never be loaded into an AppDomain its application configuration file will never be used.

What you ought to do is reference Library in ConsoleApp via a project reference. Then move all the relevant configuration data from app.config into ConsoleApp.exe.config as this is the configuration file that will be used by your application.

This will allow you to have to two things you need to invoke methods on your web service

  1. The code in Library that can send and receive SOAP messages.
  2. The configuration metadata that is required by Library to function.
请叫√我孤独 2024-07-20 13:38:32

使用类库中的服务引用然后复制配置的替代方法是使用调用 svcutil.exe 的构建事件。 我喜欢这个的一点是,当服务发生变化时,您不必进行“更新服务引用”。 它将自动更新。

在类库中,使用仅生成代理代码的构建事件:

svcutil.exe net.tcp://localhost:3315/MyService/mex /noConfig

在应用程序中,使用生成配置的构建事件。 您可以使用 /mergeConfig 选项将其合并到现有的 app.config 中。

svcutil.exe net.tcp://localhost:3315/MyService/mex 
            /config:App.config /mergeConfig

如果您不想在服务未运行时出现构建错误,请将其放入项目文件中,您将收到警告而不是错误:

<Target
    Name="PreBuildEvent"
    Condition="'$(PreBuildEvent)'!=''"
    DependsOnTargets="$(PreBuildEventDependsOn)">
  <Exec WorkingDirectory="$(OutDir)"
        Command="$(PreBuildEvent)"
        ContinueOnError="true" />
</Target>

An alternative to using a service reference in the class library and then copying the config would be to use build events that call svcutil.exe. The thing I like about this is that you don't have to make "update service reference" when the service changes. It will be updated automatically.

In the class library, use a build event that only generates the proxy code:

svcutil.exe net.tcp://localhost:3315/MyService/mex /noConfig

In the application, use a build event that generates the config. You can use the /mergeConfig option to merge it into an existing app.config.

svcutil.exe net.tcp://localhost:3315/MyService/mex 
            /config:App.config /mergeConfig

If you don't want to get a build error if the service is not running, put this in your project file and you will get a warning instead of an error:

<Target
    Name="PreBuildEvent"
    Condition="'$(PreBuildEvent)'!=''"
    DependsOnTargets="$(PreBuildEventDependsOn)">
  <Exec WorkingDirectory="$(OutDir)"
        Command="$(PreBuildEvent)"
        ContinueOnError="true" />
</Target>
完美的未来在梦里 2024-07-20 13:38:32

您只需将指向服务的配置密钥从类库配置文件复制到控制台应用程序的配置文件即可。

You just need to copy the config key, pointing to the service, from your class library config file to your console app's config file.

对不⑦ 2024-07-20 13:38:32

您可以将 app.config 的相关部分从类库的配置复制到控制台应用程序的 app.config 中。

或者,如果您确实想使其真正可移植,则需要考虑另一种从类库中引用特定服务引用的地址的方法。

You can copy the relevant portions of the app.config from the class library's configuration into the app.config for the console application.

Alternatively, if you're really trying to make this truly portable, you'll need to think about another way of referencing the address for the specific service reference from within the class library.

煮茶煮酒煮时光 2024-07-20 13:38:32

如果您运行多个配置文件,我认为会更令人困惑。

如果库具有可配置项,我完全希望必须将该配置放入我的配置文件中才能正确使用该库。

I'd think it more confusing if you had multiple configuration files running around.

If a library has configurable items, I would fully expect to have to put that configuration in my config file to properly consume the library.

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