asp.net 調用 Remoting

來源:互聯網
上載者:User

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>

相關關鍵詞:
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.