WCF 第一次尝试:如何使 BasicHttpBinding 工作

由于目标机器主动拒绝,无法建立套接字连接


我为 WCF 服务和客户端实现了教科书(2008 年出版)中的样板代码。两者都在 Windows 10 下的同一台机器上运行,使用普通用户帐户(无管理员)。


我研究了书中的信息以找到解决此问题的方法,但我不明白他们在说什么。


服务

using (var host = new ServiceHost(typeof(Service1), new Uri("http://localhost:8123/Service1")))

{

    host.AddServiceEndpoint(typeof(IService1), new BasicHttpBinding(), "Service1Http");

    host.Open();

}

这适用于管理员权限(不好,但第一次尝试可以)。


客户

var ep = new EndpointAddress("http://localhost:8123/Service1/Service1Http");


var proxy = ChannelFactory<IService1>.CreateChannel(new BasicHttpBinding(), ep);


string s = proxy.SayHello();

最后一行抛出异常,表示连接被“主动拒绝”。


有人可以对此有所了解吗?


慕神8447489
浏览 136回答 2
2回答

天涯尽头无女友

是的,正如您所说,客户端需要在服务器自动关闭之前调用服务。您可以使用以下语句来防止服务器自动关闭。class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Uri uri = new Uri("http://localhost:13020");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BasicHttpBinding binding = new BasicHttpBinding();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; binding.Security.Mode = BasicHttpSecurityMode.None;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (ServiceHost sh=new ServiceHost(typeof(MyService),uri))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sh.AddServiceEndpoint(typeof(IService), binding, "");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ServiceMetadataBehavior smb;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (smb==null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; smb = new ServiceMetadataBehavior()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpGetEnabled = true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sh.Description.Behaviors.Add(smb);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Binding mexbinding = MetadataExchangeBindings.CreateMexHttpBinding();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sh.AddServiceEndpoint(typeof(IMetadataExchange), mexbinding, "mex");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sh.Opened += delegate&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("service is ready...");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sh.Closed += delegate&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Service is closed now...");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sh.Open();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sh.Close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp;}一个问题仍然存在:如何让服务器在没有管理员权限的情况下运行?您可以为您的应用程序帐户提供所需的权限。为此,我们为帐户保留 URL 命名空间,允许相关应用程序注册该 URL。我们可以使用Netsh命令(这个工具需要管理员权限)。Netsh http add urlacl url=http://+:8080/ user=domain\user您应该用您的实际帐户代替domain\user,并使用实际端口而不是8080,例如管理员。Netsh http add urlacl url=http://+:8123/ user=administrator“ + ”是通配符,表示所有网卡请求都可以访问这个url。这是有关此工具的官方文档。https://docs.microsoft.com/en-us/windows/desktop/Http/netsh-commands-for-http如果有什么我可以帮忙的,请随时告诉我。

开满天机

这可能是由于服务器/客户端之间的代理配置错误。我认为这会有所帮助。
打开App,查看更多内容
随时随地看视频慕课网APP