1.创建类库项目.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RemotingObject
{
public class MyObject:MarshalByRefObject,IMyOject
{
#region IMyOject Members
public string SendMessage(string msg)
{
return "Hello " + msg;
}
#endregion
public override object InitializeLifetimeService()
{
return null;
}
public int add(int a, int b)
{
return a + b;
}
}
public interface IMyOject
{
string SendMessage(string msg);
}
}
生成类库DLL文件.
2. 在IIS建立网站或虚拟目录
指定Remoting的发布目录,例如,以d:/test做为网站或虚拟目录路径,在test下建立文件夹bin并将类库DLL文件复制到里面.
创建web.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<service>
<wellknown type="RemotingObject.MyObject,RemotingObject" objectUri="Class1.soap" mode="Singleton" />
</service>
<channels>
<channel ref="http"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>
IIS文件结构
../test
|---bin
|-ClassLibrary1.dll
|---web.config
(在此已经可以测试Remoting是否发布成功,在IE 输入http://localhost/Class1.soap?wsdl 可以看xml文件内容)
3. 客户端调用
web应用调用
在web.config中添加
<appSettings>
<add key="Class1url" value="http://localhost/Class1.soap"/>
</appSettings>
调用代码
protected void Button1_Click(object sender, EventArgs e)
{
string sdkConnect = ConfigurationSettings.AppSettings["Class1url"];
ClassLibrary1.Class1 app = (ClassLibrary1.Class1)Activator.GetObject(typeof(ClassLibrary1.Class1), sdkConnect);
TextBox1.Text = app.add(1,2).ToString();
}
控制台调用
app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key= "ServiceURL" value="http://localhost/Class1.soap"/>
</appSettings>
</configuration>
调用代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
namespace Clienttest
{
class Program
{
static void Main(string[] args)
{
ClassLibrary1.Class1 app = (ClassLibrary1.Class1)Activator.GetObject(typeof(ClassLibrary1.Class1), System.Configuration.ConfigurationSettings.AppSettings["ServiceURL"]);
Console.WriteLine(app.add(1, 2));
Console.ReadLine();
}
}
}