为了继续学习WCF,我正在尝试编写一个小程序,只需单击一个按钮,就可以从texbox1获取工作,将其传递给ServiceContract并恢复其长度。
这就是我得到的结果。
Form1.cs:
...
wcfLib.Service myService = new wcfLib.Service();
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = Convert.ToString( myService.go(textBox1.Text) );
}
...和wcf文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace wcfLib
{
[ServiceContract]
public interface IfaceService
{
[OperationContract]
int wordLen(string word);
}
public class StockService : IfaceService
{
public int wordLen(string word)
{
return word.Length;
}
}
public class Service
{
public int go( string wordin )
{
ServiceHost serviceHost = new ServiceHost(typeof(StockService), new Uri("http://localhost:8000/wcfLib"));
serviceHost.AddServiceEndpoint(typeof(IfaceService), new BasicHttpBinding(), "");
serviceHost.Open();
int ret = **///// HOW SHOULD I PASS wordin TO StockService to get word.Length in return?**
serviceHost.Close();
return ret;
}
}
}我现在不明白的是,我如何将上面的wordin变量传递给ServiceContract?
发布于 2011-03-08 02:57:22
您需要在表单中创建客户端并直接调用wordLen() ...只有从IfaceService继承的类才能被调用为WCF服务。所以:
// You'll have to create references to your WCF service in the project itself...
// Right-click your form project and pick 'Add Service Reference', picking
// 'Discover', which should pick up the service from the service project... else
// enter http://localhost:8000/wcfLib and hit 'Go'.
// You'll have to enter a namespace, e.g. 'MyWcfService'... that namespace is
// used to refer to the generated client, as follows:
MyWcfService.wcfLibClient client = new MyWcfService.wcfLibClient();
private void button1_Click(object sender, EventArgs e) {
// You really shouldn't have the client as a member-level variable...
textBox2.Text = Convert.ToString(client.wordLen(textBox1.Text));
}如果您的Service类旨在托管WCF服务,则它需要是自己的可执行文件并且正在运行...将go()中的代码放在Main()中
或在IIS中承载您的WCF服务...简单多了!
编辑
IIS = Internet信息服务...主要是在web上托管WCF服务。
若要在IIS中承载,请创建一个新项目"WCF服务应用程序“。您将获得一个web.config、一个默认接口和一个.svc文件。将这些重命名,或向项目中添加新项WCF服务。如果您使用该方法,则必须阅读一些有关部署到IIS的知识,但对于在Visual Studio中进行调试,这种方法工作得很好。
要拆分成两个应用程序,只需使窗体成为其自己的项目...服务引用是通过应用程序的配置文件设置的;您只需将其指向机器或网站的地址,例如http://myintranet.mycompany.com:8000/wcflib或http://myserver:8000/wcflib。
谢谢你的投票!
发布于 2011-03-08 02:55:21
你的东西肯定是前后颠倒的。您不希望在Go方法中创建ServiceHost,或者至少永远不会在客户端调用的任何方法中创建它,因为如果服务尚未创建,客户端如何调用它?
WCF中的服务已启动,然后您可以从远程客户端调用其方法。例如,这是服务的Main():
ServiceHost serviceHost = new ServiceHost(typeof(StockService), new Uri("http://localhost:8000/wcfLib"));
serviceHost.AddServiceEndpoint(typeof(IfaceService), new BasicHttpBinding(), "");
serviceHost.Open();
Console.WriteLine("Press return to terminate the service");
Console.ReadLine();
serviceHost.Close();然后,对于您的客户端,您可以在Visual Studio中使用"Add service Reference“(在解决方案资源管理器中的项目上单击鼠标右键以找到此菜单选项),并输入服务的地址。Visual Studio将为您的服务创建一个代理,这是您将在客户端实例化和使用的代理。例如:
MyServiceClient client = new MyServiceClient();
textBox2.Text = Convert.ToString( client.wordLen(textBox1.Text) );https://stackoverflow.com/questions/5223058
复制相似问题