Programmatically adding an endpoint

百般思念 提交于 2019-11-28 14:02:48

To create endpoints and bindings programmatically, you could do this on the service:

ServiceHost _host = new ServiceHost(typeof(TestService), null);

var _basicHttpBinding = new System.ServiceModel.basicHttpBinding();
            //Modify your bindings settings if you wish, for example timeout values
            _basicHttpBinding.OpenTimeout = new TimeSpan(4, 0, 0);
            _basicHttpBinding.CloseTimeout = new TimeSpan(4, 0, 0);
            _host.AddServiceEndpoint(_basicHttpBinding, "http://192.168.1.51/TestService.svc");
            _host.Open();

You could also define multiple endpoints in your service config, and choose which one to connect to dynamically at run time.

On the client program you would then do this:

basicHttpBinding _binding = new basicHttpBinding();
EndpointAddress _endpoint = new EndpointAddress(new Uri("http://192.168.1.51/TestService.svc"));

TestServiceClient _client = new TestServiceClient(_binding, _endpoint);
_client.BlahBlah();

Can you just use:

TestServiceClient client = new TestServiceClient();
client.Endpoint.Address = new EndPointAddress("http://someurl");
client.BlahBlah();

Note that your binding configuration will no longer apply, as you're not using that endpoint configuration in your configuration file. You'll have to override that, too.

You can try:

TestServiceClient client = new TestServiceClient("MyNameSpace.TestService")
client.BlahBlah()

if not recheck namespace in file TestService is correct?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!