WCF 服务中添加的新功能未在客户端上显示

New function added in WCF service is not showing on Client

我在 Asp.net MVC 上有一个简单的 Web 应用程序。我创建了一个 WFC 服务,这样我就可以向我的 Windows 8.1 应用程序获取数据,但是我的新功能没有显示在我的客户端。

这是我的 WFC 代码:

[DataContract]
    public class Service1 : IService1
    {
        ApplicationDbContext _db=new ApplicationDbContext();

        public string GetData(int value)
        {


            return string.Format("You entered: {0}", value);
        }

        public WtFImages.Models.Images GetDataL(int value)
        {
            var User = _db.Image.Local.FirstOrDefault(c => c.Id == value);

            return User;
        }
        public List<WtFImages.Models.Images> GetDataAll(int value)
        {
            var GetAllPublic = _db.Image.Local.ToList();

            return (GetAllPublic);
        }

        public IList<WtFImages.Models.Images> ZGetDataAll(int value)
        {
            var GetAllPublic = _db.Image.Local.ToList();

            return (GetAllPublic);
        }
        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}

我的客户端只显示默认功能。

服务代码

namespace ImageFechingWfc
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}

您希望作为服务方法公开的每个方法都应该存在于您的 IService1 接口中并且应该用 [OperationContract] 修饰,然后您应该在服务 class 中实现该方法.

  • 打开 IService1.cs 并把你的方法的签名放在 IService1 接口然后用 [OperationContract] 装饰你的新方法,然后把实现放在 Service1 中并重建项目,然后添加您的服务参考并使用它。
  • 此外,您不需要 [DataContract] 在您的服务实施之上。

例如,如果你想在你的服务中有一个 int Add(int x, int y) 方法,把它放在你的 IService1 接口中:

[OperationContract]
int Add(int x, int y);

然后将其放入您的 Service1 class:

public int Add(int x, int y)
{
    return x+y;
}

要了解有关 WCF 服务的更多信息,您可以阅读这篇文章Getting Started Tutorials

首先确保您的方法是否在 wsdl 中列出,否则以管理员帐户打开 visual studio 并从 Visual C# 项目文件而不是 Visual Studio 项目用户选项文件 (.user) 打开。