[英]Wcf Service Library interface type not being exposed
I've look over many examples (including Passing Interface in a WCF Service? ) with regards the problem I have, but until now have not found a solution. 关于我所遇到的问题,我查看了许多示例(包括在WCF服务中传递接口? ),但是直到现在还没有找到解决方案。 I have a Wcf Service Library (.NET 4.0) with the following
我有一个Wcf服务库(.NET 4.0),具有以下内容
public class Service : IService
{
public bool Ping(IChannelManager channelManager)
{
return channelManager.Ping();
}
}
[ServiceContract]
public interface IService
{
[OperationContract]
bool Ping(IChannelManager channelManager);
}
[ServiceContract]
[ServiceKnownType(typeof(TestChannelName))]
public interface IChannelManager
{
[OperationContract]
bool Ping();
}
[DataContract]
public class TestChannelName : IChannelManager
{
public bool Ping()
{
//perform a ping
return true;
}
}
My project compiles fine. 我的项目编译正常。 But when I try to add it as a Service Reference from a console app, I can add it fine and try to access the method like below;
但是,当我尝试从控制台应用程序将其添加为服务引用时,可以很好地添加它并尝试访问如下方法;
using (Test.ServiceClient oClient = new Test.ServiceClient())
{
oClient.Ping();
}
But the problem I have is the Ping() method on the Wcf service, the interface type is coming up as object channelmanager ? 但是我遇到的问题是Wcf服务上的Ping()方法,接口类型是否作为对象channelmanager出现? Like
喜欢
I tried adding [ServiceKnownType(typeof(TestChannelName ))] to the both interfaces but no luck. 我尝试将[ServiceKnownType(typeof(TestChannelName))]添加到两个接口,但是没有运气。
Where am I going wrong ? 我要去哪里错了?
My answer will be split in two - a technical overview of why this happens - and philosophizing about why this happens. 我的回答将分为两部分-关于发生这种情况的技术概述-并对发生这种情况的原因进行哲学思考。
first technically If you have a look at: 首先从技术上讲,如果您看一下:
http://localhost:8733/Design_Time_Addresses/YourProject/Service1/?singlewsdl
the wsdl file that is created from your example code, you will find the following: 从示例代码创建的wsdl文件,您将找到以下内容:
<xs:element name="Ping">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="channelManager" nillable="true" type="xs:anyType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Either over mex or as you see above in the wsdl. 要么通过mex,要么如您在wsdl中所见。 you get
xs:anytype
(in practical terms the c# object). 您会得到
xs:anytype
(实际上是c#对象)。 This behavior although frustrating is to be expected, Mex and the WsdlExtractor
only understand concrete types - DataMembers denoted by a [DataContract]
in the service arguments, well that or simple types. 尽管可以预料到这种行为会令人沮丧,但Mex和
WsdlExtractor
仅了解具体类型-服务参数中由[DataContract]
表示的DataMembers,即简单类型。 Any other will be treated gracefully as 'object' rather then just failing to compile. 其他任何对象都将被视为“对象”,而不是仅仅编译失败。
The philosophy part: 哲学部分:
Effectively you are trying to pass a method [ServiceContract]
as a parameter. 实际上,您正在尝试将方法
[ServiceContract]
作为参数传递。 Services (with their logic built to IL in some DLL) do not serialize and pass over networks - not because it can't be done - because it is not what they are for. 服务(其逻辑在某些DLL中内置到IL中)不会序列化并通过网络传递-不是因为无法完成-因为这不是它们的用途。
In your example IChannelManager
is a [ServiceContract]
, and you even try to export the Ping()
as an [OperationContract]
. 在您的示例中,
IChannelManager
是[ServiceContract]
,甚至尝试将Ping()
导出为[OperationContract]
。 the [ServiceKnownType]
attribute does nothing to remedy that. [ServiceKnownType]
属性无济于事。
When is the [ServiceKnownType]
useful then? [ServiceKnownType]
有用? Well that is well documented but the gist of it is - if your DataMember in runtime is to hold some concrete class - that the compiler cannot infer at compile time (eg interfaces, abstract classes or base classes) well then a deserializer will not be able to guess what concrete types it should know when trying to deserialize. 好吧,这是有据可查的,但是要点是-如果您的DataMember在运行时要保留一些具体的类-编译器无法在编译时进行推断(例如,接口,抽象类或基类),那么反序列化器将无法执行猜测尝试反序列化时应该知道哪些具体类型。
You might then say to me - well that sounds fair and well but I need to pass my logic (Service) as a parameter. 然后,您可能会对我说-听起来不错,但我需要将逻辑(服务)作为参数传递。 And to that I'd say - what you need is to redesign your solution.
我要说的是-您需要重新设计解决方案。 Maybe write and host more services and use the service equivalent of the Strategy or Delegation patterns.
也许编写并托管更多服务,并使用与策略或委派模式等效的服务。
I hope some of this helps. 我希望其中一些帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.