1

I am working on a WCF application and I need to create "routes" so OperationContracts dynamically based on some dlls.

Here's what it looks like

[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)
    {

    }
}

We can see that MyLogin is statistically defined, but I'd like to add other OperationContract during the runtime.

Is that possible? All the solutions I've found yet don't fit well with my use.

M. Christopher
  • 305
  • 3
  • 14
  • What are you trying to achieve by doing that? (by answering this you might shed some light on what proper solution is) How WCF clients would be able connect to such "dynamic" service contract? – sll Jan 11 '16 at 14:53
  • By querying which plugins are present on the server, we should see that more like a RESTful api – M. Christopher Jan 11 '16 at 14:57
  • Shouldn't you abstract plugin by custom type not a method (OperationContract)? So you have common methods like `IResult DoOperation(IPluginSpecific)` – sll Jan 11 '16 at 14:58
  • Each plugin has a ServiceContract interface and it's implementation, the perfect thing would be to add all the OperationContract contained in a specific dll into my WebService. – M. Christopher Jan 11 '16 at 15:01
  • Then you need kind of self made routing to route call depends on type to aprticular service contract probably with intermediate request type adoption – sll Jan 12 '16 at 09:15
  • I've not found a good solution yet, I mean to achieve what you've said – M. Christopher Jan 12 '16 at 16:37
  • Are all possible OperationContracts known in advance? – sll Jan 12 '16 at 17:21
  • No, they are not during the compilation, some OperationContracts would come from dlls. – M. Christopher Jan 13 '16 at 09:01
  • Like you put in a folder some DLL and WCF automatically exposes its methods? – sll Jan 13 '16 at 13:32
  • Ok let's imagine you have such dynamic API service, how would its clients would be able create proxy if contract might change or do you want client proxies so automatically regenerated every time? I'm missing some point because we can solve mostly any technical problem but it might be wrong direction if not taking into account main aims of entire solution/problem so would be great if you describe entire final architecture – sll Jan 14 '16 at 14:42
  • And just generation on server side - a C# example http://stackoverflow.com/a/9619685/485076, http://www.codeproject.com/Articles/328552/Calling-a-WCF-service-from-a-client-without-having – sll Jan 14 '16 at 15:03
  • 1
    Thank you @sll for the help, I've found my solution, I've answered to my question – M. Christopher Jan 15 '16 at 09:56

1 Answers1

1

I've found a solution to my problem.

    [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();

This, my friends, is the key! UriTemplate = @"*" The star "*" UriTemplate redirect all the requests to the specified method. Behind that I only had to create a routing system.

M. Christopher
  • 305
  • 3
  • 14