2

I have WPF applications. I this application I am dynamically adding WebBrowser controls. A signalR chat websites renders in WebBrowser control. The issue is only one hub connection made at a time in WPF. For eg if I have two WebBrowser controls in WPF, One will able to connect to hub and second will fail. But when I open two tabs in IE, both tabs able to make connection to hub. Please help

Update: Given bellow is example with demo code I created:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TabControl x:Name="tabBrowsers" TabStripPlacement="Top"  >
            <TabItem Header="IM 1">
                <WebBrowser x:Name="myBrowser1"  Source=“URL of hub”  Height="100"  HorizontalAlignment="Left" Margin="5,33,0,0" VerticalAlignment="Top"    Cursor="Arrow" />
            </TabItem>
<TabItem Header="IM 2”>
                <WebBrowser x:Name="myBrowser2" Source=“URL of hub”  Height="100"  HorizontalAlignment="Left" Margin="5,33,0,0" VerticalAlignment="Top"     Cursor="Arrow" />
            </TabItem>
           
        </TabControl>
        <Separator Height="100" Background="Transparent"/>
    </StackPanel>
</Window>

Hub connection Javascript code:

$.connection.hub.start().done().fail();

So in this above demo code, myBrowser1 will able to connect to hub but not myBrowser2.

I further investigated and found that if I run two browser controls in two separate WPF application then both are able to connect to hub successfully. After these tests, My assumption is that SignalR restrict single hub connection per exe/process. I hope there is some way to overcome this.

Edit: I found that given bellow code helps me to create the connection.

`$.connection.hub.start({json: true})`

I also changed the settings in startup.cs file to EnableJSONP = true This works may be due to WPF uses IE as engine for its web browser control. But I am not able to send messages from both tabs with web browsers controls. I am receiving messages but not able to send them.

Edit: I found a question which also had similar issue like me. Second WebBrowser control with SignalR freezes javascript

Vijay Rana
  • 123
  • 1
  • 3
  • 13
  • can you put a break point in the hub code and see if it actually gets called the second time at all? – Bassie May 24 '20 at 16:02
  • Yes it calls both time. In first case it goes in .done() function and in second case it goes in .fail() function. I edit the question. – Vijay Rana May 24 '20 at 16:07
  • Please check if there is some error message - can try `.fail(e => { console.log(e); })` could it be because the hub thinks that both connections are coming from the same client? Do you have access the the actual hub on the back end?? – Bassie May 24 '20 at 18:08
  • @Bassie I have access to hub and back end both. `.fail(e => { console.log(e); })` not seems to be working because fail didn't returning anything. As I am running this in WPF, I can't see the logs generated by SignalR in console – Vijay Rana May 24 '20 at 18:55
  • @Bassie I am able to use `.fail(function (e) {alert(e)});` This is giving me error message 'Error: Error during start request. Stopping the connection.' – Vijay Rana May 24 '20 at 19:11
  • don't understand you comment about WPF. Can't you put a try/catch and log the error somewhere? cant you put a breakpoint in VS? without clarifying this information it is very difficult to help you – Bassie May 25 '20 at 17:39
  • @Bassie: I am using web browser control in WPF. `$.connection.hub.start({json:true})` make me able to do the connection but messages not working. I am able to receive the messages on both tabs but not able to sent them – Vijay Rana May 28 '20 at 17:00
  • This issue is fixed automatically when I tested by our production server. Means changed the server of signalr application. I still don't know the reason of issue. – Vijay Rana Nov 09 '20 at 09:48

0 Answers0