当前位置: 首页 > 编程语言 > C#

如何在运行时设置端点分享

时间:2023-04-10 18:22:34 C#

如何在运行时设置端点我有一个应用程序基于这个教程我用来测试与服务器的连接的方法(在客户端应用程序中):publicclassPBMBService:IService{privatevoidbtnPing_Click(objectsender,EventArgse){ServiceClientservice=newServiceClient();tbInfo.Text=service.Ping().Replace("n","rn");服务.关闭();}//其他方法}Servicemainfunction:classProgram{staticvoidMain(string[]args){UribaseAddress=newUri("http://localhost:8000/PBMB");ServiceHostselfHost=newServiceHost(typeof(PBMBService),baseAddress);尝试{selfHost.AddServiceEndpoint(typeof(IService),newWSHttpBinding(),"PBMBService");ServiceMetadataBehaviorsmb=newServiceMetadataBehavior();smb.HttpGetEnabled=true;selfHost.Description.Behaviors.Add(smb);selfHost.Open();Console.WriteLine("Serwisgotowy.");Console.WriteLine("Naci?nijabyzamkn??serwis.");控制台.WriteLine();控制台.ReadLine();selfHost.Close();catch(CommunicationExceptionce){Console.WriteLine("Nast?pi?wyj?tek:{0}",ce.Message);selfHost.Abort();}}}在app.config中我有:我可以从这里更改IP但是如何在运行时更改它(即从文件中读取地址/IP)?您可以在创建客户端类后替换服务端点:service.Endpoint.Address=newEndpointAddress("http://the.new.address/to/the/service");tbInfo.Text=service.Ping().Replace("n","rn");服务.关闭();您可以使用以下通道工厂:usingSystem.ServiceModel;namespacePgAuthentication{publicclassServiceClientFactory:ChannelFactorywhereTChannel:class{publicTChannelCreate(stringurl){returnCreateChannel(newBasicHttpBinding{Security={Mode=BasicHttpSecurityMode.NoneAdds}url);}}}可以使用如下代码:以上就是《C#学习教程:如何在运行时设置端点共享》的全部内容。WriteLine(newServiceClientFactory().Create("http://crm.payamgostar.com/Services/IAuthentication.svc").AuthenticateUserNameAndPassWord("o","123","o","123").成功);本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处: