I'm trying to implement web socket service in my existing wcf solution.
To do this I create classes specified below:
[ServiceContract]
public interface IExampleWebSocketServiceCallback
{
[OperationContract(IsOneWay = true, Action = "*")]
Task SendMessageToClient(Message msg);
}
[ServiceContract(CallbackContract = typeof(IExampleWebSocketServiceCallback))]
public interface IExampleWebSocketService
{
[OperationContract(IsOneWay = true, Action = "*")]
Task SendMessageToServer(Message msg);
}
public class ExampleWebSocketService : IExampleWebSocketService
{
public async Task SendMessageToServer(Message msg)
{
var callback = OperationContext.Current.GetCallbackChannel<IExampleWebSocketServiceCallback>();
if (msg.IsEmpty || ((IChannel)callback).State != CommunicationState.Opened)
{
return;
}
byte[] body = msg.GetBody<byte[]>();
string msgTextFromClient = Encoding.UTF8.GetString(body);
string msgTextToClient = string.Format(
"Got message {0} at {1}",
msgTextFromClient,
DateTime.Now.ToLongTimeString());
await callback.SendMessageToClient(
CreateMessage(msgTextToClient));
}
private Message CreateMessage(string msgText)
{
Message msg = ByteStreamMessage.CreateMessage(
new ArraySegment<byte>(Encoding.UTF8.GetBytes(msgText)));
msg.Properties["WebSocketMessageProperty"] =
new WebSocketMessageProperty
{ MessageType = WebSocketMessageType.Text };
return msg;
}
}
And here's key parts of my app configuration file for this service:
<service behaviorConfiguration="ExampleWebSocketBehaviour" name="ExampleWebSocketService">
<endpoint address="" binding="netHttpBinding" bindingConfiguration="textWSHttpBinding"
contract="IExampleWebSocketService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:9896/ExampleWebSocketService/" />
</baseAddresses>
</host>
</service>
<behavior name="ExampleWebSocketBehaviour">
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
<netHttpBinding>
<binding name="textWSHttpBinding" messageEncoding="Text" maxBufferPoolSize="9999999"
maxReceivedMessageSize="9999999"
maxBufferSize="9999999" >
<webSocketSettings transportUsage="Always" createNotificationOnConnection="true"/>
</binding>
</netHttpBinding>
My existing solution not working properly - every time when i try to connect, I'm getting bad request response that i show in example below:
Could not connect to ws://localhost:9896/ExampleWebSocketService/
Error: Unexpected server response: 400
Handshake Details
Request URL: http://localhost:9896/ExampleWebSocketService/
Request Method: GET
Status Code: 400 Bad Request
Request Headers
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: 12EXCkp1jWLWDwH4xOI5lg==
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Host: localhost:9896
Response Headers
Transfer-Encoding: chunked
Server: Microsoft-HTTPAPI/2.0
soap-content-type: application/soap+xml; charset=utf-8
I can't find what cause this problem ? Is there is someting that I need to define also in app configuration to make it work ?