我正在尝试基于this example为WCF回调服务进行自托管。
这是托管代码:
服务:
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(Message), new Uri("http://localhost:8000/HelloWCF")))
{
// Set up a service endpoint [Contract, Binding, Address]
host.AddServiceEndpoint(typeof(IMessage), new WSDualHttpBinding() { ClientBaseAddress = new Uri("http://locahost:8001/HelloWCF") }, "HelloWCF");
// Enable metadata exchange
ServiceMetadataBehavior smb = new ServiceMetadataBehavior() {HttpGetEnabled =true };
host.Description.Behaviors.Add(smb);
host.Open();
Console.WriteLine("Ready...");
Console.ReadLine();
}
}
客户端:
的app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<wsDualHttpBinding >
<binding name="WSDualHttpBinding_IMessage" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" clientBaseAddress="http://locahost:8001/HelloWCF">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00" />
<security mode="Message">
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" />
</security>
</binding>
</wsDualHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8000/HelloWCF/HelloWCF" binding="wsDualHttpBinding"
bindingConfiguration="WSDualHttpBinding_IMessage" contract="CallbackService.IMessage"
name="WSDualHttpBinding_IMessage" >
<identity>
<userPrincipalName value="badasscomputing\menkaur" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
c#code:
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
class Sender : IMessageCallback, IDisposable
{
private MessageClient messageClient;
public void Go()
{
InstanceContext context = new InstanceContext(this);
messageClient = new MessageClient(context, "WSDualHttpBinding_IMessage");
for (int i = 0; i < 5; i++)
{
string message = string.Format("message #{0}", i);
Console.WriteLine(">>> Sending " + message);
messageClient.AddMessage(message);
}
}
public void OnMessageAdded(string message, DateTime timestamp)
{
}
public void Dispose()
{
messageClient.Close();
}
}
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
class Listener : IMessageCallback, IDisposable
{
private MessageClient messageClient;
public void Open()
{
InstanceContext context = new InstanceContext(this);
messageClient = new MessageClient(context, "WSDualHttpBinding_IMessage");
messageClient.Subscribe();
}
public void OnMessageAdded(string message, DateTime timestamp)
{
Console.WriteLine("<<< Recieved {0} with a timestamp of {1}", message, timestamp);
}
public void Dispose()
{
messageClient.Unsubscribe();
messageClient.Close();
}
}
static void Main(string[] args)
{
Listener l = new Listener();
l.Open();
Sender s = new Sender();
s.Go();
}
服务器启动正常。 在运行客户端时,它在尝试调用任何服务器函数时崩溃,但有以下异常:
EndpointNotFoundException:There was no endpoint listening at http://localhost:8000/HelloWCF/HelloWCF that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
内部异常是:无法连接到远程服务器。
这不是因为防火墙,因为我成功测试了没有回调函数的类似应用程序
这可能是什么原因?
已更新
完整来源可以在这里下载:http://iathao.com/tmp/fullSource.zip
答案 0 :(得分:2)
您似乎在http://localhost:8000/HelloWCF托管您的终端,但您的客户端配置指向http://localhost:8000/HelloWCF/HelloWCF(注意:额外“/ HelloWCF”)
<强>更新强>
您的客户端配置合约设置为CallbackService.IMessage
但您的代码中没有任何地方存在IMessage合同的服务实现。
答案 1 :(得分:1)
通过这些小改动,您的代码在我的机器上运行良好。
客户端配置
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<wsDualHttpBinding>
<binding name="WSDualHttpBinding_IMessage" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00" />
<security mode="Message">
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" />
</security>
</binding>
</wsDualHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8000/HelloWCF" binding="wsDualHttpBinding"
bindingConfiguration="WSDualHttpBinding_IMessage" contract="CallbackService.IMessage"
name="WSDualHttpBinding_IMessage">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
入门代码
namespace wcfStarter
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(Message), new Uri("http://localhost:8002/HelloWCF")))
{
// Set up a service endpoint [Contract, Binding, Address]
host.AddServiceEndpoint(typeof(IMessage), new WSDualHttpBinding() { ClientBaseAddress = new Uri("http://locahost:8001") }, "HelloWCF");
// Enable metadata exchange
ServiceMetadataBehavior smb = new ServiceMetadataBehavior() {HttpGetEnabled =true };
host.Description.Behaviors.Add(smb);
host.Open();
Console.WriteLine("Ready...");
Console.ReadLine();
}
}
}
}