Including a service reference from a class library

寵の児 提交于 2019-12-01 02:46:18

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.

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>

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!