2
votes

My Scenerio is that i need a SignalR self Hosted WCF Service that response and sends message to all connected users that came from Winform or WPF.

I have tried alot as follows:

  • I have Created WCF service with SignalR Self Hosting code as below which contains 3 Classes and 1 Interface.
        namespace SignalRServiceClass
{
    [ServiceContract]
    public interface ISignalRServiceClass
    {
        [OperationContract]
        string GetsMessage(string name);
        [OperationContract]
        void Configuration(IAppBuilder app);

        [OperationContract]
        void Send(string name, string message);
    }
}
namespace SignalRServiceClass
{
    public class SignalRServiceClass : ISignalRServiceClass
    {
        public string GetsMessage(string name)
        {
            return "Message From Service " + name + "!";
        }
    }
}
namespace SignalRServiceClass
{
    class ClassHub : Hub
    {
        public void Send(string name, string message)
        {
            Clients.All.addMessage(name, message);
        }
    }
}

namespace SignalRServiceClass
{
    class Startup
    {
        public void Configuration(IAppBuilder app)
        {
           // app.UseCors(CorsOptions.AllowAll);
           // app.MapSignalR();
            app.Map("/signalr", map =>
                {
                    map.UseCors(CorsOptions.AllowAll);
                    var hubConfiguration= new HubConfiguration
                    {
                        EnableDetailedErrors=true,
                        EnableJSONP= true
                    };
                    map.RunSignalR(hubConfiguration);
                });
        }
    }
}

And Secondly Winform Client. I am confused here that how to manage the client code here but i put some code for testing as below.


 private void button1_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(test.GetsMessage("This is the Test Message"));

            var hubConnection = new HubConnection("http://localhost:50172/");
            var serverHub = hubConnection.CreateHubProxy("MessageRecievingHub");
            serverHub.On("broadCastToClients", message => MessageBox.Show(message));
            hubConnection.Start().Wait();
        }

Please guide me in this manner. Your Help will be appreciated. I have tried and googled alot but in vain.

Thanks alot in Advance.

3

3 Answers

3
votes

You do not want SignalR, you need XSockets WCF sample

1
votes

SignalR and WCF don't interoperate in this way, and don't really need to. If you're using SignalR, there's no reason to use WCF- you can publish your hub on IIS or self-hosted (see the Getting Started tutorial and the Self-Host tutorial at asp.net/signalr), and connect to it with desktop or JavaScript/HTML clients.

0
votes

You can easily create a .NET client application to communicate with your SignalR server - below is a simple WinForm .NET C# client that sends and receives a SignalR message:

namespace SignalrTestClient
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    async void Form1_Load(object sender, EventArgs e)
    {
        var hubConnection = new HubConnection("http://localhost:8080/");
        IHubProxy hubProxy = hubConnection.CreateHubProxy("MyHub");
        await hubConnection.Start();
        hubProxy.On("addMessage", message => onData(message));
        await hubProxy.Invoke("Send", "Hello SignalR Server!");
    }

    private void onData(string msg)
    {
        Console.WriteLine(msg);
    }
}
}

In your SignalR server you just need the following hub class:

public class MyHub : Hub
{
    public void Send(string message)
    {
        Console.WriteLine("Received a message from a client");
        if (message.Contains("Hello")) {
            Clients.All.addMessage("Well hello there client!");
        }
    }
}

It is also possible to create a C++ client for SignalR