Vistari
Vistari

Reputation: 707

Interface Parameter / Return Type in a WCF Service reference

I have a WCF service and an MVC client that has a service reference to the aformentioned. I am trying to use marker interfaces as return types and parameters but I'm having trouble generating the service reference to reflect this.

I'm currently getting a return type / parameter type of "object" in my service reference so my question is how would I go about resolving this as the actual function in my service takes and returns an interface of IWorkRequest and IWorkResponse respectively.

My service contract looks like the following:

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    IWorkResponse DoWork(IWorkRequest request);
}

My service-side interfaces look like the following:

[ServiceKnownType(typeof(WorkRequestA))]
[ServiceKnownType(typeof(WorkRequestB))]
[ServiceContract()]
public interface IWorkRequest 
{

}

I have a similar one for IWorkResponse following the same pattern. When I build the project and update the service reference to it then the service reference looks like:

public object DoWork(object request) {
    return base.Channel.DoWork(request);
}

I also have no reference to the interfaces or any of the classes that implement the interfaces available clientside through the service reference.

Ideally I would want the service reference to be the following:

public IWorkResponse DoWork(IWorkRequest request) {
    return base.Channel.DoWork(request);
}

I have viewed a number of answers regarding this but none seem to resolve my issue so any insight would be useful.

Thanks in advance for any help provided.

Upvotes: 1

Views: 1177

Answers (1)

superblygeneralobject
superblygeneralobject

Reputation: 119

I'd like to add a comment rather than an answer but I don't have enough reputation, so here goes.

You cannot return an interface through a service operation, or pass an interface as an argument to the operation. The service and client need to be completely decoupled and not use shared interfaces. Remember that WCF clients are not always .Net applications.

If IWorkerResponse only has properties (no methods), then there is no need to use an interface at all.

Otherwise use the methods of IWorkResponse as operation contract

[ServiceContract]
public interface IWorkResponseService
{
    [OperationContract]
    SomeResponseObject DoWork(SomeRequestObject request);
}

It may still be possible to do what Maarten suggested in his comment.

Also see the answer to this How do I return a reference to an interface from a WCF contract?

Upvotes: 3

Related Questions