c# - WCF RESTful服务错误

c# - WCF RESTful服务错误,第1张

嗨,我遇到了这个错误,无法弄清楚如何修复它。我有一个RESTful WCF服务试图从SQL Server数据库中检索数据。感谢。

Error   1   'RestService.RestServiceImpl.GetCompany(string)': not all code paths return a value 

我的编码

RestServiceImpl.svc.cs

    public class RestServiceImpl : IRestServiceImpl
    {
        public string XMLDATA(string id)
        {
            return ("You Requested product"   id);
        }
        public string JSONDATA(string id)
        {
            return ("You Requested product"   id);
        }        //ERROR underlined at GetCompany 
        public Company GetCompany(string CompID)
        {
            Company comp = new Company();
            {
                SqlConnection con = new SqlConnection();
                con.ConnectionString = "";
                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT COMPANYNAME FROM tblCompany", con);
                con.Open();

                SqlDataReader reader = cmd.ExecuteReader();
            }

        }
    }
}

IRestService.cs

  public interface IRestServiceImpl
    {
        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "xml/{id}")]
        string XMLDATA(string id);

        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "json/{id}")]
        string JSONDATA(string id);

        [OperationContract]
        [WebGet(UriTemplate = "/GetCompany/{CompID}",
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json)]
        Company GetCompany(string CompID);

    }

    [DataContract]
    public class Company
    {
        [DataMember]
        public string CompID { get; set; }

        [DataMember]
        public string Company { get; set; }
    }
}

最佳答案:

2 个答案:

答案 0 :(得分:1)

您需要更改方法以返回对象类型'公司'

 public Company GetCompany(string COMPANYNAME)
        {
            Company comp = new Company();
            {
                SqlConnection con = new SqlConnection();
                con.ConnectionString = "";
                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT COMPANYNAME FROM tblCompany", con);
                con.Open();

                SqlDataReader reader = cmd.ExecuteReader();
                //Your implementation
            }
            return comp ;
        }

答案 1 :(得分:1)

您需要按照需要类Company对象的方法返回: -

 public Company GetCompany(string COMPANYNAME)
            {
                Company comp = new Company();
                {
                    SqlConnection con = new SqlConnection();
                    con.ConnectionString = "";
                    con.Open();
                    SqlCommand cmd = new SqlCommand("SELECT COMPANYNAME FROM tblCompany", con);
                    con.Open();

                    SqlDataReader reader = cmd.ExecuteReader();
                }

               return comp;

            }
本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复