接口不能声明类型

时间:2014-12-30 18:47:20

标签: c# .net wcf class types

我的问题:我正在尝试按照本教程关于与Windows窗体链接并使用数据库的wcf服务:http://www.c-sharpcorner.com/uploadfile/rohatash/insert-and-display-data-in-datagridview-in-windows-forms-app/

当我运行代码时,我得到2个错误: 'IService1':接口不能声明类型 'UserDetails':接口不能声明类型。

我在stackoverflow和google上寻找答案但是我唯一的问题是没有答案。还有一些评论告诉它适用于他们,所以它是一个代码问题或者可能是配置问题。 我把我的2个文件的代码IService.cs和Service.cs放在这里: IService.cs

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

namespace WCFServiceForInsert
{
    // REMARQUE : vous pouvez utiliser la commande Renommer du menu Refactoriser pour changer le nom d'interface "IService" à la fois dans le code et le fichier de configuration.
    [ServiceContract]
    public interface IService
    {
        // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
        [ServiceContract]
        public interface IService1
        {
            [OperationContract]
            string InsertUserDetails(UserDetails userInfo);

            [OperationContract]
            DataSet SelectUserDetails();
        }

        // Use a data contract as illustrated in the sample below to add composite types to service operations.
        [DataContract]
         public class UserDetails
        {
            int id;
            string title;
            string salary;
            string benefits;
            string keywords;
            string jobType;
            string location;
            string startDate;
            string description;
            string recruitmentAgency;
            string agencyContact;
            string agencyPhone;
            string agencyEmail;
            string jobRef;
            string datePosted;
            string dateExpire;

            [DataMember]
            public int JobID
            {
                get { return id; }
                set { id = value; }
            }

            [DataMember]
            public string Title
            {
                get { return title; }
                set { title = value; }
            }
            [DataMember]
            public string Salary
            {
                get { return salary; }
                set { salary = value; }
            }
            [DataMember]
            public string Benefits
            {
                get { return benefits; }
                set { benefits = value; }
            }
            [DataMember]
            public string Keywords
            {
                get { return keywords; }
                set { keywords = value; }
            }
            [DataMember]
            public string JobType
            {
                get { return jobType; }
                set { jobType = value; }
            }
            [DataMember]
            public string Location
            {
                get { return location; }
                set { location = value; }
            }
            [DataMember]
            public string StartDate
            {
                get { return startDate; }
                set { startDate = value; }
            }
            [DataMember]
            public string Description
            {
                get { return description; }
                set { description = value; }
            }
            [DataMember]
            public string RecruitmentAgency
            {
                get { return recruitmentAgency; }
                set { recruitmentAgency = value; }
            }
            [DataMember]
            public string AgencyContact
            {
                get { return agencyContact; }
                set { agencyContact = value; }
            }
            [DataMember]
            public string AgencyPhone
            {
                get { return agencyPhone; }
                set { agencyPhone = value; }
            }
            [DataMember]
            public string AgencyEmail
            {
                get { return agencyEmail; }
                set { agencyEmail = value; }
            }
            [DataMember]
            public string JobRef
            {
                get { return jobRef; }
                set { jobRef = value; }
            }
            [DataMember]
            public string DatePosted
            {
                get { return datePosted; }
                set { datePosted = value; }
            }
            [DataMember]
            public string DateExpire
            {
                get { return dateExpire; }
                set { dateExpire = value; }
            }
        }
    }

}

Service.cs文件:

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

namespace WCFServiceForInsert
{
    // REMARQUE : vous pouvez utiliser la commande Renommer du menu Refactoriser pour changer le nom de classe "Service" à la fois dans le code, le fichier svc et le fichier de configuration.
    public class Service : IService
    {
        public DataSet SelectUserDetails()
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=registration;User ID=sa;Password=wintellect");
            con.Open();
            SqlCommand cmd = new SqlCommand("Select * from Table", con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            cmd.ExecuteNonQuery();
            con.Close();
            return ds;
        }

        public string InsertUserDetails(IService.UserDetails userInfo)
        {
            string Message;
            Message = "";
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=registration;User ID=sa;Password=wintellect");
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into RegistrationTable(Title,Salary,Benefits,Keywords,JobType,Location,StartDate,Description,RecruitmentAgency,AgencyContact,AgencyPhone,AgencyEmail,JobRef,DatePosted,DateExpire) values(@Title,@Salary,@Benefits,@Keywords,@JobType,@Location,@StartDate,@Description,@RecruitmentAgency,@AgencyContact,@AgencyPhone,@AgencyEmail,@JobRef,@DatePosted,@DateExpire)", con);
            cmd.Parameters.AddWithValue("@Title", userInfo.Title);
            cmd.Parameters.AddWithValue("@Salary", userInfo.Salary);
            cmd.Parameters.AddWithValue("@Benefits", userInfo.Benefits);
            cmd.Parameters.AddWithValue("@Keywords", userInfo.Keywords);
            cmd.Parameters.AddWithValue("@JobType", userInfo.JobType);
            cmd.Parameters.AddWithValue("@Location", userInfo.Location);
            cmd.Parameters.AddWithValue("@StartDate", userInfo.StartDate);
            cmd.Parameters.AddWithValue("@Description", userInfo.Description);
            cmd.Parameters.AddWithValue("@RecruitmentAgency", userInfo.RecruitmentAgency);
            cmd.Parameters.AddWithValue("@AgencyContact", userInfo.AgencyContact);
            cmd.Parameters.AddWithValue("@AgencyPhone", userInfo.AgencyPhone);
            cmd.Parameters.AddWithValue("@AgencyEmail", userInfo.AgencyEmail);
            cmd.Parameters.AddWithValue("@JobRef", userInfo.JobRef);
            cmd.Parameters.AddWithValue("@DatePosted", userInfo.DatePosted);
            cmd.Parameters.AddWithValue("@DateExpire", userInfo.DateExpire);
            int result = cmd.ExecuteNonQuery();
            if (result == 1)
            {
                Message = userInfo.Title + " Details inserted successfully";
            }
            else
            {
                Message = userInfo.Title + " Details not inserted successfully";
            }
            con.Close();
            return Message;
        }
    }
}

1 个答案:

答案 0 :(得分:3)

只需将您的类移到Interface定义之外。此外,最好不要嵌套接口(对于C#,通常最佳做法是每个文件使用1个类/接口)

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

namespace WCFServiceForInsert
{

// REMARQUE : vous pouvez utiliser la commande Renommer du menu Refactoriser pour changer le nom d'interface "IService" à la fois dans le code et le fichier de configuration.
// There is not a reason to keep this IService unless it is necessary for some other piece of your code
[ServiceContract]
public interface IService
{


}

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string InsertUserDetails(UserDetails userInfo);

        [OperationContract]
        DataSet SelectUserDetails();
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    // You must declare this outside of the Interface definition.
    [DataContract]
     public class UserDetails
    {
        int id;
        string title;
        string salary;
        string benefits;
        string keywords;
        string jobType;
        string location;
        string startDate;
        string description;
        string recruitmentAgency;
        string agencyContact;
        string agencyPhone;
        string agencyEmail;
        string jobRef;
        string datePosted;
        string dateExpire;

        [DataMember]
        public int JobID
        {
            get { return id; }
            set { id = value; }
        }

        [DataMember]
        public string Title
        {
            get { return title; }
            set { title = value; }
        }
        [DataMember]
        public string Salary
        {
            get { return salary; }
            set { salary = value; }
        }
        [DataMember]
        public string Benefits
        {
            get { return benefits; }
            set { benefits = value; }
        }
        [DataMember]
        public string Keywords
        {
            get { return keywords; }
            set { keywords = value; }
        }
        [DataMember]
        public string JobType
        {
            get { return jobType; }
            set { jobType = value; }
        }
        [DataMember]
        public string Location
        {
            get { return location; }
            set { location = value; }
        }
        [DataMember]
        public string StartDate
        {
            get { return startDate; }
            set { startDate = value; }
        }
        [DataMember]
        public string Description
        {
            get { return description; }
            set { description = value; }
        }
        [DataMember]
        public string RecruitmentAgency
        {
            get { return recruitmentAgency; }
            set { recruitmentAgency = value; }
        }
        [DataMember]
        public string AgencyContact
        {
            get { return agencyContact; }
            set { agencyContact = value; }
        }
        [DataMember]
        public string AgencyPhone
        {
            get { return agencyPhone; }
            set { agencyPhone = value; }
        }
        [DataMember]
        public string AgencyEmail
        {
            get { return agencyEmail; }
            set { agencyEmail = value; }
        }
        [DataMember]
        public string JobRef
        {
            get { return jobRef; }
            set { jobRef = value; }
        }
        [DataMember]
        public string DatePosted
        {
            get { return datePosted; }
            set { datePosted = value; }
        }
        [DataMember]
        public string DateExpire
        {
            get { return dateExpire; }
            set { dateExpire = value; }
        }
    }


 }