类库应用程序不使用app.config文件

3
我在使用web服务和进行web服务调用时遇到了问题。
我正在创建一个类库应用程序,该应用程序将生成dll,并且其他应用程序将使用该dll来连接web服务。我从第三方接收了WSDL文件,使用“添加服务引用”并使用所有代理类来创建我的soap主体。现在我有两个问题,我需要像下面这样添加wsse:security头到我的soap消息中:
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <wsse:UsernameToken>
              <wsse:Username>username</wsse:Username>
              <wsse:Password>password</wsse:Password>
            </wsse:UsernameToken>
          </wsse:Security> 

为了处理这个问题并连接WebService端点地址,我修改了我的app.config文件(当添加服务引用时生成的),将其作为头标记包含在内。以下是我的app.config文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
  </configSections>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="ICMS-Http-WSBinding">
          <security mode="Transport">
            <transport clientCredentialType="None" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="https://devs-dp-in.wellpoint.com/Claims/2.0/Get_iHealthICMS_Results"
          binding="basicHttpBinding" bindingConfiguration="ICMS-Http-WSBinding"
          contract="Ihlth_Service1.ICMSHttpWSPortType" name="ICMS-Http-WSPortType">
        <headers>
          <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <wsse:UsernameToken>
              <wsse:Username>username</wsse:Username>
              <wsse:Password>password</wsse:Password>
            </wsse:UsernameToken>
          </wsse:Security>
        </headers>
      </endpoint >
    </client>
  </system.serviceModel>
</configuration>

当我尝试按照下面的方式初始化我的客户端时,我遇到了以下错误:“在ServiceModel客户端配置部分中找不到名称为'ICMS-Http-WSPortType'和合同'Ihlth_Service.ICMSHttpWSPortType'的终结点元素。这可能是因为您的应用程序找不到配置文件,或者因为找不到与此名称匹配的终结点元素。”
Service.CMSHttpWSPortTypeClient Client = new Service.CMSHttpWSPortTypeClient("ICMS-Http-WSPortType");  

我修改了我的代码,创建了一个绑定和端点地址,并将其传递给客户端,然后它正常工作了,但是又遇到了缺少安全头的问题,因为我在app.config文件中提供了安全信息。

BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
 EndpointAddress address = new EndpointAddress("https://devs-dp-in.wellpoint.com/Claims/2.0/Get_iHealthICMS_Results");

是否有办法读取app.config文件或者让我的代码引用该配置文件以获取信息,而不是在代码内部提供信息。

这整段代码在Windows应用程序中通过读取app.config文件运行良好,但在类库应用程序中却不行。这背后是否有什么原因?

请有经验的人提供一些想法。

1个回答

2

app.config 是 DLL 的配置文件,它最多只能提醒你需要在消费你的 DLL 的 EXE 或者消费你的 DLL 的网站的 web.config 中放置什么。

默认情况下,配置系统实际上并不使用它,有些工具(如 Add Service Reference)会在类库项目中创建一个 app.config 文件,并且没有任何警告。


那么你的意思是说我需要将从服务引用创建的项目的app.config内容移动到应用程序配置文件中。对吗? - Ramesh
1
@Ramesh - 是的 - 只有一个配置文件是重要的 - 那就是属于主代码的那个。 - Damien_The_Unbeliever
谢谢你的指点。那么我没有太多选择,唯一的方法就是将配置项包含在代码中,因为我们没有权限修改应用程序或其组件的任何部分。 - Ramesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接