【问题标题】:class library application not using the app.config file类库应用程序不使用 app.config 文件
【发布时间】:2015-09-05 04:23:37
【问题描述】:

我在使用网络服务和进行网络服务调用时遇到问题。

我正在创建将生成 dll 的类库应用程序,此 dll 将被其他应用程序用于连接 web 服务。我已从第三方收到 WSDL 文件,我“添加服务引用”并使用了所有代理类来创建我的肥皂体。现在我有两个问题需要将 wsse:security 标头添加到我的肥皂消息中,如下所示

<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 文件(在添加服务引用时生成)以将其包含为标头标记。下面是我的 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 文件或我的代码引用该配置文件来获取信息,而不是在代码中给出。

通过读取 app.config 文件,整个代码在 windows 应用程序中运行良好,但在类库应用程序中却没有,这背后有什么原因吗?

哪位大神给点意见

【问题讨论】:

    标签: c# web-services soap class-library


    【解决方案1】:

    DLL 的app.config 充其量只是提醒您使用您的DLL 或网站的web.config 的EXE 的app.config 中需要放置的内容这会消耗你的 DLL。

    配置系统实际上并没有实际使用它,有点遗憾的是某些工具(例如Add Service Reference)会在类库项目中创建一个并且对此没有任何警告。

    【讨论】:

    • 所以你的意思是说我需要将我的项目的 app.config 的内容(从服务引用创建)移动到应用程序配置文件。对吗?
    • @Ramesh - 是的 - 只有一个配置文件很重要 - 属于主要代码段的配置文件
    • 感谢您的启发。然后我没有太多选择,唯一的办法就是在代码中包含配置,因为我们没有权限修改应用程序的任何部分或其组件。
    猜你喜欢
    • 1970-01-01
    • 2010-10-22
    • 2011-05-19
    • 1970-01-01
    • 2011-06-04
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    相关资源
    最近更新 更多