Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
570 views
in Technique[技术] by (71.8m points)

wcf - Generic Service Interface

I have a Generic Service Interface:

[ServiceContract]
public interface IService<T> where T : Class1
{
    [OperationContract]
    void DoWork(T class1);
}

Then I have a Concrete Service that inherits from that:

public class Service : IService<Class1>
{
    public void DoWork(Class1 class1)
    {
    }
}

Everything works fine until I add a webHttpEndpoint to expose a JSON WS:

<service name="Service">
    <endpoint 
        address="" 
        binding="webHttpBinding" 
        behaviorConfiguration="webHttpBehavior"
        contract="IService"  />
</service>

<behavior name="webHttpBehavior">
    <enableWebScript />
</behavior>

In fact, I receive this error:

The contract name 'IService' could not be found in the list of contracts implemented by the service 'Service'.

That's beacuse of the generic definition of the interface. Any solution?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In my opinion (and based on what you said), the interface does not need to be generic. The caller just need to know that there is a DoWork operation.

So basically, change the concrete class to be generic instead of the interface.

public class Service<T> : IService where T : Class1
{
    public void DoWork()
    {
    }
}

EDIT after clarifying the question: You need to provide the generic parameter in the config file as well:

contract="YourAssembly.IService`1[[YourAssembly.Class1, YourAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"

Here is a similar question: Inheriting from a generic contract in WCF


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...