我在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();
}
}
}
答案 0 :(得分:11)
这是否正确编译?接口无法声明其中方法的可见性。它们是含蓄公开的。所以,这个;
[OperationContract]
public string GetDouble(int value);
应该是这个;
[OperationContract]
string GetDouble(int value);
答案 1 :(得分:1)
我解决了这个问题。显然我忘了编译更改。谢谢大家的帮助!
答案 2 :(得分:0)
因此,从操作合同中删除访问修饰符,如下所示,
string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
string GetDouble(int value)
{
return (value * 2).ToString();
}
答案 3 :(得分:0)
你得到了答案,因为你正确地做(构建和编译)。如果你没有以适当的方式打开解决方案,它也可能发生在某个地方。