Click here to Skip to main content
15,894,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I'm writing WCF Service functions that will run on Azure. This is my interface, class and enum on the server site:

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

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


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

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

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


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


If I comment out the following

C#
//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);


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
C#
Action<Certificate> callback


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

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

1 solution

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:
C#
[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;
        ...
    }
}

Have a look at this article for more information:
Duplex Service in WCF[^]
 
Share this answer
 
Comments
Henrik R L 26-Jan-16 16:21pm    
Hi Richard

Thanks for the answer. I will look at the link you have sent.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900