Web Services 分布式实战

来源:互联网 发布:计算机网络视频知乎 编辑:程序博客网 时间:2024/05/21 19:37

一、先建三个项目:

HelloLibary(类库,需要部署的代码放在这里)

HelloServer(控制台程序,用于侦听请求)

HelloClient(控制台程序,用于请求远程对象)

使用控制台程序的原因是简单快捷,呵呵

具体代码如下:

1.HelloLibary:

using System;
using System.Collections.Generic;
using System.Text;

namespace HelloLibary
{
    public class Hello   :   MarshalByRefObject
    {
        public Hello()
        {
            Console.WriteLine("Hello ctor Called");
        }
        public string SayHello(string name)
        {
       Console.WriteLine("Hello.SayHello()");//当远程对象被调用时,此行将在服务器端显示
            return "Hello," + name;
        }
    }
}

2.HelloServer:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;//注意要添加System.Runtime.Remoting的引用
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;

using ServerMain;//要引用用于元数据调用

namespace HelloServer
{
    class Program
    {    

    static void Main(string[] args)
        {

            HttpChannel channel = new HttpChannel(13101);
            ChannelServices.RegisterChannel((IChannel)channel);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(HelloLibary.Hello), "SayHello",WellKnownObjectMode.Singleton);

            Console.WriteLine("the Server is listening , press anykey to exit!");
            Console.ReadLine();
        }

    }
}

3.HelloClient:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using ServerMain;//要引用用于元数据调用
namespace HelloClient
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpChannel channel = new HttpChannel();
            ChannelServices.RegisterChannel((IChannel)channel);
            Object remoteHello = Activator.GetObject(typeof(HelloLibary.Hello), "
http://localhost:13101/SayHello");  //地址要写正确,SayHello就是上面定义的objectUri
Hello hello = (Hello)remoteHello;//客户端必须知道元数据类型才能正确获得调用远程对象
            Console.WriteLine(hello.SayHello("a9fs3"));
            Console.ReadLine();
        }
    }
}

由以上配置可正确得到结果:

HelloServer控制台输出:

the Server is listening , press anykey to exit!

Hello ctor Called

Hello.SayHello()

HelloClient控制台输出:

Hello,a9fs3

二、通过配置文件实现上述功能

1、HelloServer端:

 using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;

namespace HelloServer
{
    class Program
    {
        static void Main(string[] args)
        {
            string filename = "HelloServer.exe.config";

            RemotingConfiguration.Configure(filename);
            Console.WriteLine("the Server is listening , press anykey to exit!");
            Console.ReadLine();
        }
    }
}

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.runtime.remoting>
    <application>
      <service>
        <wellknow mode="SingleCall" type="HelloLibary.Hello, HelloLibary" objectUri="SayHello"/>
      </service>
      <channels>
        <channel  port="13101" ref="http"/><!-- 模板文件在machine.config中定义好了,因此可以直接使用-->
      </channels>
    </application>
  </system.runtime.remoting>
</configuration>

2、HelloClient端:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using ServerMain;
namespace HelloClient
{
    class Program
    {
        static void Main(string[] args)
        {

            string filename = "HelloClient.exe.config";
            RemotingConfiguration.Configure(filename);
            Hello hello = new Hello();
            Console.WriteLine(hello.SayHello("a9fs3"));

            Console.ReadLine();
        }
    }
}

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.runtime.remoting>
    <application>
      <client>
        <wellknow type="HelloLibary.Hello, HelloLibary" url="
http://localhost:13101/SayHello"/>
      </client>
      <channels>
        <channel ref="http"/><!-- 模板文件在machine.config中定义好了,因此可以直接使用-->
      </channels>
    </application>
  </system.runtime.remoting>
</configuration>

运行的结果出乎意料,根本就调用不到远程对象,到底为什么呢?而且尝试把远程对象驻留在Asp.net中,也是没有成功,我试着找了很多原因,包括: 1、远程对象是否可跨域访问,当然可访问。2、通道、协议和端口是否一致、正确,审查也是没问题。3、IIS可否常运行4、通过http://localhost/serveragency/website/hello.soap?wsdl访问结果是抛出异常:未能找到服务

最终觉得可能是我的系统问题,因为我查看了一下machine.config文件,.net Framework2.0里面根本没有http和tcp模板,只有.net Framework 1.1里才有,不知道是不是这个原因?

 

 

 

 

 

 

 

 

 

原创粉丝点击