Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
605 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...