【问题标题】:How to read System.serviceModel in Class Library with Proxy implementation of WCF service如何使用 WCF 服务的代理实现读取类库中的 System.serviceModel
【发布时间】:2019-08-09 13:52:16
【问题描述】:

我正在尝试从库的 app.config 文件中读取类库中的服务配置。但低于异常

找不到名为“WSHttpBinding_IUsers”和合同“ISGP.Plugins.SolveService.IUsers”的端点元素 在 ServiceModel 客户端配置部分。 这可能是因为没有为您的应用程序找到配置文件, 或者因为在客户端元素中找不到匹配此名称的端点元素。

我尝试实现以下代码来读取配置并创建客户端

 public class ServiceManager
{
    public static T CreateServiceClient<T>(string configName)
    {
       // string _assemblyLocation = Assembly.GetExecutingAssembly().Location;

        var configuration = ConfigurationManager.OpenMappedExeConfiguration(
                         new ExeConfigurationFileMap
                         {
                             ExeConfigFilename = "app.config"
                         }, ConfigurationUserLevel.None);

        ConfigurationChannelFactory<T> channelFactory = new ConfigurationChannelFactory<T>(configName, configuration, null);
        var client = channelFactory.CreateChannel();
        return client;
    }


}

如下调用该方法

    usersClient= (UsersClient)ServiceManager.CreateServiceClient<IUsersChannel>("WSHttpBinding_IUsers");
  usersClient.ClientCredentials.UserName.UserName = userName;
  usersClient.ClientCredentials.UserName.Password = password;

但它会抛出上述异常 app.config 文件如下

 <system.serviceModel>
  <bindings>
    <wsHttpBinding>
      <binding name="WSHttpBinding_IUsers">
        <security mode="TransportWithMessageCredential">
          <transport clientCredentialType="None" />
          <message clientCredentialType="UserName" establishSecurityContext="false" />
        </security>
      </binding>
    </wsHttpBinding>
  </bindings>
  <client>
    <endpoint address="https://xxxxxx16.prod.xxxxx.local/WebServices/v3/Users.svc"
        binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IUsers"
        contract="ISGP.Plugins.SolveService.IUsers" name="WSHttpBinding_IUsers" />
  </client>
</system.serviceModel>

【问题讨论】:

    标签: c# wcf windows-services wcf-binding


    【解决方案1】:

    请在通道工厂构造函数中传递端点参数。

    ConfigurationChannelFactory channelFactory = new ConfigurationChannelFactory(configName, configuration, null);

    您可以参考以下代码。

    ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                fileMap.ExeConfigFilename = "app.config";
                Configuration newConfiguration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                ConfigurationChannelFactory<IService> factory = new ConfigurationChannelFactory<IService>("endpoint1", newConfiguration, new EndpointAddress("http://localhost:8000/servicemodelsamples/service"));
                factory.Credentials.UserName.UserName = "jack";
                factory.Credentials.UserName.Password = "123456";
                IService client1 = factory.CreateChannel();
    

    https://docs.microsoft.com/en-us/dotnet/framework/wcf/samples/configuration-channel-factory
    如果有什么可以帮助的,请随时告诉我。

    【讨论】:

    • 嗨,如果你看到我的代码,我也这样做了。但只有你有“Iservice”,我需要改变它。
    • 在网上搜索了很多资料后,得出了一个结论。不幸的是,WCF 中没有对此的内置支持。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多