包括来自类库的服务引用
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
想想你想要做什么 - 你有两个正在构建的程序集:
这两个程序集都有配置文件 - 我想它们看起来像这样:
当你运行
ConsoleApp
时,它没有办法从您的Library
程序集中读取或了解app.config
。 它知道或关心的唯一配置文件是ConsoleApp.exe.config。 现在可以让配置文件相互引用,但这不是您想要执行的操作的正确解决方案。由于您的
Library
程序集没有入口点,因此它永远不会被加载到AppDomain中。 由于它永远不会被加载到 AppDomain 中,因此它的应用程序配置文件永远不会被使用。您应该做的是通过项目引用来引用
ConsoleApp
中的Library
。 然后将所有相关配置数据从app.config
移至ConsoleApp.exe.config
,因为这是您的应用程序将使用的配置文件。这将使您拥有调用 Web 服务上的方法所需的两件事
Library
中可以发送和接收 SOAP 消息的代码。Library
运行所需的配置元数据。Think about what you are trying to do - you have two assemblies that you are building:
Both of these assemblies have configuration files - I would imagine they look something like this:
When you run
ConsoleApp
it has no way of reading from or knowing abooutapp.config
from yourLibrary
assembly. The only configuration file that it knows or cares about isConsoleApp.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
inConsoleApp
via a project reference. Then move all the relevant configuration data fromapp.config
intoConsoleApp.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
Library
that can send and receive SOAP messages.Library
to function.使用类库中的服务引用然后复制配置的替代方法是使用调用 svcutil.exe 的构建事件。 我喜欢这个的一点是,当服务发生变化时,您不必进行“更新服务引用”。 它将自动更新。
在类库中,使用仅生成代理代码的构建事件:
在应用程序中,使用生成配置的构建事件。 您可以使用 /mergeConfig 选项将其合并到现有的 app.config 中。
如果您不想在服务未运行时出现构建错误,请将其放入项目文件中,您将收到警告而不是错误:
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:
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.
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:
您只需将指向服务的配置密钥从类库配置文件复制到控制台应用程序的配置文件即可。
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.
您可以将 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.
如果您运行多个配置文件,我认为会更令人困惑。
如果库具有可配置项,我完全希望必须将该配置放入我的配置文件中才能正确使用该库。
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.