Microsoft Dynamics 365 Blog

CRM MVP Michael Höhne is our guest blogger today.

You may have noticed that when working with the Execute method of the CRM web service you always get a response, even if it doesn’t contain any data. For instance, when passing a DeleteRequest you receive a DeleteResponse. There are no properties in the DeleteResponse though.

The reason is quite simple and I’m using the same approach in many of my own web services. Consider the following classes in a web service project:

[XmlInclude(AddRequest)]
[XmlInclude(SubtractRequest)]
public abstract class Request {
public abstract Response Execute();
}

[XmlInclude(AddResponse)]
[XmlInclude(SubtractResponse)]
public abstract class Response {}

public class AddRequest : Request {
public int x;
public int y;
public override Response Execute() {
return new AddResponse { result = x+y }
}
}
public class AddResponse : Response {
public int result;
}

public class SubtractRequest : Request {
public int x;
public int y;
public override Response Execute() {
return new SubtractResponse { result = x-y }
}
}
public class SubtractResponse : Response {
public int result;
}

public class MyWebService : WebService {
public Response Execute(Request request) {
return request.Execute();
}
}

There’s only one method named “Execute” in MyWebService and you can either pass an AddRequest or a SubtractRequest. As a response you get an AddResponse or a SubtractResponse. The great thing is that you never have to touch the Execute method again and therefore the interface of your service never changes. If you need new functionality then you create new Request and Response classes.

As the Execute method returns an abstract Response object even an operation not providing results, like the CRM DeleteRequest, has to return an instance of a derived Response class. That’s the reason why you receive an empty DeleteResponse n CRM.

The concept is very easy and powerful and as said before, I have used it successfully in many implementations. If you are creating web services then consider trying it. Even if you don’t then it helps understanding CRM a bit more.

Cheers,

Michael Höhne

We're always looking for feedback and would like to hear from you. Please head to the Dynamics 365 Community to start a discussion, ask questions, and tell us what you think!