以下代码适用于我的服务器计算机:
var discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());
var findCriteria = FindCriteria.CreateMetadataExchangeEndpointCriteria(typeof(DevExpress.Xpo.DB.IDataStoreService));
findCriteria.MaxResults = 1;
var findResults = discoveryClient.Find(findCriteria);
foreach (EndpointDiscoveryMetadata item in findResults.Endpoints)
{
Console.WriteLine("uri found = " + item.Address.Uri.ToString());
List<Uri> uris = new List<Uri>(item.ListenUris);
var endpoints = MetadataResolver.Resolve(typeof(DevExpress.Xpo.DB.IDataStoreService), item.Address);
}
if (findResults.Endpoints.Count > 0)
{
var endpoints = MetadataResolver.Resolve(typeof(DevExpress.Xpo.DB.IDataStoreService), findResults.Endpoints[0].Address);
if (endpoints.Count > 0)
{
foreach (var item in endpoints)
{
if (item.Name.Contains("NetTcpBinding"))
{
Console.WriteLine("Discovered NetTcpAddress [{0}]", item.Address);
tcpEndpoint = item.Address;
}
if (item.Name.Contains("BasicHttpBinding"))
basicHTTPendpoint = item.Address;
}
}
}
有两个foreach循环只是我试图让它工作的一部分。
当我尝试在客户端计算机上使用它时,当代码尝试执行MetadataResolver方法时会抛出以下异常...
Unhandled Exception: System.InvalidOperationException: Metadata contains a refer
ence that cannot be resolved: 'http://192.168.0.2:8112/mex'. ---> System.Service
Model.EndpointNotFoundException: There was no endpoint listening at http://192.1
68.0.2:8112/mex that could accept the message. This is often caused by an incorr
ect address or SOAP action. See InnerException, if present, for more details. --
-> System.Net.WebException: Unable to connect to the remote server ---> System.N
et.Sockets.SocketException: A connection attempt failed because the connected pa
rty did not properly respond after a period of time or established connection fa
iled because connected host has failed to respond 192.168.0.2:8112
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddre
ss socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Sock
et s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state,
IAsyncResult asyncResult, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
at System.Net.HttpWebRequest.GetRequestStream()
at System.ServiceModel.Channels.HttpOutput.WebRequestHttpOutput.GetOutputStre
am()
--- End of inner exception stack trace ---
Server stack trace:
at System.ServiceModel.Channels.HttpOutput.WebRequestHttpOutput.GetOutputStre
am()
at System.ServiceModel.Channels.HttpOutput.Send(TimeSpan timeout)
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpC
hannelRequest.SendRequest(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeS
pan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean on
eway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan tim
eout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCall
Message methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage req
Msg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgDa
ta, Int32 type)
at System.ServiceModel.Description.IMetadataExchange.Get(Message request)
at System.ServiceModel.Description.MetadataExchangeClient.MetadataReferenceRe
triever.DownloadMetadata(TimeoutHelper timeoutHelper)
at System.ServiceModel.Description.MetadataExchangeClient.MetadataRetriever.R
etrieve(TimeoutHelper timeoutHelper)
--- End of inner exception stack trace ---
at System.ServiceModel.Description.MetadataExchangeClient.MetadataRetriever.R
etrieve(TimeoutHelper timeoutHelper)
at System.ServiceModel.Description.MetadataExchangeClient.ResolveNext(Resolve
CallState resolveCallState)
at System.ServiceModel.Description.MetadataExchangeClient.GetMetadata(Metadat
aRetriever retriever)
at System.ServiceModel.Description.MetadataResolver.Resolve(IEnumerable`1 con
tracts, EndpointAddress address, MetadataExchangeClient client)
at StandaloneClient.Program.Main(String[] args) in e:\My Projects\AllTestType
Progys\WCFTestLibrary\StandaloneClient\Program.cs:line 35
它找到mex地址OK但是我需要确定哪个端点正在使用net.tcp以及哪个端点正在使用http。