可访问性不一致:返回类型比RESTful服务中的方法更难访问

时间:2017-01-18 09:15:09

标签: c# linq wcf restful-url

以下是我的完整源代码。

任何人都可以建议如何解决以下错误:

  

错误2可访问性不一致:返回类型' ConsumingApp.Dept'不如方法' ConsumingApp.Form1.CreateDept(ConsumingApp.Dept)'

  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Net;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;

 using System.Runtime.Serialization.Json;
 using System.IO;
 namespace ConsumingApp
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

        var client = new WebClient();
        client.Headers.Add("Accept", "application/json");

        var result =   client.DownloadString("http://localhost:20642/ComRESTfulService.svc/Depts");
        var serializer = new DataContractJsonSerializer(typeof(Dept[]));
        Dept[] resultObject;
        using (var stream = new MemoryStream(Encoding.ASCII.GetBytes(result)))
        {
            resultObject = (Dept[])serializer.ReadObject(stream);
        }
        DataTable dt = new DataTable();
        dt.Columns.Add("DeptId", typeof(int));
        dt.Columns.Add("DeptName", typeof(string));

        foreach (var item in resultObject)
        {
            int DeptId = item.DeptId;
            string DeptName = item.DeptName;
            DataRow row = dt.NewRow();
            row[0] = DeptId;
            row[1] = DeptName;
            dt.Rows.Add(row);
        }

        dataGridView1.DataSource = dt;

    }

    public Dept CreateDept(Dept dept)
    {
        return SendDataToServer("http://localhost:63588/PunService.svc/Depts", "POST", dept);
    }
    private T SendDataToServer<T>(string endpoint, string method, T dept)
    {
        var request = (HttpWebRequest)HttpWebRequest.Create(endpoint);
        request.Accept = "application/json";
        request.ContentType = "application/json";
        request.Method = method;
        var serializer = new DataContractJsonSerializer(typeof(T));
        var requestStream = request.GetRequestStream();
        serializer.WriteObject(requestStream, dept);
        requestStream.Close();
        var response = request.GetResponse();
        if (response.ContentLength == 0)
        {
            response.Close();
            return default(T);
        }
        var responseStream = response.GetResponseStream();
        var responseObject = (T)serializer.ReadObject(responseStream);
        responseStream.Close();
        return responseObject;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Dept dept = new Dept
        {
            DeptId = Convert.ToInt32(textBox1.Text),
            DeptName = textBox2.Text

        };
        CreateDept(dept);

    }

  }
}`

1 个答案:

答案 0 :(得分:0)

您的Dept课程不公开。您无法从公共方法返回非公共类型。

如果没有为非嵌套类指定可访问性,则默认使用internal。