如何使用C#构建XMLRPC服务?

时间:2016-06-24 09:54:52

标签: xml-rpc

我是XMLRPC的新手。我曾经使用过soap而不是XMLRPC。不幸的是,我需要构建一个XMLRPC服务,因为我正在处理的项目需要它。我一直在网上寻找最后一周得到一些帮助,我确实得到了一些像XML-RPC.net这样的链接,这是Cook Computing建立xmlrpc服务的库,但是他们提供的文档还不足以帮助新手像我这样的。我试了但是失败了。我需要知道如何开始的基础知识 - 使用什么类型的文件扩展名来调用服务等。我尝试使用通用处理程序.ashx来实现xml-rpc.net库,但它不起作用并且它一直只给出响应中的故障代码和故障字符串。如果您知道如何使用此库,请告诉我或分享不同的方式。请帮帮我。我真的很感激。感谢。

.ashx代码如下。请让我知道我哪里错了:

    <%@ WebHandler Language="C#" Class="xmlrpc" %>     
using System;
using System.Web;
using System.Data;
using System.Data.OleDb;


using CookComputing.XmlRpc;

public struct Menu
{
    public string MenuItem;
    public long SessionID;
}

public struct Req
{
   public string SERVICE_KEY;
   public string MOBILE_NUMBER;
   public string SESSION_ID;
   public string SEQUENCE;
   public string USSD_BODY;
}

public struct Resp
{
   public string SESSION_ID;
   public string SEQUENCE;
   public string USSD_BODY;
   public string REQUEST_TYPE;
   public string END_OF_SESSION;

}

[XmlRpcUrl("http://[IP]/xmlrpc/xmlrpc.ashx")] 
[XmlRpcService(Description ="This service produces the functionality of USSD Menu.")]
public class xmlrpc : XmlRpcService
{

    [XmlRpcMethod("xmlrpc.getMenu")]
    [return: XmlRpcReturnValue(Description = "USSD Menu Response")]
   public Resp getMenu([XmlRpcParameter(Description = "USSD Request")]Req request)
    {
        try
        {
            MenuService.Service objservice = new MenuService.Service();
            MenuService.Menu objmenu = new MenuService.Menu();

           Resp resp = new Resp();

            OleDbConnection con = new OleDbConnection(DAL.GetConnectionString());
            DataTable dt = new DataTable();

            string query = @"select * from 
xxxxx us where us.Session_Id = '"+request.SESSION_ID+@"' 
                            and us.MOBILE_NUMBER='"+request.MOBILE_NUMBER+"' and us.End_Of_Session = 'FALSE' and us.IsActive = '1'";

            OleDbDataAdapter adputl = new OleDbDataAdapter(query, con);
            adputl.Fill(dt);

            if (dt.Rows.Count == 0)
            {
                int res = DAL.InsertUTLSession(request.SERVICE_KEY, request.MOBILE_NUMBER, request.SESSION_ID, request.SEQUENCE, request.USSD_BODY);

                if (res > 1)
                {

                }

                objmenu = objservice.USSDMenu(request.MOBILE_NUMBER, request.USSD_BODY, "0");

                resp.END_OF_SESSION = "FALSE";
                resp.REQUEST_TYPE = "REQUEST";
                resp.SEQUENCE = request.SEQUENCE;
                resp.SESSION_ID = request.SESSION_ID;
                resp.USSD_BODY = objmenu.MenuItem;

            }
            else
            {
                DataRow dr = dt.Rows[0];

                objmenu = objservice.USSDMenu(request.MOBILE_NUMBER, request.USSD_BODY, dr["MenuSessionID"].ToString());

                int res = DAL.UpdateUTLSession(request.SERVICE_KEY, request.MOBILE_NUMBER, request.SESSION_ID, request.SEQUENCE, request.USSD_BODY);

                resp.END_OF_SESSION = dr["End_Of_Session"].ToString();
                resp.REQUEST_TYPE = "RESPONSE";
                resp.SEQUENCE = request.SEQUENCE;
                resp.SESSION_ID = request.SESSION_ID;
                resp.USSD_BODY = objmenu.MenuItem;

            }


            return resp;
        }
        catch(Exception ex)
        {
            string ConnectString = DAL.GetConnectionString();
            OleDbConnection connec = new OleDbConnection(ConnectString);
            //Connection Open

            OleDbCommand cmd = new OleDbCommand("Insert into errorlogs(INDEXPTR,mobile,remarks,timestamp) Values ((select Max(nvl(INDEXPTR,0))+1 from inc_errorlogs),'" + request.MOBILE_NUMBER + "','" + ex.Message.ToString() + "', sysdate, '" + "XMLRPC-Service~" + request.SESSION_ID + "~" + request.USSD_BODY + "')", connec);
            cmd.Connection = connec;
            cmd.CommandType = CommandType.Text;
            connec.Open();
            cmd.ExecuteNonQuery();


            Resp resp = new Resp();
             resp.END_OF_SESSION = "TRUE";
            resp.REQUEST_TYPE = "REQUEST";
            resp.SEQUENCE = "0";
            resp.SESSION_ID = "";
            resp.USSD_BODY = ex.Message;

            return resp; 
               // (ex.Message); 

        }

    }


}

我致电此服务时收到的回复:

<?xml version="1.0"?>

<methodResponse>

  <fault>

    <value>

      <struct>

        <member>

          <name>faultCode</name>

          <value>

            <i4>0</i4>

          </value>

        </member>

        <member>

          <name>faultString</name>

          <value>

            <string>Object reference not set to an instance of an object.</string>

          </value>

        </member>

      </struct>

    </value>

  </fault>

</methodResponse>

0 个答案:

没有答案