WCF 服务中的方法未显示在客户端应用中
本文关键字:客户端 显示 应用 方法 服务 WCF | 更新日期: 2023-09-27 17:56:48
我在 C# 编码方面的最新尝试是学习 WCF 服务。我正在尝试制作一个非常简单的服务。它有一个不是默认的方法,它只是返回一个用户输入的整数乘以 2 的字符串。我已将服务上传到 aspspider.com,并创建了一个 winforms 客户端应用程序。但是,在查看服务类时,它仅显示 GetData 方法。老实说,我对此感到困惑,并且一辈子都无法理解为什么我的方法不会出现。我已经从命令行使用了 svcutil.exe 命令,但仍然什么都没有。如果有人能帮助我,我将不胜感激!这是我的代码:
服务代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Web.Services;
namespace WcfService1
{
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public string GetDouble(int value) //Method that isn't showing up
{
return (value * 2).ToString();
}
}
}
服务接口:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfService1
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
public string GetDouble(int value); //Method that isn't showing up
// TODO: Add your service operations here
}
}
Service1Client(请注意,这很混乱。仍在尝试弄清楚如何在此处格式化代码):
namespace WcfService1
{
using System.Runtime.Serialization;
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="CompositeType", Namespace="http://schemas.datacontract.org/2004/07/WcfService1")]
public partial class CompositeType : object, System.Runtime.Serialization.IExtensibleDataObject
{
private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
private bool BoolValueField;
private string StringValueField;
public System.Runtime.Serialization.ExtensionDataObject ExtensionData
{
get
{
return this.extensionDataField;
}
set
{
this.extensionDataField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public bool BoolValue
{
get
{
return this.BoolValueField;
}
set
{
this.BoolValueField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public string StringValue
{
get
{
return this.StringValueField;
}
set
{
this.StringValueField = value;
}
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="IService1")]
public interface IService1
{
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService1/GetData", ReplyAction="http://tempuri.org/IService1/GetDataResponse")]
string GetData(int value);
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService1/GetDataUsingDataContract", ReplyAction="http://tempuri.org/IService1/GetDataUsingDataContractResponse")]
WcfService1.CompositeType GetDataUsingDataContract(WcfService1.CompositeType composite);
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public interface IService1Channel : IService1, System.ServiceModel.IClientChannel
{
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public partial class Service1Client : System.ServiceModel.ClientBase<IService1>, IService1
{
public Service1Client()
{
}
public Service1Client(string endpointConfigurationName) :
base(endpointConfigurationName)
{
}
public Service1Client(string endpointConfigurationName, string remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}
public Service1Client(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}
public Service1Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress)
{
}
public string GetData(int value)
{
return base.Channel.GetData(value);
}
public WcfService1.CompositeType GetDataUsingDataContract(WcfService1.CompositeType composite)
{
return base.Channel.GetDataUsingDataContract(composite);
}
}
客户:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WebServiceTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Service1Client client = new Service1Client();
client.Open();
label1.Text = client.GetData(5);
client.Close();
}
}
}
这甚至编译正确吗?接口无法声明其中方法的可见性。它们是隐含的公开的。所以,这个;
[OperationContract]
public string GetDouble(int value);
应该是这个;
[OperationContract]
string GetDouble(int value);
我解决了这个问题。显然我忘了编译更改。感谢大家的帮助!
因此,从操作合约中删除访问修饰符,如下所示,
string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
string GetDouble(int value)
{
return (value * 2).ToString();
}
你得到了答案,因为你在正确的方向上做(构建和编译)。 如果您没有以正确的方式打开解决方案,它也可能发生在某个地方。