通用服务接口 [英] Generic Service Interface

查看:30
本文介绍了通用服务接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用服务接口:

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)
    {
    }
}

一切正常,直到我添加一个 webHttpEndpoint 来公开一个 JSON WS:

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:

合同名称IService"不能在合同清单中找到由服务Service"实现.

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?

推荐答案

在我看来(基于你所说的),界面不需要是通用的.调用者只需要知道有一个 DoWork 操作.

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]]"

这是一个类似的问题:从 WCF 中的通用合同继承

这篇关于通用服务接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆