wcf服务应用程序中缺少程序集引用

本文关键字:程序集 引用 服务 应用程序 wcf | 更新日期: 2023-09-27 18:17:16

我正在使用WCF服务创建一个Web应用程序。

接下来的步骤是,

1)创建一个新的WCF服务应用程序。

2)新增WCF服务

3)创建一个类库。

4)添加了一个新的类,我有这些代码下面提到

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ServiceModel;

namespace ClassLibrary1
{
    public interface IUSerDetails
    {
        [OperationContract]
        string InsertUserDetails(UserDetails user);
    }
    [DataContract]
    public class UserDetails
    {
        string username = string.Empty;
        string password = string.Empty;
        string country = string.Empty;
        string email = string.Empty;
        [DataMember]
        public string UserName
        {
            get { return username; }
            set { username = value; }
        }
        [DataMember]
        public string Password
        {
            get { return password; }
            set { password = value; }
        }
        [DataMember]
        public string Country
        {
            get { return country; }
            set { country = value; }
        }
        [DataMember]
        public string Email
        {
            get { return email; }
            set { email = value; }
        }
    }
}

现在在Ln 13,当我做鼠标悬停在[OperationContract]。它给出了一个错误。

是否缺少using指令或程序集引用?

所以我想我错过了名称空间

我补充道使用system.ServiceModel;,但是在两行中再次显示相同的错误。你错过了什么?

wcf服务应用程序中缺少程序集引用

我使用Mac版的Visual Studio在。net Framework 4.7中创建一个新的控制台项目。

奇怪的是,它对[ServiceContract]属性来自System.ServiceModel非常满意,但却抱怨[OperationContract]属性:

Program.cs(10,10): Error CS0246: The type or namespace name 'OperationContactAttribute' could not be found (are you missing a using directive or an assembly reference?) (CS0246)
Program.cs(10,10): Error CS0246: The type or namespace name 'OperationContact' could not be found (are you missing a using directive or an assembly reference?) (CS0246)

检查OperationContractAttribute类文档后,我发现System.ServiceModel.Primitives程序集是必需的。由于某些原因,System.ServiceModel.Primitives程序集不能通过项目参考在Packages和. net程序集选项卡中使用。

我从项目参考中删除了所有程序集,并使用NuGet修复了上述错误,即:

Install-Package System.ServiceModel.Primitives

除了将System.ServiceModel.Primitives添加到项目参考之外,还重新添加了:

  • mscorlib
  • 系统
  • 系统。IdentityModel
  • System.Runetime.Serialization
  • 系统。ServiceModel
  • System.Xml

现在我可以重新添加我需要继续的其他程序集。

按照链接https://social.msdn.microsoft.com/Forums/vstudio/en-US/96ffb6d7-8737-4c4d-8512-58967d0b69cd/wcf-on-godaddy-compiler-doesnt-recognize-operationcontract?forum=wcf,您需要做两个更改。首先,将以下代码行添加到web的httpmodules部分。配置文件:

<httpModules>

  <add name="ServiceModel" type="System.ServiceModel.Activation.HttpModule, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

</httpModules>

其次,将以下行添加到web的assemblies部分。配置文件:

  <assemblies>

    <add assembly="System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

    <add assembly="System.ServiceModel.Web, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

  </assemblies>

在"interface IUSerDetails"行上方写入[ServiceContract]