2

我最近在 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();
        }
    }
}
4

4 回答 4

11

这甚至可以正确编译吗?接口不能声明其中方法的可见性。它们是隐式公开的。所以这;

[OperationContract]
public string GetDouble(int value);

应该是这个;

[OperationContract]
string GetDouble(int value);
于 2013-01-30T03:16:09.297 回答
1

我解决了这个问题。显然我忘了编译更改。感谢大家的帮助!

于 2013-01-30T18:46:28.383 回答
0

所以从 Operation 合约中移除访问修饰符,如下所示,

string GetData(int value)
{
    return string.Format("You entered: {0}", value);
}
string GetDouble(int value) 
{
    return (value * 2).ToString();
}
于 2013-01-30T12:38:26.187 回答
0

你得到了答案,因为你在正确的方向上做了(构建和编译)。如果您没有以正确的方式打开解决方案,它也可能发生在某个地方。

于 2017-07-18T10:35:57.190 回答