如何使用回调编写WCF函数? [英] How do I write a WCF function with a callback?

查看:87
本文介绍了如何使用回调编写WCF函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

我正在编写将在Azure上运行的WCF服务功能。这是我在服务器站点上的界面,类和枚举:



Hi
I'm writing WCF Service functions that will run on Azure. This is my interface, class and enum on the server site:

[ServiceContract]
public interface ICertificateService
{
    [OperationContract]
    Task<Certificate> GetCertificateAsync(long serialNumber);

    [OperationContract]
    void GetCertificateWithCallback(long serialNumber, Action<Certificate> callback);
}







[DataContract]
public class Certificate
{
    [DataMember]
    public long serialNumber { get; set; }

    DataMember]
    public byte[] Document { get; set; }

    [DataMember]
    public ECertificateType CertificateType { get; set; }
}







[DataContract]
public enum ECertificateType
{
    [EnumMember]
    NONE = 0,
    [EnumMember]
    Single = 1,
    [EnumMember]
    Combined = 2
}





如果我注释掉以下内容





If I comment out the following

//The result of this code is that I cannot see any functions at all on client side
//[OperationContract]
//void GetCertificateWithCallback(long serialNumber, Action<Certificate> callback);





然后我可以看到在添加服务引用后,在客户端使用我的其他函数GetCertificateAsync(...)。但是,如果我没有注释掉上面的代码,那么即使我在服务器端没有编译错误,我也无法在客户端看到这两个函数中的任何一个。



我认为问题是回调



then I can see and use my other function GetCertificateAsync(...) on the client side after adding a Service Reference. But if I do not comment out the code above then I cannot see any of the two functions on the client side even though I have no compile errors on server side.

I think the problem is the callback

Action<Certificate> callback





那么如何在下面编写WCF函数GetCertificateWithCallback(...)?在界面中应该做什么来使用Action< Certificate>回调?





So how do I write the WCF function GetCertificateWithCallback(...) below? What should be done in the interface to use Action<Certificate> callback?

[OperationContract]
void GetCertificateWithCallback(long serialNumber, Action<Certificate> callback);

推荐答案

你可以不将委托传递给WCF服务。如果您希望服务器回调客户端代码,则需要创建双工服务 [ ^ ]而是。



这样的事情应该有效:

You can't pass a delegate to a WCF service. If you want the server to call back into the client code, you need to create a Duplex Service[^] instead.

Something like this should work:
[ServiceContract(CallbackContract = typeof(ICertificateServiceCallback))]
public interface ICertificateService
{
    [OperationContract(IsOneWay = true)]
    void GetCertificateWithCallback(long serialNumber);
}

public interface ICertificateServiceCallback
{
    [OperationContract(IsOneWay = true)]
    void GetCertificateCallback(Certificate certificate);
}

sealed class CertificateService : ICertificateService
{
    private ICertificateServiceCallback Callback
    {
        get { return OperationContext.Current.GetCallbackChannel<ICertificateServiceCallback>(); }
    }
    
    public void GetCertificateWithCallback(long serialNumber)
    {
        Action<Certificate> callback = Callback.GetCertificateCallback;
        ...
    }
}



查看这篇文章了解更多信息:

WCF中的双工服务 [ ^ ]


这篇关于如何使用回调编写WCF函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆