接口调用失败,失败原因:在 ServiceModel 客户端配置部分中,找不到引用协定的默认终结点元素
2014-11-21 14:23
246 查看
我的程序中,已经配置了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文件中配置的地址,来动态使用接口了,在测试环境使用就不成问题了。
在我的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文件中配置的地址,来动态使用接口了,在测试环境使用就不成问题了。
相关文章推荐
- 在 ServiceModel 客户端配置部分中,找不到引用协定“WebServiceTest.WebServiceSoap”的默认终结点元素。这可能是因为未找到应用程序的配置文件,或者是因为客户端元素
- 错误:在 ServiceModel 客户端配置部分中,找不到引用协定“*********.I******”的默认终结点元素。这可能是因为未找到应用程序的配置文件,或者是因为客户端元素中找不到与此协定匹配的终结点元素。
- 在 ServiceModel 客户端配置部分中,找不到引用协定“XX”的默认终结点元素的解决方法
- 在 ServiceModel 客户端配置部分中,找不到引用协定“PutMCCases.PutMCCasesSoap”的默认终结点元素。这可能是因为未找到应用程序的配置文件,或者是因为客户端元素中找不到
- 在 ServiceModel 客户端配置部分中,找不到引用协定“IpsBarcode.ScanService”的默认终结点元素。这可能是因为未找到应用程序的配置文件,或者是因为客户端元素中找不到与此协定匹配的终结点元素。
- 在 ServiceModel 客户端配置部分中,找不到引用协定“XXX”的默认终结点元素
- 在 ServiceModel 客户端配置部分中,找不到引用协定“XX”的默认终结点元素的解决方法
- 类库文件引用web服务报错解决方法-在 ServiceModel 客户端配置部分中,找不到引用协定的默认终结点元素
- 在 ServiceModel 客户端配置部分中,找不到引用协定“ServiceReference1.IPathIdentifyService”的默认终结点元素
- 在 ServiceModel 客户端配置部分中,找不到引用协定“WebServiceTest.WebServiceSoap”的默认终结点元素。这可能是因为未找到应用程序的配置文件,或者是因为客户端元素
- 在 ServiceModel 客户端配置部分中,找不到引用协定“XX”的默认终结点元素的解决方法
- 在 ServiceModel 客户端配置部分中,找不到引用协定“myservice.Service1Soap”的默认终结点元素。这可能是因为未找到应用程序的配置文件,或者是因为客户端元素中找不到与此协定匹配的终结点元素。
- 在 ServiceModel 客户端配置部分中,找不到引用协定“XX”的默认终结点元素的解决方法
- 在 ServiceModel 客户端配置部分中,找不到引用协定“WebServiceTest.WebServiceSoap”的默认终结点元素。这可能是因为未找到应用程序的配置文件,或者是因为客户端元素
- 在 ServiceModel 客户端配置部分中,找不到引用协定“PmWs.PmWebServiceSoap”的默认终结点元素
- WebService在 ServiceModel客户端配置部分中,找不到引用协定 的默认终结点元素
- 在ServiceModel客户端配置部分中,找不到引用协定“”的默认终结点元素
- 在 ServiceModel 客户端配置部分中,找不到名称...和协定...的终结点元素
- 在 ServiceModel 客户端配置部分中,找不到名称“$”和协定“$”的终结点元素。
- 在ServiceModel客户端配置中找不到应用协定的默认终结点元素。