Trusik
Trusik

Reputation: 23

Object return type on WCF service

I'm using WCF service in my application.My plan is design service contract as follows:

[ServiceContract]
public interface IService
{

    [OperationContract]
    object ServiceCall(string typeName, string methodName,  object[] methodParameters);
}

i will call ServiceCall method by given params from client side.

then on server side i will create method info and call specified method and return response to client. By this way single method will solve all my remote cals. because of known types issue in WCF i cant create such service.return type and method parameter object [] methodParameters can not be type of object

How can i solve this problem? Or instead what can i do?

Upvotes: 1

Views: 2515

Answers (1)

Mike Parkhill
Mike Parkhill

Reputation: 5551

Some reasons why defining explicit Data and Service contracts is preferable because:

  • It provides clarity to your consumers as to what your service is expecting. Without it you increase your documentation requirements
  • Makes your service more discoverable and easier to consume
  • Allows WCF to provide type-checking on the messages
  • Allows detailed controlled over how your objects are (de)serialized

However, if you want to have one operation to rule them all you could try something like this:

[ServiceContract]
public interface IService
{

    [OperationContract]
    MyResponse ServiceCall(IMyRequest request);
}

Your IMyRequest would be something like:

public interface IMyRequest{
    string TypeName {get; set}
    string MethodName {get; set;}
}

You would then expand it with the needed parameter objects for each call and would decorate it with KnownTypes. You can't really get around that. Since WCF doesn't allow you to specify just "object" (and it shouldn't since both sides need to know what they're talking about) you'll need to at least define your request object explicitly. A single OperationContract method can be used, but as I mentioned above I think you're better off with explicit operations.

Upvotes: 1

Related Questions