asp.net 調用 Remoting

1.定義接口

1.1 IDuplexCalculator 操作接口

  public  interface ICalculatorCallback
{
void ShowResult(double x, double y, double result);
}

1.2 定義囘調接口 ICalculatorCallback

 public  interface ICalculatorCallback
{
void ShowResult(double x, double y, double result);
}

2.定義服務器端操作類 DuplexCalculatorRemoting,需要繼承 MarshalByRefObject ,

IDuplexCalculator
   public class DuplexCalculatorRemoting : MarshalByRefObject, IDuplexCalculator

{

public void Add(double x, double y, ICalculatorCallback callback)

{

//Console.WriteLine("Invoke the method Add({0},{1}).", x, y);

double result = x + y;

callback.ShowResult(x, y, result);

}

}

3.定義宿主

3.1   class Program
    {
        static void Main(string[] args)
        {
            System.Runtime.Remoting.RemotingConfiguration.Configure("Hosting.exe.config", false);
            Console.WriteLine("Calculator service has begun to listen... ...");
            Console.Read();
        }
    }

啟動 Remoting 服務。

3.2 定義配置文件

View Code
<configuration>
<system.runtime.remoting>
<application name="Calculator">
<service>
<wellknown mode="SingleCall"
type="DuplexCalculatorRemoting.DuplexCalculatorRemoting,DuplexCalculatorRemoting"
objectUri="DuplexCalculator.soap" />
</service>

<channels>
<channel ref="http" port="8080">
<serverProviders>
<provider ref="wsdl" />
<formatter ref="binary" typeFilterLevel="Full" />
</serverProviders>
<clientProviders>
<formatter ref="binary" />
</clientProviders>
</channel>
</channels>
</application>
</system.runtime.remoting>
</configuration>

其中 <application name="Calculator"> objectUri="DuplexCalculator.soap" />為客戶端條用的參數設置

http://localhost:8080/Calculator/DuplexCalculator.soap 客戶端調用的url

在這裡需要引用接口類,操作類。

4.客戶端調用

View Code
  public partial class _Default : System.Web.UI.Page
{

CalculatorCallbackHandler calcul = new CalculatorCallbackHandler();
protected void Page_Load(object sender, EventArgs e)
{
System.Runtime.Remoting.RemotingConfiguration.Configure(Server.MapPath("Web.config"), false);
calcul.myEvent += new EventHandler(calcul_myEvent);
InvocateDuplexCalculator("http://localhost:8080/Calculator/DuplexCalculator.soap");
//InvocateDuplexCalculator("http://localhost/DuplexCalculatorRemoting/DuplexCalculator.soap");

}

void calcul_myEvent(object sender, EventArgs e)
{
TextBox1.Text = ((CalculatorCallbackHandler)sender).Value;
}

private void InvocateDuplexCalculator(string remoteAddress)
{
IDuplexCalculator proxy = (IDuplexCalculator)Activator.GetObject(typeof(IDuplexCalculator), remoteAddress);
proxy.Add(1.0, 2.0, calcul);

}
}



客戶端需要實現囘調函數的接口的方法。如下:

View Code
    public class CalculatorCallbackHandler : MarshalByRefObject, ICalculatorCallback
{

public event EventHandler myEvent;
#region ICalculatorCallback Members

public void ShowResult(double x, double y, double result)
{
Value=string.Format("x + y = {2} where x = {0} and y = {1}", x, y, result);
if (myEvent != null) {
myEvent(this, null);
}
}

#endregion

public string Value { get; set; }
}



同樣客戶端需要在配置文件中進行設置

如下:

View Code
  <system.runtime.remoting>
<application>
<channels>
<channel ref="http" port="0">
<clientProviders>
<formatter ref="binary" />
</clientProviders>
<serverProviders>
<formatter ref="binary" typeFilterLevel="Full" />
</serverProviders>
</channel>
</channels>
</application>
</system.runtime.remoting>











posted @ 2012-04-02 20:53  無限遐想  阅读(345)  评论(0编辑  收藏  举报