使用c#中的配置文件的Web服务

时间:2013-10-16 07:55:32

标签: c#-4.0

我需要帮助在xml文件中使用以下配置来更改我的网址Web服务

`<configuration>
  <appSettings>
    <add key="UserName" value="ova"/>
    <add key="UserPassword" value="ova"/>
    <add key="ServiceName" value="xe"/>
    <add key="ServerName" value="localhost"/>
    <add key="WebService" value="/FDC_Service/FDC_Service.asmx"/>
  </appSettings>
</configuration>`

及其代码我需要在源代码应用程序中调用服务器名称和Web服务,如下所示

FDC_Service.FDC_ServiceClass asd = new FDC_Service.FDC_ServiceClass();
retval = asd.FDC_Command(database.UserName, database.UserPassword, database.ServiceName, str);

FDC_Service是我的网络服务,我需要帮助 感谢....

3 个答案:

答案 0 :(得分:0)

我希望这能回答你的问题

你.cs代码添加

using System.Configuration;

然后在你的方法中添加

var username = ConfigurationManager.AppSettings["UserName"];
            var password = ConfigurationManager.AppSettings["UserPassword"];
            var serviceName = ConfigurationManager.AppSettings["ServerName"];

            FDC_Service.FDC_ServiceClass asd = new FDC_Service.FDC_ServiceClass();
            retval = asd.FDC_Command(username, password, serviceName, str);

答案 1 :(得分:0)

这对我有用。在实例化时设置适当的uri:

ws = new wsPedidosWeb.OperacionesTiendaPortTypeClient(
         new wsPedidosWeb.OperacionesTiendaPortTypeClient.EndpointConfiguration(), 
         new uri("http://whatever")
         );

答案 2 :(得分:0)

如果有人正在寻找完整的代码;像我一样:

private static string GetServiceAddressUrlByContractName(string contractFullName)
        {
            var clientSection = WebConfigurationManager
                .GetSection("system.serviceModel/client") as ClientSection;
            var endpointList = clientSection?.Endpoints.Cast<ChannelEndpointElement>().ToList();
            return endpointList?.FirstOrDefault(e => e.Contract== contractFullName)?.Address.ToString();
        }