kbo*_*kbo 16 c# wcf service-discovery discovery
我正在尝试将ad-hoc发现添加到简单的WCF服务 - 客户端设置(当前通过控制台应用程序中的自托管实现).在Windows 7上使用VS2010进行调试,并执行我在在线教程中可以找到的任何内容,但仍然 - 发现客户端根本找不到任何内容.不用说,如果我打开客户端到正确的服务端点,我可以从客户端访问服务.
服务代码:
using (var selfHost = new ServiceHost(typeof(Renderer)))
{
try
{
selfHost.Open();
...
selfHost.Close();
Run Code Online (Sandbox Code Playgroud)
service app.config:
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<service name="TestApp.Renderer">
<host>
<baseAddresses>
<add baseAddress="http://localhost:9000" />
</baseAddresses>
</host>
<endpoint address="ws" binding="wsHttpBinding" contract="TestApp.IRenderer"/>
<endpoint kind="udpDiscoveryEndpoint"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceDiscovery/>
<serviceMetadata httpGetEnabled="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)
客户端发现代码:
DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());
var criteria = new FindCriteria(typeof(IRenderer)) { Duration = TimeSpan.FromSeconds(5) };
var endpoints = discoveryClient.Find(criteria).Endpoints;
Run Code Online (Sandbox Code Playgroud)
"端点"集合总是空洞的.我试过从调试器,从命令行,从管理命令行运行服务和客户端 - 一切,但无济于事(当然,所有在本地机器上,不是我需要它运行我的整个子网最终)
任何帮助,将不胜感激 :-)
roc*_*ast 37
这是一个超级简单的发现示例.它不使用配置文件,它都是c#代码,但您可以将概念移植到配置文件中.
在主机和客户端程序之间共享此接口(现在复制到每个程序)
[ServiceContract]
public interface IWcfPingTest
{
[OperationContract]
string Ping();
}
Run Code Online (Sandbox Code Playgroud)
把这段代码放在主机程序中
public class WcfPingTest : IWcfPingTest
{
public const string magicString = "djeut73bch58sb4"; // this is random, just to see if you get the right result
public string Ping() {return magicString;}
}
public void WcfTestHost_Open()
{
string hostname = System.Environment.MachineName;
var baseAddress = new UriBuilder("http", hostname, 7400, "WcfPing");
var h = new ServiceHost(typeof(WcfPingTest), baseAddress.Uri);
// enable processing of discovery messages. use UdpDiscoveryEndpoint to enable listening. use EndpointDiscoveryBehavior for fine control.
h.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
h.AddServiceEndpoint(new UdpDiscoveryEndpoint());
// enable wsdl, so you can use the service from WcfStorm, or other tools.
var smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
h.Description.Behaviors.Add(smb);
// create endpoint
var binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
h.AddServiceEndpoint(typeof(IWcfPingTest) , binding, "");
h.Open();
Console.WriteLine("host open");
}
Run Code Online (Sandbox Code Playgroud)
把这段代码放在客户端程序中
private IWcfPingTest channel;
public Uri WcfTestClient_DiscoverChannel()
{
var dc = new DiscoveryClient(new UdpDiscoveryEndpoint());
FindCriteria fc = new FindCriteria(typeof(IWcfPingTest));
fc.Duration = TimeSpan.FromSeconds(5);
FindResponse fr = dc.Find(fc);
foreach(EndpointDiscoveryMetadata edm in fr.Endpoints)
{
Console.WriteLine("uri found = " + edm.Address.Uri.ToString());
}
// here is the really nasty part
// i am just returning the first channel, but it may not work.
// you have to do some logic to decide which uri to use from the discovered uris
// for example, you may discover "127.0.0.1", but that one is obviously useless.
// also, catch exceptions when no endpoints are found and try again.
return fr.Endpoints[0].Address.Uri;
}
public void WcfTestClient_SetupChannel()
{
var binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
var factory = new ChannelFactory<IWcfPingTest>(binding);
var uri = WcfTestClient_DiscoverChannel();
Console.WriteLine("creating channel to " + uri.ToString());
EndpointAddress ea = new EndpointAddress(uri);
channel = factory.CreateChannel(ea);
Console.WriteLine("channel created");
//Console.WriteLine("pinging host");
//string result = channel.Ping();
//Console.WriteLine("ping result = " + result);
}
public void WcfTestClient_Ping()
{
Console.WriteLine("pinging host");
string result = channel.Ping();
Console.WriteLine("ping result = " + result);
}
Run Code Online (Sandbox Code Playgroud)
在主机上,只需调用WcfTestHost_Open()函数,然后永远睡眠或其他东西.
在客户端上,运行这些功能.主机打开需要一段时间,因此这里有几个延迟.
System.Threading.Thread.Sleep(8000);
this.server.WcfTestClient_SetupChannel();
System.Threading.Thread.Sleep(2000);
this.server.WcfTestClient_Ping();
Run Code Online (Sandbox Code Playgroud)
主机输出应该是这样的
host open
Run Code Online (Sandbox Code Playgroud)
客户端输出应该是这样的
uri found = http://wilkesvmdev:7400/WcfPing
creating channel to http://wilkesvmdev:7400/WcfPing
channel created
pinging host
ping result = djeut73bch58sb4
Run Code Online (Sandbox Code Playgroud)
对于一个发现例子来说,这是我能想到的最低限度.这些东西变得非常复杂.
| 归档时间: |
|
| 查看次数: |
10193 次 |
| 最近记录: |