Einfaches Beispiel zu Remoting mit Springframework .Net

Thomas Darimont

Erfahrenes Mitglied
Hallo,

hier mal ein einfaches Beispiel zu Remoting mit Springframework .Net. (Ein einfaches Beispiel zu konventionellem Remoting unter .Net findet man hier: http://www.tutorials.de/forum/net-w...66857-einfaches-beispiel-zu-net-remoting.html )
Hierbei haben wir zwei Projekte:
1) Server (beinhaltet die Service Implementierung und exportiert den Service als Server Activated Object (SAO))
2) Client (benutzt den Service über einen Remoting Proxy)

Die Springframework .Net Bibliotheken bekomment man von:
http://springframework.net/download.html

D:\stuff\.net\spring.net\1.1-M1\Spring.NET\bin\net\2.0\debug -> (der einfachheit halber alle assemblies Referenzieren)

Dokumentation zu Springframework.Net Remoting findet man hier:
file:///D:/stuff/.net/spring.net/1.1-M1/Spring.NET/doc/reference/html/remoting.html

Das Client Projekt hat einen Verweis auf das Server Projekt.

Unser Interface über den wir unseren Service Remote verfügbar machen wollen:
C#:
using System;
using System.Collections.Generic;
using System.Text;

namespace De.Tutorials.Training.Spring.Services
{
    public interface IBusinessService
    {
        string BusinessOperation(string argument);
    }
}

Unsere Service Implementierung:
C#:
using System;
using System.Collections.Generic;
using System.Text;

namespace De.Tutorials.Training.Spring.Services.Internal
{
    public class BusinessService : IBusinessService
    {
        

        public bool LoggingEnabled
        {
            get { return loggingEnabled; }
            set { loggingEnabled = value; }
        } private bool loggingEnabled;
    

        #region IBusinessService Member

        public string BusinessOperation(string argument)
        {
            if (LoggingEnabled)
            {
                Console.WriteLine("Executing business operation with: " + argument);
            }
            return "Upper: " + argument.ToUpper();
        }

        #endregion
    }
}

Unser Server:
C#:
using System;
using System.Collections.Generic;
using System.Text;
using Spring.Context;
using Spring.Context.Support;
using System.Runtime.Remoting;

namespace De.Tutorials.Training.Spring
{
    public class Server
    {
        static void Main(string[] args)
        {
            IApplicationContext applicationContext = ContextRegistry.GetContext();
            Console.WriteLine("Server started");
            Console.ReadLine();
        }
    }
}

Unsere App.config:
XML:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    </sectionGroup>
  </configSections>
  <spring>
    <context name="spring.root">
      <resource uri="config://spring/objects"/>
    </context>
    <objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd">
      <object name="businessService" type="De.Tutorials.Training.Spring.Services.Internal.BusinessService"
      singleton="true">
        <property name="loggingEnabled" value="true"/>
      </object>

      <object name="saoBusinessService" type="Spring.Remoting.SaoExporter, Spring.Services">
        <property name="TargetName" value="businessService" />
        <property name="ServiceName" value="RemotedBusinessService" />
      </object>

      <object type="Spring.Remoting.RemotingConfigurer, Spring.Services">
        <property name="filename" value="Server.exe.config"/>
      </object>
      
    </objects>
  </spring>

  <system.runtime.remoting>
    <application>
      <channels>
        <channel ref="tcp" port="8005"  />
      </channels>
    </application>
  </system.runtime.remoting>
</configuration>

Unser Client:
C#:
using System;
using System.Collections.Generic;
using System.Text;
using Spring.Context;
using Spring.Context.Support;
using De.Tutorials.Training.Spring.Services;
using System.Threading;

namespace Client
{
    public class Program
    {
        static void Main(string[] args)
        {
            IApplicationContext applicationContext = ContextRegistry.GetContext();
            IBusinessService businessService = (IBusinessService)applicationContext.GetObject("businessService");
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Client got response: " + businessService.BusinessOperation("bubu"+DateTime.Now.Ticks));
                Thread.Sleep(1000);
            }
        }
    }
}

Unsere App.config (für den Client):
XML:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    </sectionGroup>
  </configSections>
  <spring>
    <context name="spring.root">
      <resource uri="config://spring/objects"/>
    </context>
    <objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd">
      <object id="businessService" type="Spring.Remoting.SaoFactoryObject, Spring.Services">
        <property name="ServiceInterface" value="De.Tutorials.Training.Spring.Services.IBusinessService, Server" />
        <property name="ServiceUrl" value="tcp://localhost:8005/RemotedBusinessService" />
      </object>
    </objects>
  </spring>
</configuration>

Ausgabe:
Server:
Code:
Server started
Executing business operation with: bubu633182032608906250
Executing business operation with: bubu633182032619375000
Executing business operation with: bubu633182032629375000
Executing business operation with: bubu633182032639375000
...

Ausgabe Client:
Code:
Client got response: Upper: BUBU633182035942031250
Client got response: Upper: BUBU633182035952343750
Client got response: Upper: BUBU633182035962343750
Client got response: Upper: BUBU633182035972343750
Client got response: Upper: BUBU633182035982343750
...

Gruß Tom
 

Anhänge

Zuletzt bearbeitet von einem Moderator:
Zurück