简体   繁体   中英

Can I extend ServiceStackHost to handle requests from a CEF browser in my application?

I have a solution which includes a thick client (implemented using CefSharp for the majority of the user interface), and the javascript application needs to execute some C# logic in the application hosting the CEF browser. I considered using WebView.RegisterJsObject() , but I can write less glue code if I can just use $.ajax() from the html pages.

I already have ServiceStack set up for the web services and the web client in this solution. I'd like to route requests from the CEF browser to a local ServiceStack host (without actually using http).

Here's some psuedo code to illustrate what I would like to do:

public partial class MainWindow : IRequestHandler {
  WebView _webView;
  CefSharpServiceStackHost _serviceHost;

  public MainWindow() {
    // initialize CefSharp...
    _webView.RequestHandler = this;
    // initialize ServiceStackHost...
  }

  // other IRequestHandler methods...

  // method this intercepts ajax calls from the CEF browser
  public bool OnBeforeResourceLoad(IWebBrowser browser, IRequestResponse requestResponse) {
    // translate CefSharp.IRequestResponse to ServiceStack.IRequest or HttpRequest
    // should execute HelloService.Any() for the requestResponse.Url = "/hello/Zach"
    var response = _serviceHost.ExecuteService(Translate(requestResponse));
    requestResponse.RespondWith(response.Stream);
    return false;
  }
}

[Route("/hello/{Name}")]
public class Hello {
  public string Hello { get; set; }
}
public class HelloService {
  public object Any(Hello request) { // ... }
}

The part I can't figure out is how to extend ServiceStackHost so I can pass some sort of request object to it. Is this even possible?

This might be a stupid answer, but why not just use http anyway? The web is so heavily based on it that things actually gets easier if you use it even in cases like this (where it isn't really necessary).

If this isn't OK, you can implement a custom scheme handler that routes requests to foo://bar to your C# code, and do whatever you like. The CefSharp.Wpf.Example has an example custom scheme handler, so it should help you along the way.

What you're after sounds similar to how MQ Servers execute services in ServiceStack by simply routing messages to:

ServiceController.ExecuteMessage(IMessage)

There are a number of other API's on ServiceController you can use to execute requests in ServiceStack:

//Execute the Request DTO with an empty Request context:
object Execute(object requestDto)

//Execute the Request DTO with the supplied Request context:
object Execute(object requestDto, IRequest request)

For the IRequest context, you can use the built-in BasicRequest class, or your own that implements IRequest .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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