WCF сервис и клиент, запуск и создание программно

Сервер:

Uri baseAddress = new Uri(string.Format("net.tcp://127.0.0.1:{0}/ MyService", port));
ServiceHost serviceHost = new ServiceHost(typeof(MyService));

serviceHost.AddServiceEndpoint(typeof(IMyService),
	new NetTcpBinding(),
	baseAddress);

serviceHost.Open();

Клиент:

var myBinding = new BasicHttpBinding();
var myEndpoint = new EndpointAddress("http://localhost/myservice");
var myChannelFactory = new ChannelFactory<IMyService>(myBinding, myEndpoint);

IMyService client = null;

try
{
    client = myChannelFactory.CreateChannel();
    client.MyServiceOperation();
    ((ICommunicationObject)client).Close();
}
catch
{
    if (client != null)
    {
        ((ICommunicationObject)client).Abort();
    }
}

Add comment

Loading