我已经在 VS2008 中创建了默认的 WCF 服务。它被称为“服务1”
public class Service1 : IService1
{
public string GetData( int value )
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract( CompositeType composite )
{
if ( composite.BoolValue )
{
composite.StringValue += "Suffix";
}
return composite;
}
}
它工作正常,接口是 IService1:
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData( int value );
[OperationContract]
CompositeType GetDataUsingDataContract( CompositeType composite );
// TODO: Add your service operations here
}
这都是默认的; Visual Studio 2008 创建了这一切。
然后我创建了一个简单的 Winforms 应用程序来“测试”它。我将服务参考添加到上述服务中,一切正常。我可以实例化并调用 myservice1.GetData(100);我得到了结果。
但我被告知该服务必须由 Winforms .NET 2.0 应用程序通过 Web 服务使用,因此我继续添加对从头创建的新 Winforms .NET 2.0 应用程序的引用(只有一个名为 form1 的 winform)。这次在添加“web引用”时,添加了典型的属于webservices的“localhost”;向导看到了 WCF 服务(在后台运行)并添加了它。
当我尝试使用它时,我发现 GetData(int) 方法现在是 GetData(int, bool)。
这是代码
private void button1_Click( object sender, EventArgs e )
{
localhost.Service1 s1 = new WindowsFormsApplication2.localhost.Service1();
Console.WriteLine(s1.GetData(100, false));
}
注意到 GetData 调用中的 false 了吗?
我不知道该参数是什么或从哪里来,它被称为“bool valueSpecified”。
有谁知道这是从哪里来的?我还应该做些什么来使用 WCF 服务作为 .NET 2.0 中的 Web 服务? (winforms)。