简体   繁体   English

如何使用WCF服务的代理实现读取类库中的System.serviceModel

[英]How to read System.serviceModel in Class Library with Proxy implementation of WCF service

I am trying to read service configuration in Class Library from the app.config file of the library. 我试图从库的app.config文件中读取类库中的服务配置。 but getting below exception 但要低于例外

Could not find endpoint element with name 'WSHttpBinding_IUsers' and contract 'ISGP.Plugins.SolveService.IUsers' in the ServiceModel client configuration section. 找不到名为“WSHttpBinding_IUsers”的端点元素,并在ServiceModel客户端配置部分中收缩“ISGP.Plugins.SolveService.IUsers”。 This might be because no configuration file was found for your application, or because no endpoint element matching this name could be found in the client element. 这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中找不到与此名称匹配的端点元素。

I tried ti implement the below code to read the config and create the client 我试着实现下面的代码以读取配置并创建客户端

 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;
    }


}

Call this method as below 调用此方法如下

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

But it throws the above exception the app.config file is as below 但是它抛出上述异常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>

Please pass the endpoint parameter in the channel factory constructor. 请在通道工厂构造函数中传递endpoint参数。

ConfigurationChannelFactory channelFactory = new ConfigurationChannelFactory(configName, configuration, null); ConfigurationChannelFactory channelFactory =新的ConfigurationChannelFactory(configName,配置,null);

You can refer to the following code. 您可以参考以下代码。

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 https://docs.microsoft.com/en-us/dotnet/framework/wcf/samples/configuration-channel-factory
Feel free to let me know if there is anything I can help with. 如果有什么我可以帮忙,请随时告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从类库中读取“ System.ServiceModel”配置节组? - How do I read the “System.ServiceModel” configuration section group from a class library? 为WCF数据服务客户端生成System.ServiceModel配置部分 - Generating the System.ServiceModel configuration section for WCF Data Service client 使用System.ServiceModel在Xamarin中使用WCF REST基本身份验证服务 - Consume WCF REST basic auth service in Xamarin using System.ServiceModel System.ServiceModel引用 - System.ServiceModel References System.ServiceModel消失 - System.ServiceModel disappear System.ServiceModel缺失 - System.ServiceModel missing 目标.net框架类库,具有来自.net核心应用程序的System.ServiceModel依赖项 - target .net framework class library with System.ServiceModel dependency from a .net core application 部署WCF客户端时出错 - 在SQL Catalog中找不到&#39;system.servicemodel&#39; - Error deploying WCF Client - 'system.servicemodel was not found in SQL Catalog' System.ServiceModel 错误 使用 ASP.NET 核心 ZDB974238714CA8DE6FZAACE714CA8DE6FZAACE714CA8DE6FZAACE7 中嵌入 Windows 服务中的 WCF 时不支持操作 - System.ServiceModel error Operation not supported when consuming WCF embedded in Windows Service in ASP.NET Core API 记录System.ServiceModel时的远程服务跟踪侦听器 - Remote Service Trace Listener while logging System.ServiceModel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM