我有一个带回叫接口的简单WCF应用程序。 我想要的是在客户端的回调函数中调用其他WCF服务。
但是当我尝试做的时候,我得到了异常
DetailsSystem.ServiceModel.CommunicationException:服务器没有 提供有意义的答复;这可能是合同造成的 不匹配,过早的会话关闭或内部服务器错误。
这是合同:
[ServiceContract(CallbackContract = typeof(IMyServiceCallback))]
public interface IMyService
{
[OperationContract]
void DoAddition(int numberOne, int numberTwo);
[OperationContract]
double Pow(double number, double power);
}
public interface IMyServiceCallback
{
[OperationContract]
void OnCallback(string result);
}
这是简单的服务实现
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class MyCustomService : IMyService
{
public void DoAddition(int numberOne, int numberTwo)
{
Console.WriteLine("Now Do Addition called");
var callback = OperationContext.Current.
GetCallbackChannel<IMyServiceCallback>();
callback.OnCallback("The result is=" + numberOne + numberTwo );
}
public double Pow(double number, double power)
{
double result = 0;
try
{
Console.WriteLine("Now Pow called");
result= Math.Pow(number, power);
}
catch (Exception e)
{
Console.WriteLine("Error:" + e);
}
return result;
}
}
我将此作为服务托管在
中callServiceHost = new ServiceHost(typeof(MyService.MyCustomService));
callServiceHost.AddServiceEndpoint(typeof (IMyService), new WSDualHttpBinding(), new Uri(GetUri()));
callServiceHost.Open();
Console.WriteLine("Press to terminate.\n\n");
Console.ReadLine();
在客户端,我想在回调函数中调用wcf服务,以便
[ServiceContract(CallbackContract = typeof(IMyServiceCallback))]
public class MyServiceCallback : IMyServiceCallback
{
public void OnCallback(string result)
{
Console.WriteLine("From Server I got the result" + result);
Console.WriteLine(result);
double powValue = Service.Pow(2, 3);
Console.WriteLine("Power=" + powValue);
}
public IMyService Service { get; set; }
}
我的客户代码
var binding = new WSDualHttpBinding();
var address = new EndpointAddress(GetUri());
var callback = new MyServiceCallback();
var instanceContext = new InstanceContext(callback);
var factory = new DuplexChannelFactory<IMyService>(instanceContext, binding, address);
IMyService callService = factory.CreateChannel();
callback.Service = callService;
callService.DoAddition(4,6);
Console.ReadKey();
答案 0 :(得分:0)
我认为您的IMyServiceCallback接口缺少[ServiceContract]
装饰。
您是否也可以更改您的运营合同[OperationContract(IsOneWay = true)]