且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从窗体传递值到Windows服务?

更新时间:2023-12-06 08:34:10

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



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 &GT; 跨度>


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>