【问题标题】:WCF DataContract only visible if service referenced from client exe, not DLLWCF DataContract 仅在从客户端 exe 而不是 DLL 引用的服务时可见
【发布时间】:2012-11-09 13:32:59
【问题描述】:

我有一个简单的 WCF 日志记录服务..

namespace WCFServiceLibrary
{
    [ServiceContract]
    public interface ILog
    {
        [OperationContract]
        string InsertLogItem(LogItem logItem);
    }

    [DataContract]
    public class LogItem
    {
        private string _Text = string.Empty;

        [DataMember]
        public string Text
        {
            get { return _Text; }
            set { _Text = value; }
        }
    }
}

我有一个客户端和一个 DLL,都在同一个解决方案中。

如果我从客户端引用服务,我可以看到 DataContract。 如果我从 DLL 引用服务,我看不到 DataContract。

我的意思是我将服务引用为 LogService,并在客户端中有此代码...

static LogService.LogClient logService = new LogService.LogClient();
//create log item - <<< Compile error in DLL on following line >>>
var itemTolog = new LogService.LogItem { Text = "log this"}; 
string result = logService.InsertLogItem(itemTolog);

那么它可以正常工作,但是如果DLL中相同的代码,则由于LogService.LogItem不可见而无法编译,并报告以下错误

The type or namespace name 'LogItem' does not exist in the namespace 'MyDll.LogService' (are you missing an assembly reference?)    

当从 dll 引用服务时,dataContract "LogItem" 根本不在对象资源管理器中,但如果从客户端 exe 引用,则它存在。

什么给了?不用说我想从 DLL 中引用服务。 我在其他地方读到,只有在其中一个服务合同中积极使用数据合同时,您才能看到它,但确实如此。

【问题讨论】:

  • “看不到数据合同”是什么意思?您是否在运行时遇到问题,或者在编写代码时无法声明 wcf 服务的实例?
  • @Steve 希望现在通过包含在 DLL 中引用服务时失败/不可见的代码让自己更清楚。

标签: c# .net visual-studio-2010 wcf


【解决方案1】:

好的。我已经通过以下方式解决了这个问题:

  • 右键单击 DLL 中的服务引用
  • 点击了“配置服务参考...”
  • 未选中“在引用的程序集中重用类型”。

tbh 我不知道为什么这行得通,但它确实...... [盖尔耸肩]

【讨论】:

  • 是的,这是使引用服务的项目可以看到 DataContract 类或枚举类型的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 2012-07-24
  • 1970-01-01
  • 2011-02-01
  • 2015-05-31
  • 2019-12-14
  • 1970-01-01
相关资源
最近更新 更多