如何从窗体传递值到Windows服务? [英] How to pass values to Windows Service from Form?

查看:66
本文介绍了如何从窗体传递值到Windows服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



我对C#很陌生。我制作了一个Windows窗体,可以在计算机上进行一些网络更改。我想在域env中使用它。并希望域用户能够在没有任何'runas'的情况下使用它。这就是问题。域用户没有权限(例如)IP地址更改,但他们需要。



我的想法是:



- 创建一个可以进行IP更改的Windows服务

- 该服务可以作为本地系统运行

- 我想传递一些从我的表格到服务的IP地址(ip,mask,gw,dns)





我的表单可以完美地运行管理员权限。我应该怎么做才能以上述方式使用?



也许我应该将我的所有代码复制到'protected override void OnStart(string [] args) 'service.cs的一部分?但是怎么样?



如何将一些args传回表格以通知用户已经进行了更改?



请帮我做什么。一步一步的帮助真的很有帮助。



非常感谢



干杯

解决方案

我正按照您的要求编写示例。



1.在添加两个数字之前,我们必须创建在webservice中添加两个数字的方法。



在Service1.asmx.cs中写下面的代码(我的网络服务名称是WebService1)。



 [WebMethod] 

公共 int Sum( int a, int b)
{
int a,b;
int sum = a + b;
return sum;
}





上面的方法返回总和,所以我们得到Form1中的总和值。



之后运行WebService1并从浏览器复制服务URL。创建表单应用程序,然后按照以下步骤操作。



1.Solution Explorer>右键单击>添加服务参考>粘贴服务网址>点击确定。

2.然后在按钮点击事件中写下面的代码。



  private   void  ButtonLogin_Click( object  sender,EventArgs e) 
{
int a,b;
a = Convert.ToInt(textBox1.Text);
b = Convert.ToInt(textBox2.Text);
myService.Service1SoapClient client = new Service1SoapClient(); // 我将WebService重命名为myService1,同时添加引用。
client.ValidateQueryCompleted + = new EventHandler< validatequerycompletedeventargs>(client_ValidateQueryCompleted);
client.ValidateQueryAsync(a,b);
}

void client_ValidateQueryCompleted( object sender,ValidateQueryCompletedEventArgs e)
{
MessageBox.Show(sum.ToString());
}

< / validatequerycompletedeventargs >


Dear All,

I'm quite new to C#. I made a Windows Form that makes some network changes on a computer. I would like to use it in a Domain env. and would like the domain users to be able to use it without any 'runas'. This is the problem. Domain users don't have privileges to make (for example) IP address changes, but they need to.

My idea is:

- create a Windows Service that makes the IP changes
- the service can run as local system
- I'd like to pass some IP addresses from my Form to Service (ip, mask, gw, dns)


My form works perfectly running with admin privileges. What should I do to be able to use in the mentioned way?

Maybe I should copy all my code to the 'protected override void OnStart(string[] args)' part of the service.cs? But how?

And how can I pass back some args to the Form to inform the user that the changes have been made?

Please help me what to do. A step by step help would be really helpful.

Thanks a lot

Cheers

解决方案

I'm writing an example as you requested above.

1. Before adding two numbers we have to create method for adding two numbers in webservice.

Write the following code in Service1.asmx.cs (My webservice name is WebService1).

[WebMethod]

Public int Sum(int a,int b)
       {
           int a,b;
           int sum=a+b;
           return sum;
       }



the above method returning sum, so we get the sum value in Form1.

After that run the WebService1 and copy the service URL from browser. Create the form application then follow the below steps.

1.Solution Explorer>Right Click> Add Service Refrence> Paste the service url> click Ok.
2.Then write the below code in the button click event.

private void ButtonLogin_Click(object sender, EventArgs e)
        {
           int a,b;
           a=Convert.ToInt(textBox1.Text);
           b=Convert.ToInt(textBox2.Text);
           myService.Service1SoapClient client = new Service1SoapClient(); //I renamed the WebService to myService1, while adding in the reference.
          client.ValidateQueryCompleted += new EventHandler<validatequerycompletedeventargs>(client_ValidateQueryCompleted); 
          client.ValidateQueryAsync(a,b);
        }

void client_ValidateQueryCompleted(object sender, ValidateQueryCompletedEventArgs e)
        {
           MessageBox.Show(sum.ToString());
        }

</validatequerycompletedeventargs>


这篇关于如何从窗体传递值到Windows服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆