WCF DataContract仅在从客户端exe引用的服务中可见,而不是DLL

时间:2012-11-09 13:32:59

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

我有一个简单的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引用服务。 我在其他地方读过,如果它在一个服务合同中被积极使用,你只能看到数据合同,但确实如此。

1 个答案:

答案 0 :(得分:1)

行。我已经解决了这个问题:

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

tbh我不知道为什么这有效,但确实...... [盖尔耸耸肩]