【问题标题】:Add OperationContracts to a ServiceContract dynamically动态地将 OperationContracts 添加到 ServiceContract
【发布时间】:2016-01-11 14:45:17
【问题描述】:

我正在开发一个 WCF 应用程序,我需要创建“路由”,以便 OperationContracts 基于一些 dll 动态地创建。

这就是它的样子

[ServiceContract]
public interface ImyWebService
{
    [OperationContract] //Login to web server
    [WebInvoke(BodyStyle = WebMessageBodyStyle.Bare,
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        Method = "POST",
        UriTemplate = @"/login")]
    LoginResponse MyLogin(LoginRequest request);
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple, MaxItemsInObjectGraph = int.MaxValue)]
public class WebService : ImyWebService
{
    public LoginResponse MyLogin(LoginRequest request)
    {

    }
}

我们可以看到 MyLogin 是统计定义的,但我想在运行时添加其他 OperationContract。

这可能吗?我找到的所有解决方案都不适合我的使用。

【问题讨论】:

  • 您这样做是为了达到什么目的? (通过回答这个问题,您可能会了解什么是正确的解决方案)WCF 客户端如何能够连接到这种“动态”服务合同?
  • 通过查询服务器上存在哪些插件,我们应该会发现它更像一个 RESTful api
  • 你不应该通过自定义类型抽象插件而不是方法(OperationContract)吗?所以你有常用的方法,比如IResult DoOperation(IPluginSpecific)
  • 每个插件都有一个ServiceContract接口和它的实现,最完美的做法是将特定dll中包含的所有OperationContract添加到我的WebService中。
  • 感谢@sll 的帮助,我找到了解决方案,我已经回答了我的问题

标签: c# wcf


【解决方案1】:

我找到了解决问题的方法。

    [OperationContract]
    [WebInvoke(BodyStyle = WebMessageBodyStyle.Bare,
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        Method = "POST",
        UriTemplate = @"*")]
    GenericResponse Post(GenericRequest request);


    [OperationContract]
    [WebGet(BodyStyle = WebMessageBodyStyle.Bare,
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = @"*")]
    GenericResponse Get();

朋友们,这才是关键! UriTemplate = @"*" 星号“*”UriTemplate 将所有请求重定向到指定的方法。 在这之后我只需要创建一个路由系统。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 2021-02-12
    • 1970-01-01
    • 2012-08-09
    • 2011-02-27
    • 2014-12-20
    相关资源
    最近更新 更多