【问题标题】:WCF RESTful Service ErrorWCF RESTful 服务错误
【发布时间】:2015-07-27 10:53:10
【问题描述】:

您好,我遇到了这个错误,不知道如何修复它。我有一个 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; }
    }
}

【问题讨论】:

    标签: c# web-services wcf rest


    【解决方案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 ;
            }
    

    【讨论】:

      【解决方案2】:

      您需要根据需要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;
      
                  }
      

      【讨论】:

        猜你喜欢
        • 2013-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-11
        • 2011-08-31
        • 2011-07-11
        相关资源
        最近更新 更多