Created
April 13, 2017 17:23
-
-
Save DavidBasarab/99d2064f70588309ab1b64df280a01e4 to your computer and use it in GitHub Desktop.
WCF Test Sample for Stackoverflow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ServiceModel; | |
using System.Threading.Tasks; | |
namespace WcfTest | |
{ | |
[ServiceContract] | |
public interface ITestService | |
{ | |
[OperationContract] | |
string MakeQuery(string query); | |
[OperationContract] | |
void SendCommand(string command); | |
} | |
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)] | |
public class TestService : ITestService | |
{ | |
public string MakeQuery(string query) | |
{ | |
Console.WriteLine($"SERVER: Got Query := {query}"); | |
return $"I am responding to := {query}"; | |
} | |
public void SendCommand(string command) | |
{ | |
Console.WriteLine($"SERVER: Got Command := {command}"); | |
} | |
} | |
public class Server | |
{ | |
private readonly ushort port; | |
private readonly TestService testService; | |
private NetTcpBinding netTcpBinding; | |
private ServiceHost serviceHost; | |
public Server(ushort port) | |
{ | |
this.port = port; | |
testService = new TestService(); | |
} | |
public void Start() | |
{ | |
try | |
{ | |
CreateServiceHost(); | |
CreateBinding(); | |
AddEndpoint(); | |
serviceHost.Open(); | |
Console.WriteLine("Service has been opended"); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($"ERROR := {ex.Message}"); | |
Console.WriteLine($"StackTrace := {ex.StackTrace}"); | |
} | |
} | |
private void AddEndpoint() | |
{ | |
serviceHost.AddServiceEndpoint(typeof(ITestService), netTcpBinding, string.Format("{1}://0.0.0.0:{0}/", port, "net.tcp")); | |
} | |
private void CreateBinding() | |
{ | |
netTcpBinding = new NetTcpBinding | |
{ | |
MaxReceivedMessageSize = int.MaxValue, | |
MaxBufferPoolSize = int.MaxValue, | |
Security = new NetTcpSecurity | |
{ | |
Mode = SecurityMode.None, | |
Transport = new TcpTransportSecurity() | |
} | |
}; | |
} | |
private void CreateServiceHost() | |
{ | |
serviceHost = new ServiceHost(testService); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Task.Run(() => | |
{ | |
var server = new Server(8523); | |
server.Start(); | |
}); | |
// Going to Connect | |
var clientBinding = new NetTcpBinding | |
{ | |
MaxReceivedMessageSize = int.MaxValue, | |
MaxBufferPoolSize = int.MaxValue, | |
SendTimeout = TimeSpan.FromSeconds(60), | |
Security = new NetTcpSecurity | |
{ | |
Mode = SecurityMode.None, | |
Transport = new TcpTransportSecurity() | |
} | |
}; | |
var factory = new ChannelFactory<ITestService>(clientBinding); | |
var endpointAddress = new EndpointAddress(string.Format("{2}://{0}:{1}/", "localhost", 8523, "net.tcp")); | |
var client = factory.CreateChannel(endpointAddress); | |
client.SendCommand($"Hey Are you going to get this."); | |
var response = client.MakeQuery("Do you know what time it is?"); | |
Console.WriteLine($"Response from Server := '{response}'"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment