C# 包括来自类库的服务引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/529550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 06:54:32  来源:igfitidea点击:

Including a service reference from a class library

c#app-configservice-reference

提问by JR.

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?

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

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.

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

采纳答案by Andrew Hare

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 ConsoleAppit has no way of reading from or knowing aboout app.configfrom your Libraryassembly. 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.

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

Since your Libraryassembly 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.

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

What you ought to do is reference Libraryin ConsoleAppvia a project reference. Then move all the relevant configuration data from app.configinto ConsoleApp.exe.configas this is the configuration file that will be used by your application.

什么你应该做的是参考LibraryConsoleApp通过项目引用。然后将所有相关的配置数据从app.configinto移动,ConsoleApp.exe.config因为这是您的应用程序将使用的配置文件。

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

这将允许您在调用 Web 服务上的方法时需要做两件事

  1. The code in Librarythat can send and receive SOAP messages.
  2. The configuration metadata that is required by Libraryto function.
  1. 其中的代码Library可以发送和接收 SOAP 消息。
  2. 运行所需的配置元数据Library

回答by Martin Bring

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.

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

回答by Joe

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.

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

回答by David Morton

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

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

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.

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

回答by D.H.

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.

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

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.

在应用程序中,使用生成配置的构建事件。您可以使用 /mergeConfig 选项将其合并到现有的 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>