简体   繁体   English

未在服务中实现接口成员

[英]Not implementing interface member in service

First of all I apologize if the references to this question are not correct. 首先,如果对这个问题的引用不正确,我会道歉。

I am developing a three-tier MVC 3 application. 我正在开发一个三层MVC 3应用程序。 In the business layer I need to create a service which will call the Data Access Layer, in order to retrieve a list of existing towns. 在业务层中,我需要创建一个服务,该服务将调用数据访问层,以便检索现有城镇的列表。

My ITown interface is as follows; 我的ITown界面如下;

[ServiceContract]
public interface ITown
{
    [OperationContract]
    IEnumerable<Town> GetAllTowns();
}

In the Town.svc.cs , I am inserting as follows; Town.svc.cs ,我插入如下;

public class Town : ITown
{
    public IEnumerable<Common.Town> GetAllTowns()
    {
        return new DATown().GetAllTowns();
    }
}

The problem is in the above code. 问题出在上面的代码中。 I am getting an error under Town in the class declaration public class Town : ITown . 我得到下一个错误Town在类声明public class Town : ITown

'Business.Town' does not implement interface member 'Business.ITown.GetAllTowns()'. 
'Business.Town.GetAllTowns()' cannot implement 'Business.ITown.GetAllTowns()'
because it does not have the matching return type of 'System.Collections.Generic.IEnumerable<Business.Town>'.

This is the content in my DATown ; 这是我DATown的内容;

public class DATown : Connection
{
    //Constructor
    public DATown()
        :base()
    { }

    public IEnumerable<Town> GetAllTowns()
    {
        return entities.Town.AsEnumerable();
    }
}

So basically what I am doing is calling the GetAllTowns() method of DATown from the Town.svc.cs class. 所以基本上我做的是调用GetAllTowns()的方法DATownTown.svc.cs类。

I've searched around for other similar queries; 我一直在寻找其他类似的问题; mostly saying that there is a correct parameter in one of the methods. 大多数人都说其中一种方法中存在正确的参数。 I do not think that this is the case with my code. 我不认为我的代码就是这种情况。

I would appreciate if someone could help me figure out what is wrong. 如果有人能帮助我弄清楚什么是错的,我将不胜感激。

Thanks in advance. 提前致谢。

In your Town class you have an IEnumerable<Common.Town> and in the interface you've simply got IEnumerable<Town> , but in your error it shows that the interface is using Business.Town and not Common.Town . 在您的Town类中,您有一个IEnumerable<Common.Town>并且在界面中您只是获得了IEnumerable<Town> ,但在您的错误中它显示该界面正在使用Business.Town而不是Common.Town The issue is that these are not the same as each other. 问题是这些彼此并不相同。

If you change your Interface so the IEnumerable is also of Common.Town you should be ok. 如果你改变你的界面,所以IEnumerable也是Common.Town,你应该没问题。

From what I see, you've got an incorrect return type - your Town in ITown is Bussiness.Town, whereas in the service class you return Common.Town. 从我看到的,你有一个不正确的返回类型 - 你的城镇在商店是Bussiness.Town,而在服务类,你返回Common.Town。 Try returning Bussiness.Town in svc 尝试在svc中返回Bussiness.Town

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM