您的位置:首页 > 编程语言 > C#

c# 创建 XML-RPC服务

2012-08-10 15:44 417 查看
1、下载XML-PRC for .net 包,工程中引用CookComputing.XmlRpcV2.dll

2、接口文件

using System;
using System.Collections.Generic;
using System.Windows.Forms;

using SMSServer;
using CookComputing.XmlRpc;

namespace SMSServer.Service
{
#region 测试结构体

//忽略某字段
//public struct SampleStruct
//{
//    public string title;
//    public string link;
//    [XmlRpcMissingMapping(MappingAction.Ignore)]
//    public string description;
//}

//某字段必填
//[XmlRpcMissingMapping(MappingAction.Ignore)]
//public struct SampleStruct
//{
//    [XmlRpcMissingMapping(MappingAction.Error)]
//    public string title;
//    [XmlRpcMissingMapping(MappingAction.Error)]
//    public string link;
//    public string description;
//}

public struct sTest
{
public int a;
public string b;
public DateTime c;
[XmlRpcMissingMapping(MappingAction.Ignore)]
//public char d;
public byte[] e;
public bool f;
}
#endregion

public interface ISMSService
{
[XmlRpcMethod("SMSServer.Lee")]
string Lee(string str);
}

public class SMSService : MarshalByRefObject, ISMSService
{
public string Lee(string str)
{
return DateTime.Now.ToString() + ":" + str;
}
}
}


3、创建启动和关闭RPC服务

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Threading;
using System.IO;
using System.Runtime.InteropServices;

using CookComputing.XmlRpc;
using SMSServer.Service;

namespace SMSServer
{
public partial class FrmMain : Form
{

public HttpChannel channel;

public FrmMain()
{
InitializeComponent();
}

private void FrmMain_Load(object sender, EventArgs e)
{
//button1.Visible = false;
IDictionary props = new Hashtable();
props["name"] = "SMSHttpChannel";
props["port"] = 5678;
channel = new HttpChannel(
props,
null,
new XmlRpcServerFormatterSinkProvider()
);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(SMSService),
"sms.rem",
WellKnownObjectMode.Singleton);
}

private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
{
ChannelServices.UnregisterChannel(channel);
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: