zoukankan      html  css  js  c++  java
  • Remoting与Windows服务

    一、创建远程对象,建立项目类库

    namespace RemoteObject
    {
        public class MyObject : MarshalByRefObject
        {
            public DataSet GetData()
            {
                SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["strconn"]);
                SqlDataAdapter da = new SqlDataAdapter("select * from authors", conn);
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }

        }

    }

    二、服务端、建立控制台应用程序
    using System.Runtime.Remoting;

    namespace RemoteServer
    {
        class MyServer
        {
            [STAThread]
            static void Main(string[] args)
            {
                RemotingConfiguration.Configure("RemoteServer.exe.config");
                Console.ReadLine();
            }
        }
    }

    建立配置文件

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <add key="strconn" value="server=(local);uid=sa;pwd=123;database=pubs" />
      </appSettings>
      <system.runtime.remoting>
        <application name="RemoteServer">
          <service>
            <wellknown type="RemoteObject.MyObject,RemoteObject" objectUri="RemoteObject.MyObject"
                mode="Singleton" />
          </service>
          <channels>
            <channel ref="tcp" port="9999"/>
          </channels>
        </application>
      </system.runtime.remoting>
    </configuration>

    三、客户端:建立控制台项目:RemoteClient

    namespace RemoteClient

        class MyClient
        {
            [STAThread]
            static void Main(string[] args)
            {
                RemoteObject.MyObject app = (RemoteObject.MyObject)Activator.GetObject(typeof(RemoteObject.MyObject), System.Configuration.ConfigurationSettings.AppSettings["ServiceURL"]);
                DataTable dt = app.GetData().Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    Console.WriteLine(dr["au_id"] + " " + dr["au_lname"]);
                }
                Console.ReadLine();

            }
        }
    }

    建立配置文件

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <add key="ServiceURL" value="tcp://localhost:9999/RemoteObject.MyObject"/>
      </appSettings>
    </configuration>

    4、使用windows服务承载

    1、建立一个新的windows服务项目WindowsService1
    2、打开Service1代码视图,找到OnStart部分,加入代码

            System.Runtime.Remoting.RemotingConfiguration.Configure(AppDomain.CurrentDomain.BaseDirectory + "RemoteServer1.exe.config");

    config和控制台方式的config是一样的,我们让这个windows服务做的仅仅是从config文件读出配置信息进行配置通道。别忘记添加配置文件。(可将RemoteServer项目的配置文件拷进
    3、切换到设计视图,右键-添加安装程序
    4、切换到新生成的ProjectInstaller.cs设计视图,找到serviceProcessInstaller1对Account属性设置为LocalSystem,对serviceInstaller1的ServiceName属性设置为WindowsService1(服务的名字),StartType属性设置为Automatic(系统启动的时候自动启动服务)
    5、还要添加RemoteObject的引用


    五、建立安装项目

    1、建立一个新的安装项目RemoteServerSetup
    2、右键-添加-项目输出-主输出-选择WindowsService1-确定
    3、右键-视图-自定义操作-自定义操作上右键-添加自定义操作-打开应用程序文件夹-选择刚才那个主输出-确定
    4、重新生成这个安装项目-右键-安装

  • 相关阅读:
    Docker
    docker
    Go
    Docker4Windows -- 从外部(非本机host)访问 由docker container运行的程序
    Unable to resolve target 'android-9'
    win7中VS2010中安装CSS3.0问题解决方法
    修改vs2005,vs2008,vs2010调试默认浏览器
    Android SDK Content loader has encountered a problem” “parseSdkContent Failed ”
    解决parseSdkContent failed java.lang.NullPointerException错误
    50个Android开发人员必备UI效果源码[转载]
  • 原文地址:https://www.cnblogs.com/yidianfeng/p/1357892.html
Copyright © 2011-2022 走看看