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



>我在使用 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> 

为了处理此问题并连接 webserivce 端点地址,我修改了我的 app.config 文件(在添加服务引用时生成)以包含此标头标记。 下面是我的应用程序.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 文件或我的代码引用该配置文件以获取信息而不是在代码中给出。

通过读取 app.config 文件,整个代码在 windows 应用程序中工作正常,但在类库应用程序中不起作用,这背后有什么原因吗?

任何人请给出一些想法

DLL

app.config充其量只是提醒人们在使用 DLL 的 EXE 或web.config使用您的 DLL 的网站app.config中需要放置的内容。

默认情况下,配置系统实际上并不使用它,并且有点不幸的是,某些工具(例如Add Service Reference)将在类库项目中创建一个并且不会对此发出警告。

最新更新