使用c#使用Datasnap服务器

时间:2014-08-25 13:42:40

标签: c# nullreferenceexception datasnap

我有一个Datasnap Server,我需要使用Windows Phone消费数据。

TSituacaoProxy类,当我发送参数(Id)时,返回必须是JSON对象。

但是显示错误:

  

对象引用未设置为对象的实例。

我的代码:

 private void clique(object sender, RoutedEventArgs e)
    {

        DSRESTConnection connection = new DSRESTConnection();
        connection.setHost("10.0.0.10");
        connection.setPort(8080);
        connection.setProtocol("http");
        connection.setUserName("master");
        connection.setPassword("C28861C83BD5397187FD55D04C2547F9");
        connection.setContext("datasnap/REST");

        DSProxy.TSituacaoProxy Proxy = new DSProxy.TSituacaoProxy(connection, ExceptionCallback);
        TJSONObject jObject = TJSONObject.Parse("22");

        Proxy.Loader(jObject, (TJSONObject Result) =>
        {
            MessageBox.Show(Result.ToString()); 

        }, ExceptionCallback);

    }


    public void ExceptionCallback(Exception e)
    {
        MessageBox.Show(e.Message);
    }

TSituacaoProxy上课:

public class TSituacaoProxy : DSAdmin
{
  public TSituacaoProxy(DSRESTConnection Connection, ExceptionCallback ExCal)
      : base(Connection, ExCal)
  {
  }

  private DSRESTParameterMetaData[] TSituacaoProxy_teste_Metadata;
  private DSRESTParameterMetaData[] get_TSituacaoProxy_teste_Metadata()
  {
    if (TSituacaoProxy_teste_Metadata == null)
    {
      TSituacaoProxy_teste_Metadata = new DSRESTParameterMetaData[]
      {
        new DSRESTParameterMetaData("xValue", DSRESTParamDirection.Input, DBXDataTypes.WideStringType, "string"),
        new DSRESTParameterMetaData("", DSRESTParamDirection.ReturnValue, DBXDataTypes.WideStringType, "string"),
      };
    }
    return TSituacaoProxy_teste_Metadata;
  }

Loader方法:

public delegate void LoaderCallback(TJSONObject Result);

  public void Loader(TJSONObject xId, LoaderCallback callback = null, ExceptionCallback ExCal = null)
  {
    DSRESTCommand cmd = getConnection().CreateCommand();
    cmd.setRequestType(DSHTTPRequestType.POST);
    cmd.setText("TSituacaoProxy.Loader");
    cmd.prepare(get_TSituacaoProxy_Loader_Metadata());
    InternalConnectionDelegate LoaderDel = () =>
    {
      if (callback != null)
      {
        try
        {
          callback.DynamicInvoke((TJSONObject)cmd.getParameter(1).getValue().GetAsJSONValue());
        }
        catch (Exception ex)
        {
          if (ExCal != null) getConnection().syncContext.Send(new SendOrPostCallback(x => ExCal.DynamicInvoke(ex.InnerException)), null);
          else getConnection().syncContext.Send(new SendOrPostCallback(x => BaseExCal.DynamicInvoke(ex.InnerException)), null);
        }
      }
    };
    cmd.getParameter(0).getValue().SetAsJSONValue(xId);
    getConnection().execute(cmd, this, LoaderDel, ExCal);
  }
  private DSRESTParameterMetaData[] TSituacaoProxy_Select_Metadata;
  private DSRESTParameterMetaData[] get_TSituacaoProxy_Select_Metadata()
  {
    if (TSituacaoProxy_Select_Metadata == null)
    {
      TSituacaoProxy_Select_Metadata = new DSRESTParameterMetaData[]
      {
        new DSRESTParameterMetaData("xAssorts", DSRESTParamDirection.Input, DBXDataTypes.JsonValueType, "TJSONArray"),
        new DSRESTParameterMetaData("", DSRESTParamDirection.ReturnValue, DBXDataTypes.JsonValueType, "TJSONArray"),
      };
    }
    return TSituacaoProxy_Select_Metadata;
  }

1 个答案:

答案 0 :(得分:0)

更改dsrestconnection.cs中的代码

        HttpWebRequest Client = (HttpWebRequest)WebRequest.Create(URL + "?" + DateTime.Now.Ticks.ToString());

到这个

HttpWebRequest Client =(HttpWebRequest)WebRequestCreator.ClientHttp.Create(new Uri(URL +“?”+ DateTime.Now.Ticks.ToString()));