简体   繁体   English

接口参数/返回WCF服务参考中的类型

[英]Interface Parameter / Return Type in a WCF Service reference

I have a WCF service and an MVC client that has a service reference to the aformentioned. 我有一个WCF服务和一个MVC客户端,它具有对前述的服务引用。 I am trying to use marker interfaces as return types and parameters but I'm having trouble generating the service reference to reflect this. 我试图使用标记接口作为返回类型和参数,但我无法生成服务引用来反映这一点。

I'm currently getting a return type / parameter type of "object" in my service reference so my question is how would I go about resolving this as the actual function in my service takes and returns an interface of IWorkRequest and IWorkResponse respectively. 我目前在我的服务引用中得到一个返回类型/参数类型的“对象”,所以我的问题是我将如何解决这个问题,因为我的服务中的实际函数需要分别返回IWorkRequestIWorkResponse的接口。

My service contract looks like the following: 我的服务合同如下:

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    IWorkResponse DoWork(IWorkRequest request);
}

My service-side interfaces look like the following: 我的服务端接口如下所示:

[ServiceKnownType(typeof(WorkRequestA))]
[ServiceKnownType(typeof(WorkRequestB))]
[ServiceContract()]
public interface IWorkRequest 
{

}

I have a similar one for IWorkResponse following the same pattern. 我有一个类似的IWorkResponse遵循相同的模式。 When I build the project and update the service reference to it then the service reference looks like: 当我构建项目并更新服务引用时,服务引用如下所示:

public object DoWork(object request) {
    return base.Channel.DoWork(request);
}

I also have no reference to the interfaces or any of the classes that implement the interfaces available clientside through the service reference. 我也没有参考通过服务引用实现可用客户端接口的接口或任何类。

Ideally I would want the service reference to be the following: 理想情况下,我希望服务引用如下:

public IWorkResponse DoWork(IWorkRequest request) {
    return base.Channel.DoWork(request);
}

I have viewed a number of answers regarding this but none seem to resolve my issue so any insight would be useful. 我已经看过很多关于此问题的答案,但似乎没有解决我的问题所以任何见解都会有用。

Thanks in advance for any help provided. 提前感谢您提供的任何帮助。

I'd like to add a comment rather than an answer but I don't have enough reputation, so here goes. 我想添加一个评论而不是一个答案,但我没有足够的声誉,所以这里有。

You cannot return an interface through a service operation, or pass an interface as an argument to the operation. 您不能通过服务操作返回接口,也不能将接口作为参数传递给操作。 The service and client need to be completely decoupled and not use shared interfaces. 服务和客户端需要完全解耦,而不是使用共享接口。 Remember that WCF clients are not always .Net applications. 请记住,WCF客户端并不总是.Net应用程序。

If IWorkerResponse only has properties (no methods), then there is no need to use an interface at all. 如果IWorkerResponse只有属性(没有方法),则根本不需要使用接口。

Otherwise use the methods of IWorkResponse as operation contract 否则使用IWorkResponse的方法作为操作合同

 [ServiceContract] public interface IWorkResponseService { [OperationContract] SomeResponseObject DoWork(SomeRequestObject request); } 

It may still be possible to do what Maarten suggested in his comment. 可能仍然有可能做马腾在评论中提出的建议。

Also see the answer to this How do I return a reference to an interface from a WCF contract? 另请参阅此答案如何从WCF合同返回对接口的引用?

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

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