簡體   English   中英

如何使用WCF服務的代理實現讀取類庫中的System.serviceModel

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

我試圖從庫的app.config文件中讀取類庫中的服務配置。 但要低於例外

找不到名為“WSHttpBinding_IUsers”的端點元素,並在ServiceModel客戶端配置部分中收縮“ISGP.Plugins.SolveService.IUsers”。 這可能是因為沒有為您的應用程序找到配置文件,或者因為在客戶端元素中找不到與此名稱匹配的端點元素。

我試着實現下面的代碼以讀取配置並創建客戶端

 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>

請在通道工廠構造函數中傳遞endpoint參數。

ConfigurationChannelFactory channelFactory =新的ConfigurationChannelFactory(configName,配置,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
如果有什么我可以幫忙,請隨時告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM