服务通信,即IIS wcf服务和Windows服务中托管的wcf

时间:2014-05-13 05:17:59

标签: windows wcf

我正在实现服务通信服务,即IIS wcf服务和Windows服务中托管的wcf

  1. Windows服务中托管的一个WCF服务
  2. 在Windows应用程序中使用上述服务,即在其后面有一个访问数据块的Exe,例如AccessDB1
  3. 然后在IIS中托管一个wcf服务,该服务将拥有自己的访问权限,例如 AccessDB2
  4. 所以最后有2个Accessdbs,从exe上点击一些按钮,将一些数据从DB2传输到DB。
  5. 然后反过来将数据从AccessDB1传输到AccessDB2。
  6. 如何以这种方式做到这一点。我试过这段代码

    • 创建Iservice1接口并声明两个方法 [服务合约] 公共接口IService1 {

      [OperationContract]
      string InsertUserDetails(UserDetails userInfo);
      
      [OperationContract]
      DataSet SelectUserDetails();
      

      }

    • 在Service1.svc类

      中实现此方法

      公共类Service1:IService1 {    // string connetionString = null;     OleDbConnection cnn;     string connetionString = @“Provider = Microsoft.ACE.OLEDB.12.0; Data Source = D:\ Anand \ Database \ UserDB.accdb”;

      public DataSet SelectUserDetails()
      {
      
          OleDbConnection cnn;
      
          cnn = new OleDbConnection(connetionString);
          cnn.Open();
          OleDbCommand cmd = new OleDbCommand("select UserID,UserName,Country,Email from 
          UserRecord order by UserId ", cnn);
          OleDbDataAdapter da = new OleDbDataAdapter(cmd);
          DataSet ds = new DataSet();
          da.Fill(ds);
          cmd.ExecuteNonQuery();
          cnn.Close();
          return ds;
      }
      
      
      
      
      public string InsertUserDetails(UserDetails userInfo)
      {
      
          string Message;
      
          OleDbConnection cnn;         
          cnn = new OleDbConnection(connetionString);
          cnn.Open();
      
          string str = "insert into UserRecord values('"+userInfo.UserID+"','" + 
          userInfo.UserName + "','" + userInfo.Password + "','" + userInfo.Country + 
          "','" + userInfo.Email + "') ";
          OleDbCommand cmd = new OleDbCommand(str, cnn);
      
          try
          {
              int result = cmd.ExecuteNonQuery();
      
              if (result == 1)
              {
                  Message = userInfo.UserName + " Details inserted successfully";
              }
              else
              {
                  Message = userInfo.UserName + " Details not inserted successfully";
              }
              cnn.Close();
              return Message;
          }
          catch (Exception)
          {
      
              throw;
          }
      
      }
      
    • 之后创建Windows服务并将服务引用提供给Windows服务并创建服务引用对象

      public partial class UserRegistration : Form
      

      {     ServiceReference1.Service1Client service = new ServiceReference1.Service1Client();

      public UserRegistration()
      {
          InitializeComponent();
          showdata(); 
      }
      
      private void showdata()  // to show the data in the DataGridView
      {
          DataSet ds = new DataSet();
          ds = service.SelectUserDetails();   
          dataGridView1.DataSource = ds.Tables[0];
          dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
      }
      
      private void button1_Click(object sender, EventArgs e)
      {
          ServiceReference1.UserDetails objuserdetail = new ServiceReference1.UserDetails(); // add type reference
          objuserdetail.UserID = Int32.Parse(txtuserId.Text);
          objuserdetail.UserName = txtUserName.Text;
          objuserdetail.Password = txtPass.Text;
          objuserdetail.Country = txtCountry.Text;
          objuserdetail.Email = txtEmailId.Text;
          service.InsertUserDetails(objuserdetail);
          showdata();
          Empty();
      }
      
      public void Empty()
      {
          txtCountry.Text = "";
          txtEmailId.Text = "";
          txtPass.Text = "";
          txtuserId.Text = "";
          txtUserName.Text = "";
      
      }
      

      }

0 个答案:

没有答案