简体   繁体   English

WCF DataContract仅在从客户端exe而非DLL引用服务的情况下可见

[英]WCF DataContract only visible if service referenced from client exe, not DLL

I have a simple WCF logging service.. 我有一个简单的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; }
        }
    }
}

And I have a client, and a DLL, all in the same solution. 我有一个客户端和一个DLL,都在同一解决方案中。

If I reference the service from the Client, I can see the DataContract. 如果我从客户端引用服务,则可以看到DataContract。 If I reference the service from the DLL, I can NOT see the DataContract. 如果我从DLL引用服务,则看不到DataContract。

By which I mean that it I reference the service as LogService, and have this code in the client... 我的意思是说我将服务称为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);

then it works fine, but if the same code is in the DLL, then it will not compile due to LogService.LogItem not being visible, and reports the following error 那么它可以正常工作,但是如果DLL中包含相同的代码,则由于LogService.LogItem不可见,因此它将无法编译,并报告以下错误

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

The dataContract "LogItem" is simply not in the object explorer when the service is referenced from the dll, but it is if referenced from the client exe. 当从dll引用服务时,dataContract“ LogItem”根本不在对象资源管理器中,但是,如果从客户端exe引用,则为dataContract“ LogItem”。

What gives? 是什么赋予了? Needless to say I want to reference the Service from the DLL. 不用说我想从DLL引用服务。 I've read elsewhere that you can only see the data contract if it is actively used in one of the service contracts, but it is. 我在其他地方读过,只有在其中一项服务合同中积极使用了数据合同时,您才可以看到它。

OK. 好。 I've fixed this by: 我已通过以下方式解决此问题:

  • right clicking on the service reference in the DLL 右键单击DLL中的服务引用
  • clicked 'Configure Service Reference...' 点击“配置服务参考...”
  • unchecked "Resuse types in referenced assemblies". 取消选中“在引用的程序集中重新使用类型”。

tbh I do not know why this works, but it does... [gaelic shrug] tbh我不知道为什么会这样,但是确实可以... [盖尔耸肩]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM