没有实现接口成员

时间:2013-08-17 16:36:30

标签: c# asp.net wcf

您好我有以下代码,但我一直收到错误:

'JsonWcfService.GetVenues'没有实现接口成员'GetVenuesByLocation(string search)'。

我对C和.Net相当陌生,所以大部分内容都是剪切和粘贴的。

非常感谢您的帮助。

IGetVenues.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;

namespace JsonWcfService
{
    [ServiceContract]
    public interface IGetVenues
    {
        [OperationContract]
        //attribute for returning JSON format
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "json/Venues/search={search}")]
        //method
        List<Venue> GetAllVenuesMethod(string search);

        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "json/Venues/location={search}")]
        List<Venueloc> GetVenuesByLocation(string search); 
    }
}

GetVenues.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SqlClient;
namespace JsonWcfService
{
    public class GetVenues : IGetVenues 
    {
        public List<Venue> GetAllVenuesMethod(string search)
        {
            List<Venue> mylist = new List<Venue>();

            using (SqlConnection conn = new SqlConnection("server=*****;database=******;Trusted_Connection=True;"))
            {
                conn.Open();
                string cmdStr = String.Format("Select id,name,address1,town,postcode,lon,lat from venuesearch WHERE searchterm like @searchterm");
                SqlCommand cmd = new SqlCommand(cmdStr, conn);
                cmd.Parameters.Add(new SqlParameter("searchterm", "%" + Convert.ToString(search) + "%"));
                SqlDataReader rd = cmd.ExecuteReader();
                if (rd.HasRows)
                {
                    while (rd.Read())
                        mylist.Add(new Venue(
                            rd.GetInt32(0),
                            //rd.GetString(1),
                            rd.IsDBNull(1) ? null : rd.GetString(1),
                            //rd.GetString(2), 
                            rd.IsDBNull(2) ? null : rd.GetString(2),
                            //rd.GetString(3),
                            rd.IsDBNull(3) ? null : rd.GetString(3),
                            //rd.GetString(4)
                            rd.IsDBNull(4) ? null : rd.GetString(4),
                            //rd.GetString(4)
                            rd.IsDBNull(5) ? 0 : rd.GetDecimal(5),
                            //rd.GetString(4)
                            rd.IsDBNull(6) ? 0 : rd.GetDecimal(6)
                            ));
                }
                conn.Close();
            }

            return mylist;
        }
    }

    [DataContract]

    public class Venue
    {
        [DataMember]
        public Int32 id { get; set; }
        [DataMember]
        public string name { get; set; }
        [DataMember]
        public string address1 { get; set; }
        [DataMember]
        public string town { get; set; }
        [DataMember]
        public string postcode { get; set; }
        [DataMember]
        public Decimal lon { get; set; }
        [DataMember]
        public Decimal lat { get; set; }
        public Venue(int venid, string venname, string venaddress1, string ventown, string venpostcode, decimal venlon, decimal venlat)
        {
            id = venid;
            name = venname;
            address1 = venaddress1;
            town = ventown;
            postcode = venpostcode;
            lon = venlon;
            lat = venlat;

        }

        public List<Venueloc> GetVenuesByLocation(string search)
        {
            List<Venueloc> mylist = new List<Venueloc>();

            using (SqlConnection conn = new SqlConnection("server=***;database=******;Trusted_Connection=True;"))
            {
                conn.Open();
                string cmdStr = String.Format("Select id,name,address1,town,postcode,lon,lat from venuesearch WHERE searchterm like @searchterm");
                SqlCommand cmd = new SqlCommand(cmdStr, conn);
                cmd.Parameters.Add(new SqlParameter("searchterm", "%" + Convert.ToString(search) + "%"));
                SqlDataReader rd = cmd.ExecuteReader();
                if (rd.HasRows)
                {
                    while (rd.Read())
                        mylist.Add(new Venueloc(
                            rd.GetInt32(0),
                            //rd.GetString(1),
                            rd.IsDBNull(1) ? null : rd.GetString(1),
                            //rd.GetString(2), 
                            rd.IsDBNull(2) ? null : rd.GetString(2),
                            //rd.GetString(3),
                            rd.IsDBNull(3) ? null : rd.GetString(3),
                            //rd.GetString(4)
                            rd.IsDBNull(4) ? null : rd.GetString(4),
                            //rd.GetString(4)
                            rd.IsDBNull(5) ? 0 : rd.GetDecimal(5),
                            //rd.GetString(4)
                            rd.IsDBNull(6) ? 0 : rd.GetDecimal(6)
                            ));
                }
                conn.Close();
            }

            return mylist;
        }
    }

    [DataContract]

    public class Venueloc
    {
        [DataMember]
        public Int32 id { get; set; }
        [DataMember]
        public string name { get; set; }
        [DataMember]
        public string address1 { get; set; }
        [DataMember]
        public string town { get; set; }
        [DataMember]
        public string postcode { get; set; }
        [DataMember]
        public Decimal lon { get; set; }
        [DataMember]
        public Decimal lat { get; set; }
        public Venueloc(int venid, string venname, string venaddress1, string ventown, string venpostcode, decimal venlon, decimal venlat)
        {
            id = venid;
            name = venname;
            address1 = venaddress1;
            town = ventown;
            postcode = venpostcode;
            lon = venlon;
            lat = venlat;

        }
    }
}

第一个功能有效但第二个功能没有。

2 个答案:

答案 0 :(得分:5)

您的班级GetVenues未实施错误消息中提到的方法。你到底在哪里? GetVenuesByLocation(string search)在课程Venue中定义。

我注意到这个问题开始引起人们的兴趣。这里的问题很简单:你的类没有实现(即使它是abstract,你必须定义它们)接口中定义的所有方法。您的IDE应该在编译之前警告您。

答案 1 :(得分:0)

第二个函数在Venue类中不在GetVenues中,将其移动到GetVenues