来自WCF服务的方法未显示在客户端应用程序中

Zac*_*ews 2 .net c# wcf

我在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();
        }
     }
  }
Run Code Online (Sandbox Code Playgroud)

服务接口:

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
    }   
}
Run Code Online (Sandbox Code Playgroud)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

客户:

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

RJ *_*han 11

这甚至正确编译?接口无法声明其中方法的可见性.它们是含蓄公开的.所以这;

[OperationContract]
public string GetDouble(int value);
Run Code Online (Sandbox Code Playgroud)

应该这样;

[OperationContract]
string GetDouble(int value);
Run Code Online (Sandbox Code Playgroud)