0

I've got a WCF service, which contains a Entity Framework model. My class has a timestamp attribute, so conflicting updates should throw an OptimisticConcurrencyException. My question is, what's the best way to pass this exception to the client, without assuming that the client is .NET?

So, I will skeleton out one approach, that I think demonstrates the problem. Here is a WCF service with an async method:

[ServiceContract]
public interface ICarService
{
    [OperationContract]
    [FaultContract(typeof(OptimisticConcurrencyException))]
    Task UpdateCarAsync(Car obj);
}

Then, here is a ASP.NET MVC client:

try {
    await this.repo.UpdateCarAsync(theCar);
}
catch (FaultException<OptimisticConcurrencyException>) {
    ModelState.AddModelError(string.Empty, "Optimistic Concurrency Exception");
}

In this case the client to the WCF service knows about OptimisticConcurrencyException because it is already a .NET client. But, what if this was being called from some other language? Shouldn't there be some kind of abstraction of OptimisticConcurrencyException?

I think I'm really missing something here. So, a proper example would be helpful.

AJ Morris
  • 1,099
  • 2
  • 10
  • 19

2 Answers2

1

WCF follows *ws standard for fault (error) handling. It is up to the client to consume the service definition (wsdl) in a correct way (I assume your client is not sharing an assembly containing the service definition). Since fault contracts are reflected in the wsdl file it is up to the client to act on faults.

So, in that sense Fault (contracts) are not .NET based, they are part of the ws standard and technology agnostic.

In your example the client knows about OptimisticConcurrencyException from the service definition, and that information is used when a potential client proxy is created (automated).

Hope this makes some kind of sense.

Jocke
  • 2,189
  • 1
  • 16
  • 24
  • That does make sense, now. I realize that the generated OptimisticConcurrencyException is different than the .NET framework's OptimisticConcurrencyException. So, I'm assuming that it's bad practice to pass that exception, and I'll create my own. – AJ Morris Jun 04 '15 at 18:54
0

This functionality is based on .NET framework. I think there is no one-on-one mapping between .NET and eg.:Java exceptions. However my solution was - for cross platform client - a Result DataContract which contains IsSuccess bool flag and ExceptionMessage string (and your result object too).

My service always returned and handled exceptional case, but it filled ExceptionMessage and set IsSuccess to false. Client checked these properties.

Edit:

Btw: SOAP has FaultContract

Btw2: This topic more or less duplication of WCF/WebService: Interoperable exception handling

Community
  • 1
  • 1
sac1
  • 1,344
  • 10
  • 15
  • At first I didn't realize that the wcfsvcutil was creating a new class for OptimisticConcurrencyException, and not reusing the framework's class. In that sense, the exception would be cross-platform. Still, looking at the duplicate question, I think I'm going to with the approach whereby I create a FaultContract and my concurrency exception would contain the current database values. – AJ Morris Jun 04 '15 at 18:51