我的服务合同和回调合同如下:
[ServiceContract(CallbackContract = typeof(IWebshopCallback))]
interface IWebshop
{
[OperationContract]
string GetWebshopName ();
[OperationContract]
string GetProductInfo (Item i);
[OperationContract]
List<Item> GetProductList ();
[OperationContract]
bool BuyProduct (string i);
[OperationContract]
void ConnectNewClient ();
}
[ServiceContract]
interface IWebshopCallback
{
[OperationContract]
void NewClientConnected (int totalNrOfConnectedClients);
}
我的服务:
[ServiceBehavior (InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Reentrant)]
class WebshopService : IWebshop
{
...
}
在服务内部,我有一个方法可以在客户端调用另一个方法:
public void ConnectNewClient ()
{
totalNumber++;
OperationContext.Current.GetCallbackChannel<IWebshopCallback> ().NewClientConnected (totalNumber);
}
在客户端,我有一个从IWebshopCallback派生的表格,其中有一个方法NewClientConnected(int a)。
问题在于,当我尝试运行代码时,遇到了以下异常:
发送到http://localhost:4000/IWebshopContract的此请求操作未在配置的超时(00:00:59.9989895)内收到答复。分配给该操作的时间可能是较长超时的一部分。
但是,更奇怪的是,如果我继续应用程序的工作,我会看到此功能有效。
是什么原因造成的?
答案 0 :(得分:0)
在服务器端,当您调用一个函数时,您需要使用Task.Run(),因此您应该像这样:
var callback = OperationContext.Current.GetCallbackChannel<IWebshopCallback> ();
Task.Run (() => callback.NewClientConnected(totalNumber));
不是这样的:
OperationContext.Current.GetCallbackChannel<IWebshopCallback> ().NewClientConnected ();
答案 1 :(得分:0)
通常,除双工模式外,我们的行为是客户端在调用服务后立即返回,并且服务器发送回的信息通过回调协定进行传输。在这种工作模式下,我们将服务合同和回调合同都转换为单向通信。
[OperationContract(Action = "post_num", IsOneWay = true)]
void PostNumber(int n);
我制作了一个演示,希望它对您有用。
服务器端。
class Program
{
static void Main(string[] args)
{
using (ServiceHost sh=new ServiceHost(typeof(MyService)))
{
ServiceMetadataBehavior smb;
smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (smb==null)
{
smb = new ServiceMetadataBehavior()
{
HttpGetEnabled = true
};
sh.Description.Behaviors.Add(smb);
}
sh.Open();
Console.WriteLine("Service is ready");
Console.ReadKey();
sh.Close();
}
}
}
[ServiceContract(Namespace ="mydomain",Name = "demo", ConfigurationName = "isv", CallbackContract = typeof(ICallback))]
public interface IDemo
{
[OperationContract(Action = "post_num", IsOneWay = true)]
void PostNumber(int n);
}
[ServiceContract]
public interface ICallback
{
[OperationContract(Action = "report", IsOneWay = true)]
void Report(double progress);
}
[ServiceBehavior(ConfigurationName ="sv")]
public class MyService : IDemo
{
public void PostNumber(int n)
{
ICallback callback = OperationContext.Current.GetCallbackChannel<ICallback>();
for (int i = 0; i <=n; i++)
{
Task.Delay(500).Wait();
double p = Convert.ToDouble(i) / Convert.ToDouble(n);
callback.Report(p);
}
}
}
服务器配置
<system.serviceModel>
<services>
<service name="sv">
<endpoint address="http://localhost:3333" binding="wsDualHttpBinding" contract="isv"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:3333"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
客户端。
class Program
{
static void Main(string[] args)
{
DuplexChannelFactory<IDemo> factory = new DuplexChannelFactory<IDemo>(new CallbackHandler(), "test_ep");
IDemo channel = factory.CreateChannel();
Console.WriteLine("Start to Call");
channel.PostNumber(15);
Console.WriteLine("Calling is done");
Console.ReadLine();
}
}
[ServiceContract(Namespace ="mydomain",Name = "demo", ConfigurationName = "isv", CallbackContract = typeof(ICallback))]
public interface IDemo
{
[OperationContract(Action = "post_num",IsOneWay =true)]
void PostNumber(int n);
}
[ServiceContract]
public interface ICallback
{
[OperationContract(Action = "report",IsOneWay =true)]
void Report(double progress);
}
public class CallbackHandler : ICallback
{
public void Report(double progress)
{
Console.WriteLine("{0:p0}", progress);
}
}
客户端配置
<system.serviceModel>
<client>
<endpoint name="test_ep" address="http://localhost:3333" binding="wsDualHttpBinding" contract="isv"/>
</client>
</system.serviceModel>
结果。