如何在C#中获取WCF端点地址URL

时间:2016-06-17 13:27:02

标签: c# wcf

有没有办法可以根据服务客户端名称读取终点地址?

这是我的服务参考名称:

TestServiceReference.TestServiceClient TestClient = new TestServiceReference.TestServiceClient ();

这是我的web.config文件

 <endpoint address="http://....................../TestService.svc"
 behaviorConfiguration="webEndpoint" binding="webHttpBinding"
 bindingConfiguration="webHTTPBinding_abc" contract="TestServiceReference.ITestService" />**

喜欢以下网址

var url =“http://............./TestService.svc”;

4 个答案:

答案 0 :(得分:1)

尝试使用:

TestClient.InnerChannel.RemoteAddress

此致

答案 1 :(得分:0)

TestClient.InnerChannel.RemoteAddress

答案 2 :(得分:0)

好的,这很容易做到,我只需要使用 MetadataExchangeClient 来获取配置。在代码中,我要做的就是获取MetaData Xml:

var meta = new System.ServiceModel.Description.MetadataExchangeClient(new Uri("net.tcp://10.0.2.124:9000/TeraService/SetInstruments/mex"), System.ServiceModel.Description.MetadataExchangeClientMode.MetadataExchange);
var data = meta.GetMetadata();
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(data.GetType());
TextWriter writer = new StreamWriter("xmlfile.xml");
x.Serialize(writer, data);
writer.Close();

现在,我只需要花些时间从该配置中获取端点。

答案 3 :(得分:0)

TestClient.Endpoint.Address.Uri对我有用。