WCF Want to Implement Client progress update

769 views Asked by At

I have a duplex WCF service that clients connect to and wait for commands. The structure of the application is as below.

GUI(Asp.net)------------>WCF Service------------------>WCF Client

The from Asp.net pages you can run Gallio/MB Unit Tests. Those are passed to the Service and the service asks the client(s) to run the tests. It's all working good. What i want to implement now is to update the Asp.net application with the status of the test.

So if the user wants to run a whole assembly of tests at least i should know on the Asp.NET application which test is running and possibly the percentage of the test that is completed.

Any help on how to go about this / what approach to take is greatly appreciated.

Thanks

2

There are 2 answers

4
Nima M On

You need to define a callback contract and implement it in your ASP.Net website. You also need to use wsDualHttpBinding to add support for duplex operations.

public interface ITransferAck{
    [OperationContract(IsOneWay = true)]
    void TransferRecordResendRequest(int transferId); 
}
[ServiceContract(CallbackContract=typeof(ITransferAck))]
public interface IBankTransfer{
    [OperationContract(IsOneWay=true)]
    void ExchangeTransferRecord(int transferId, string record);
}

The service communicates with the client application through a proxy reference that it obtains from OperationContext, as the following code examples show.

client = OperationContext.Current.GetCallbackChannel<ITransferAck>(); 

client.TransferRecordResendRequest(1); 
2
Nima M On

SOAP fault is a mechanism for transferring error or fault conditions from a service to a consumer. The SOAP specification includes a definition for SOAP faults, providing structure for the contents of the message body when errors occur. This makes it possible for all the different SOAP stacks to issue faults in a standard way.

FaultException Used to send untyped fault data back to the consumer.

FaultException<TDetail> A generic version used to send typed fault data back to the client, where TDetail represents the type parameter for the detailed fault information to be serialized back to the consumer as part of the SOAP fault message.

The FaultContractAttribute The FaultContractAttribute, also defined in System.ServiceModel, enables a service developer to declare which faults a given service operation might issue if things go wrong. Following are the key pieces of information pertinent to working with the FaultContractAttribute:

The attribute can be applied to operations only.
The attribute is not inherited.
The attribute can be applied multiple times; for instance, if your service operation might return different types of faults, you would have a FaultContractAttribute declaration for each type of fault.

The attribute’s constructor takes a Type object used to reference the .NET type of the Detail object, that is, the type of fault information details you want to bundle with your faults.

[ServiceContract()]
public interface ICalculatorService{
[OperationContract()]
[FaultContract(typeof(string))]
double Divide(double numerator, double denominator);
}
public class CalculatorService : ICalculatorService {
public double Divide(double numerator, double denominator) {
if (denominator == 0.0d) {
string faultDetail = "You cannot divide by zero";
throw new FaultException<string>(faultDetail);
}
return numerator / denominator;
}
}

*Please note that instead string class in this example you can define your own custom exception class.