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); } } }