接口调用失败,失败原因:在 ServiceModel 客户端配置部分中,找不到引用协定的默认终结点元素

我的程序中,已经配置了webserivce了,但是无法再开发环境使用,我想拿到测试环境使用,而webservice又只能在开发环境调用。这个时候,为了解决这种尴尬问题,我只能先将就着用开发时的webservice。


在我的web.config中,有如下终结点配置,但是我的程序可能,我希望能在测试环境中,能够根据web.config中的配置来使用。

<system.serviceModel>
    <client>
      <endpoint address="http://192.168.254.120:7001/ui/services/OnlineBussService"
        binding="basicHttpBinding" bindingConfiguration="OnlineBussServiceSoapBinding"
        contract="ABCLifeFTP.OnlineBussWebService" name="OnlineBussService" />
    </client> 

  </system.serviceModel>

原先开发环境的调用webservice已经写好了,但是现在不得不为了动态使用配置而新增代码,添加了如下的代码:


 //获取web.config中的终结点配置

public string GetEndPointAddressByName(string name)   

        {
            string configUrl = "";
            ClientSection clientSection = (ClientSection)WebConfigurationManager.GetSection("system.serviceModel/client");  //找到终结点标签
            if (clientSection == null && clientSection.Endpoints.Count <= 0)  //判断是否存在
            {
                RecordLog<string>("Recive:[GetEndPointAddressByName]", "Endpoint地址未配置");
                return null;
            }
            foreach (ChannelEndpointElement cee in clientSection.Endpoints)    //循环终结点,找到需要使用的配置
            {
                if (cee.Name == name && cee.Address != null)
                {
                    configUrl = clientSection.Endpoints[0].Address.AbsoluteUri;   //获取地址
                    RecordLog<string>("Recive:[GetEndPointAddressByName.configUrl]", configUrl);
                    return configUrl;
                }
            }
            RecordLog<string>("Recive:[GetEndPointAddressByName]", "Endpoint地址未配置或配置错误,调用的终端节点名称:" + name);
            return null;

        }


而在调用web.config代码中,也需要做修改:


           OnlineBussWebServiceClient OBWSC = null;     //定义接口Client


            try
            {
                string str = GetEndPointAddressByName("OnlineBussService");     //根据此名称,调用上面的方法,获取webservice的地址
                if (str != null)
                    OBWSC = new OnlineBussWebServiceClient(new BasicHttpBinding(), new EndpointAddress(str));   //根据地址去实例化接口
                else
                    return;
                if (OBWSC == null)
                {
                    RecordLog<string>("Recive:[SendPolicyInfo.OBWSC]", "服务调用失败");
                    return;
                }
                RecordLog<OnlineBussWebServiceClient>("Recive:[SendPolicyInfo.OBWSC]", OBWSC);
            }
            catch (Exception ex)
            {
                RecordLog<string>("Recive:[SendPolicyInfo.Exception1]", ex.Message);
                return;
            }

使用接口方法:

        string sss=OBWSC .GetName("Test");


就这样,我的程序,就可以根据web.config文件中配置的地址,来动态使用接口了,在测试环境使用就不成问题了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

willgon123

谢谢打赏,我将再接再厉!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值